Obtaining a Location from a Marker in Google Maps For Android v2
I am trying to obtain the position of a marker so that I can use the
distanceTo() method to determine the distance between my position and the
Marker Position.
So firstly, I declare the Marker as well as Location globally (in order to
use distanceTo()) like so:
private Marker hamburger;
private Location enemyLocation;
I also declare a few positions on he map globally, for instance
private LatLng MYMARK9 = new LatLng(-22.848612,25.178946);
private LatLng MYMARK10 = new LatLng(-22.846132,25.184652);
Then, in the onCreate method, I create an instance of a GoogleMap called
theMap. I then add the marker that I declared globally to the map like so:
hamburger = theMap.addMarker(new
MarkerOptions().position(MYMARK9).title("Position1"));
up to this point the code works fine, but if I add a line for the Location
to get it's position from the marker like so:
enemyLocation.setLatitude(hamburger.getPosition().latitude);
enemyLocation.setLongitude(hamburger.getPosition().longitude);
then the code breaks when executing on my android device. However, if I
replace the same lines above with:
enemyLocation.setLatitude(MYMARK9.latitude);
enemyLocation.setLongitude(MYMARK9.longitude);
the code works fine, except that enemyLocation now only has the value from
MYMARK9, meaning that if Marker hamburger moves, then the enemyLocation
does not change, which is not useful for my purposes.
If anyone can explain why this is, I would appreciate this very much!
Thank you in advance!
No comments:
Post a Comment