0

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;

Nompumelelo
  • 929
  • 3
  • 17
  • 28
Nick
  • 51
  • 2
  • 7
  • Are you referring to `MapView` and `MyLocationOverlay`, or some facility through the Web-based Google Maps? – CommonsWare Apr 17 '12 at 22:24
  • I am referring to MapView and MyLocationOverlay. I have commented the answer below, I have the icon changed but need to get it to point in the same direction as the phone... – Nick Apr 18 '12 at 23:30

2 Answers2

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.:

Community
  • 1
  • 1
MH.
  • 45,303
  • 10
  • 103
  • 116
  • Thanks for that, I have got the icon changed but the real thing I need is getting it to point in the direction of the device's travel. The code from this example: http://stackoverflow.com/questions/753793/how-can-i-use-a-custom-bitmap-for-the-you-are-here-point-in-a-mylocationoverlay Doesn't seem to work. I have tried creating a variable in my locationlistener using getBearing() from the GPS, and then feeding that variable into matrix.postrotate() but not having any luck so far... – Nick Apr 18 '12 at 23:26
  • *"Doesn't seem to work"* - as in: doesn't do anything, or doesn't do what you expected? I'm pretty sure there will be more examples on this subject floating around. I suggest you do seach for how to create a custom compass on a `MapView`, as that mainly concerns the rotational aspect you're having issues with. – MH. Apr 19 '12 at 19:17
  • It doesn't do anything - I have a toast message with the current bearing that is reading 0.0. HOWEVER I have had it working for all of 5 seconds, then it goes back to being 0.0 again so I know that technically it should be working? :S I'll add parts of the code to the main question... – Nick Apr 20 '12 at 14:13
  • @Nick: I had a look at the code you added and was surprised to see you're not using the [`getOrientation()`](https://developers.google.com/maps/documentation/android/reference/com/google/android/maps/MyLocationOverlay#getOrientation%28%29) method in `MyLocationOverlay`. Any particular reason why you're trying to get the bearing from GPS in stead of the compass? – MH. Apr 20 '12 at 21:26
  • I needed to get the actual direction of travel, rather than the direction the device is pointing in – Nick Apr 29 '12 at 20:37
0

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

Community
  • 1
  • 1
Nick
  • 51
  • 2
  • 7