Reputation: 1194
I have implemented GoogleMaps into my application. What I am trying to do is show the users current location and then allow the user to add a marker anywhere else on the map. I have shown the users current location by setting setMyLocationEnabled to true. Then a marker is added wherever you click on the screen. I want to get the new lat/lng coordinates of the marker but it keeps giving me the coordinates of the users location. When I change setMyLocationEnabled to false and add a marker it returns the correct coordinates. So it seems as if I might have to write my own code to get the users current location instead of using setMyLocationEnabled. Any ideas? Below is my code
public class BasicMapDemoActivity extends FragmentActivity implements
OnMarkerDragListener, OnMapClickListener {
private GoogleMap mMap;
private String TAG = "TEST";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.basic_demo);
setUpMapIfNeeded();
mMap.setMyLocationEnabled(true);
mMap.setOnMarkerDragListener(this);
mMap.setOnMapClickListener(this);
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the
// map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
// setUpMap();
}
}
}
@Override
public void onMarkerDrag(Marker marker) {
// TODO Auto-generated method stub
}
@Override
public void onMarkerDragEnd(Marker marker) {
// TODO Auto-generated method stub
LatLng position = marker.getPosition(); //
Toast.makeText(
BasicMapDemoActivity.this,
"Lat " + position.latitude + " " + "Long " + position.longitude,
Toast.LENGTH_LONG).show();
}
@Override
public void onMarkerDragStart(Marker marker) {
// TODO Auto-generated method stub
}
@Override
public void onMapClick(LatLng point) {
// TODO Auto-generated method stub
// Creating a marker
MarkerOptions markerOptions = new MarkerOptions();
// Setting the position for the marker
markerOptions.position(point);
// Setting the title for the marker.
// This will be displayed on taping the marker
markerOptions.title(point.latitude + " : " + point.longitude);
// Clears the previously touched position
mMap.clear();
// Animating to the touched position
mMap.animateCamera(CameraUpdateFactory.newLatLng(point));
// Placing a marker on the touched position
mMap.addMarker(markerOptions).setDraggable(true);
}
}
Upvotes: 1
Views: 3733
Reputation: 16768
It is not clear to me where the marker is coming from in the method you are trying to get it's location from, but it does not look like you are retaining a reference to the marker you want to report the position of.
When you call this method:
// Placing a marker on the touched position
mMap.addMarker(markerOptions).setDraggable(true);
it returns a reference to the marker that was added. I think you need to hold the reference it returns and then query the location based on that reference.
So you could try something like this:
// Placing a marker on the touched position
Marker myMarker = mMap.addMarker(markerOptions).setDraggable(true);
LatLng position = myMarker.getPosition(); //
Toast.makeText(
BasicMapDemoActivity.this,
"Lat " + position.latitude + " " + "Long " + position.longitude,
Toast.LENGTH_LONG).show();
First of all I don't see what in the posted code is triggering the call to the onMarkerDragEnd() event. I don't see anything in the doc that says it would be triggered simply by adding a marker to the map. If you want to get the location from the maker added at the end of the onMapClick() method, then, what I show above should do it. While this should be the same as the point object being passed into the onMapClick() event, the question asked about the position of the marker that was added, not the point clicked, even tho in this case they should be the same.
Upvotes: 4