rookieDeveloper
rookieDeveloper

Reputation: 2468

Get latitude and longitude on click of Marker

I am getting lat/lng: (19.1972,72.93) in my String using

String position=String.valueOf(marker.getPosition());

how to get only latitude and longitude, I don't want to use split

Thanks in advance!

Upvotes: 0

Views: 3745

Answers (3)

Pran R.V
Pran R.V

Reputation: 1156

You can use (markerClick) instead of click with event. in ts file you can use event.latitude or event.longitude to get them }

Upvotes: 1

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75798

You can split Lat Long this way

String getLatLong="19.1972,72.93";
String[] str_split= getLatLong.split(","); // Split.
String getLat= str_split[0];
String getLong= str_split[1];

You should follow setOnMarkerClickListener

 your_map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener()
        {

            @Override
            public boolean onMarkerClick(Marker arg0) {
               // Creating a marker
            MarkerOptions markerOptions = new MarkerOptions();

            // Setting the position for the marker
            markerOptions.position(arg0); // get Latlong
            // Now you use above logic  
                return true;
            }

        });

Edit

use marker.getPosition().latitude; for better approach .

Upvotes: 1

jos
jos

Reputation: 1070

With marker.getPosition() you get a LatLng object which has a latitude and longitude properties.

So, you can do:

Double latitude = marker.getPosition().latitude;
Double longitude = marker.getPosition().longitude;

Hope it helps!

Upvotes: 6

Related Questions