Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert String to LatLng

I'm using Google Maps API v2 and I get a location coordinates single String "-34.8799074,174.7565664" from my SharedPreferences that I need to convert to LatLng.

Can anyone help with this please?

Thx!

like image 710
Makoto Avatar asked Dec 03 '14 00:12

Makoto


People also ask

How do you get Latlng?

On your computer, open Google Maps. Right-click the place or area on the map. This will open a pop-up window. You can find your latitude and longitude in decimal format at the top.

What does Latlng mean?

An immutable class representing a pair of latitude and longitude coordinates, stored as degrees.


1 Answers

[Google Maps Android API]

You can split the string by comma and then parse the string to long

String[] latlong =  "-34.8799074,174.7565664".split(",");
double latitude = Double.parseDouble(latlong[0]);
double longitude = Double.parseDouble(latlong[1]);

To constructs a LatLng with the given latitude and longitude coordinates

LatLng location = new LatLng(latitude, longitude);

[Google Maps JavaScript API]

To do same operation with Maps JavaScript API service (JSFiddle demo) -

  var latlong =  '-34.397,150.644'.split(',');
  var latitude = parseFloat(latlong[0]);
  var longitude = parseFloat(latlong[1]);
  var mapOptions = {
    zoom: 8,
    center: {lat: latitude, lng: longitude}
  };
like image 111
display name Avatar answered Nov 13 '22 14:11

display name