将字符串转换为LatLng

问题描述:

我正在使用Google Maps API v2,并且从我需要转换为LatLng的SharedPreferences中获得了位置坐标单个字符串"-34.8799074,174.7565664".

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.

任何人都可以帮忙吗?

谢谢!

[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]);

要构建 LatLng 具有给定的纬度和经度坐标

To constructs a LatLng with the given latitude and longitude coordinates

LatLng location = new LatLng(latitude, longitude);

[Google Maps JavaScript API]

要使用Maps JavaScript API服务执行相同的操作( JSFiddle演示)-

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}
  };