Andrew Stewart
Andrew Stewart

Reputation: 279

Get longitude and latitude of marker on google maps android

In my app the user can onLongClick add a marker to google maps. It saves these coordinates as a point. I know that with the location = location.getLastKnownLocation the methods getLongitude() and getLatitude() can be used. Is there anyway to do this with a marker placed on google maps by the user so that the coordinates can be retrieved. This is the code that gets the marker point and stores it.

public void onMapLongClick(LatLng point) {
    tvLocInfo.setText("New marker added@" + point.toString());             map.addMarker(new MarkerOptions().position(point).title(point.toString())); 
pointfinal = point;
Toast.makeText(this, point.toString(), Toast.LENGTH_LONG).show();
}

Upvotes: 1

Views: 3943

Answers (2)

Lal
Lal

Reputation: 14820

See this link

It says

map.setOnInfoWindowClickListener(
    new OnInfoWindowClickListener(){
    @Override
    public void onInfoWindowClick(Marker arg0) {

        // TODO Auto-generated method stub
        arg0.hideInfoWindow();
        double dlat =arg0.getPosition().latitude;
        double dlon =arg0.getPosition().longitude;
        String slat = String.valueOf(dlat);
        String slon = String.valueOf(dlon);
        Log.d("Position","Lat:"+slat+",Lon:"+slon);  
    }
});

Upvotes: 1

Carlos J
Carlos J

Reputation: 3045

Try this:

public void onMapLongClick(LatLng point) {
    tvLocInfo.setText("New marker added@" + point.toString());        
    //Create a marker object
    Marker myMarker = map.addMarker(new MarkerOptions().position(point).title(point.toString()));

    //And now you can use it's values
    myMarker.getPosition().latitude;
    myMarker.getPosition().longitude;
}

If you have multiple markers you can use an array to store all of them

Upvotes: 3

Related Questions