Android programming, mapview locationoverlay: how

2019-08-16 06:59发布

问题:

I need to work out how to change the default 'my location' blue dot with an image, that will also act in the same fashion as a compass pointing the direction of travel. This can be worked out by the device compass or by detecting direction of travel from GPS

I've done some googling but so far only found a reference for the method of changing the dot on the iPhone, and I am working with Android...

I'd appreciate some support and guidance in this

Kind regards

Nick

In OnCreate: LocationManager locman = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    locman.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 2, locationListener); 

    //Adds a current location overlay to the map 'mapView' and turns on the map's compass

    MyLocation myLocationOverlay = new MyLocation(this, mapView);
    mapView.getOverlays().add(myLocationOverlay);
    myLocationOverlay.enableMyLocation();
    myLocationOverlay.enableCompass();

    mapView.getOverlays().add(myLocationOverlay);
    mapView.postInvalidate();

In OnLocationChanged:

dbllatitude = locFromGps.getLatitude();
        dbllongitude = locFromGps.getLongitude();
        dblaltitude = locFromGps.getAltitude();


bearing = locFromGps.getBearing(); 


     strlatitude = Double.toString(dbllatitude);
        strlongitude = Double.toString(dbllongitude);
        dblaltitude = (dblaltitude / 0.3048); 

        LocationText.setText("Your Location: Latitude " + dbllatitude + " Longitude: " +dbllongitude + " Altitude " + dblaltitude);



        boolean hasbearing = locFromGps.hasBearing();


        if (hasbearing =  false) {

            Toast.makeText(getApplicationContext(), "No bearing", Toast.LENGTH_SHORT).show();

        }
        else
        {
            Toast.makeText(getApplicationContext(), "I HAZ bearing: " + bearing, Toast.LENGTH_SHORT).show();
        }
        MyLocation.mOrientation = bearing;

回答1:

Subclass MyLocationOverlay, override the drawMyLocation(...) method and draw your image using the provided parameters. There are several examples here on SO and blogs that can help you on your way, e.g.:

  • How to properly use drawMyLocation
  • How can I use a custom bitmap for the "you are here" point in a MyLocationOverlay?
  • How to draw at the current GPS location on MapView using MyLocationOverlay?
  • myLocationOverlay change the marker


回答2:

For anyone else looking to get similar functionality of rotating the user location icon see the post at:

Android getbearing only returns 0.0. Trying to use to rotate current location icon

the location manager .getbearing() comes in handy

Regards

Nick