Android Maps API v2 Change MyLocation Icon

后端 未结 6 1615
野趣味
野趣味 2020-11-27 04:50

I would like to replace the default icon that Android Maps V2 uses for \'My Location\' (The little blue dot/arrow) with my own image. I\'ve created my own tile provide

相关标签:
6条回答
  • 2020-11-27 05:25

    The latest update to Google Play Services now allows you to use 'Flat' Markers and rotate them which is exactly what I needed. Here is a simple version of the way I implemented it. There is probably a fair bit I can do to optimize and tweak the animation, but it does the job for the moment. Any feedback is welcome.

    Marker mPositionMarker;
    GoogleMap mMap;
    
    @Override
    public void onLocationChanged(Location location) {
    
        if (location == null)
            return;
    
        if (mPositionMarker == null) {
    
            mPositionMarker = mMap.addMarker(new MarkerOptions()
                    .flat(true)
                    .icon(BitmapDescriptorFactory
                            .fromResource(R.drawable.positionIndicator))
                    .anchor(0.5f, 0.5f)
                    .position(
                            new LatLng(location.getLatitude(), location
                                    .getLongitude())));
        }
    
        animateMarker(mPositionMarker, location); // Helper method for smooth
                                                    // animation
    
        mMap.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(location
                .getLatitude(), location.getLongitude())));
    
    }
    
    public void animateMarker(final Marker marker, final Location location) {
        final Handler handler = new Handler();
        final long start = SystemClock.uptimeMillis();
        final LatLng startLatLng = marker.getPosition();
        final double startRotation = marker.getRotation();
        final long duration = 500;
    
        final Interpolator interpolator = new LinearInterpolator();
    
        handler.post(new Runnable() {
            @Override
            public void run() {
                long elapsed = SystemClock.uptimeMillis() - start;
                float t = interpolator.getInterpolation((float) elapsed
                        / duration);
    
                double lng = t * location.getLongitude() + (1 - t)
                        * startLatLng.longitude;
                double lat = t * location.getLatitude() + (1 - t)
                        * startLatLng.latitude;
    
                float rotation = (float) (t * location.getBearing() + (1 - t)
                        * startRotation);
    
                marker.setPosition(new LatLng(lat, lng));
                marker.setRotation(rotation);
    
                if (t < 1.0) {
                    // Post again 16ms later.
                    handler.postDelayed(this, 16);
                }
            }
        });
    }
    
    0 讨论(0)
  • 2020-11-27 05:25

    You can not handle click event of MyLocation button on Google map, yeah but there is a trick through which you can get behavior as per your need:
    first in your layout.xml file add map and dummy MyLocation button.
    You can achieve this with the help of Relative Layout:

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    
        <fragment
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
        <Button
            android:id="@+id/myLocationButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp"
            android:background="@drawable/mylocation" />
    </RelativeLayout>
    


    You can take "mylocation.png" seems like u see on Google map.
    LocationManager locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);

        Criteria criteria = new Criteria();
        String s = locationManager.getBestProvider(criteria, false);
    
        Location location = locationManager.getLastKnownLocation(s);  
    

    Call click event of your dummy MyLocation button, in which you will add marker
    private GoogleMap googleMap; googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude()); googleMap.addMarker(new MarkerOptions().icon(YOUR_MODIFIED_ICON).position(latLng).title(TITLE).snippet(SNIPET));
    In this way you can modify Google map's default icon.
    Yeah also you need to set zoom level:
    googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

    0 讨论(0)
  • 2020-11-27 05:34

    my simple solution way is just disable "my location" of Google map

    and create ImageView on Map with my icon then capture ImageView with

    onClick and getMyLocation , animateCamera in onClick

     this.mGoogleMap.getUiSettings().setMyLocationButtonEnabled(false);
     this.mGoogleMap.setMyLocationEnabled(true);
    

    .

    @Override
    public void onClick(final View v) {
    
        Location location = this.mGoogleMap.getMyLocation();
    
            if (location != null) {
    
                LatLng target = new LatLng(location.getLatitude(), location.getLongitude());
                CameraPosition position = this.mGoogleMap.getCameraPosition();
    
                Builder builder = new CameraPosition.Builder();
                builder.zoom(15);
                builder.target(target);
    
                this.mGoogleMap.animateCamera(CameraUpdateFactory.newCameraPosition(builder.build()));
    
              }    
    }
    
    0 讨论(0)
  • 2020-11-27 05:35

    try this; I use this for draw a route on the map, but I set icons for the markers and for my location Declare this for view your location:

        LocationManager locationManager = (LocationManager) 
        getSystemService(LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        String provider = locationManager.getBestProvider(criteria, true);
        Location myLocation= locationManager.getLastKnownLocation(provider);
    
        longitude = myLocation.getLongitude();
        latitude  = myLocation.getLatitude();
        fromPosition = new LatLng(latitude, longitude);
    

    And create a function like that, I used for draw on the map but I set icons on the markers and my position:

    mGoogleMap.clear();
    if(response.equalsIgnoreCase("Success"))
    {
         ArrayList<LatLng> directionPoint = v2GetRouteDirection.getDirection(document);
         PolylineOptions rectLine = new PolylineOptions().width(7).color(Color.BLUE);
    
         for (int i = 0; i < directionPoint.size(); i++) {
         rectLine.add(directionPoint.get(i));
         }
         // Adding route on the map
         mGoogleMap.setOnMarkerClickListener(MainActivity.this);
         mGoogleMap.addPolyline(rectLine);
         markerOptions.position(fromPosition);
         markerOptions.draggable(true);
         Marker1 = mGoogleMap.addMarker(new MarkerOptions()
                   .position(Estadio)
                   .title("Estadio Cuscatlan")
                   .snippet("Estadio Cuscatlan")                                                    
                   .icon(BitmapDescriptorFactory                                          
                   .fromResource(R.drawable.mapmarker))); 
         Marker2 = mGoogleMap.addMarker(new MarkerOptions()
                   .position(Metrocentro)
               .title("Metrocentro")
               .snippet("Metrosuelo")
               .icon(BitmapDescriptorFactory
               .fromResource(R.drawable.mapmark
          Marker3 = mGoogleMap.addMarker(new MarkerOptions()
                .position(Mejicanos)
                .title("Mejicanos")
                .snippet("Mejicanos")
                .icon(BitmapDescriptorFactory
                .fromResource(R.drawable.mapmarker)));  
          MarkerMe = mGoogleMap.addMarker(new MarkerOptions()                                             
                            .position(fromPosition) 
                            .icon(BitmapDescriptorFactory
                            .fromResource(R.drawable.car64)));
       }
        Dialog.dismiss();
        } 
    } 
    
    0 讨论(0)
  • 2020-11-27 05:44

    As of 3.1.36 you cannot change the icon (I feel it will change in the future, but looking at the speed of API v2 improvements implementation, it won't be before Christmas).

    You can however use a marker now, because it has setIcon function.

    Basically you have to forget about GoogleMap.setMyLocationEnabled, because it will always appear above. You will instead create your own LocationClient and use lat, long and bearing from Location to update the Marker. Additionally add a Circle and use accuracy to have an effect similar to the default myLocation visualization.

    0 讨论(0)
  • 2020-11-27 05:47

    Here is the code to access location button:

    View mv=findViewById(R.id.map);
            View locationButton = ((View) mv.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2"));
            RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) locationButton.getLayoutParams();
    // position on right bottom
            rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
            rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
            rlp.setMargins(0, 180, 180, 0);
    
    0 讨论(0)
提交回复
热议问题