Convert String to LatLng

2019-03-18 12:49发布

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!

2条回答
老娘就宠你
2楼-- · 2019-03-18 13:17

[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}
  };
查看更多
看我几分像从前
3楼-- · 2019-03-18 13:23

Firstly you will want to split the string on the comma:

String[] latLng = "-34.8799074,174.7565664".split(",");

This will then give you two String variables, which you will want to parse as doubles like so because the LatLng constructor takes two doubles for the latitude and longitude of the location:

double latitude = Double.parseDouble(latLng[0]);
double longitude = Double.parseDouble(latLng[1]);

Finally, adding to the previous answer, you will then want to put these into a LatLng object which is the class frequently used by Google Maps:

LatLng location = new LatLng(latitude, longitude);
查看更多
登录 后发表回答