Android programming, mapview location-overlay: how to change the default blue location dot, with another image

不问归期 提交于 2019-12-11 16:47:36

问题


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



来源:https://stackoverflow.com/questions/10200074/android-programming-mapview-location-overlay-how-to-change-the-default-blue-lo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!