任何人都可以解释如何使用位置名称获取位置的纬度和经度值
答案 0 :(得分:1)
答案 1 :(得分:0)
使用以下代码通过反向地理编码从位置名称获取lat long值 getFromLocationName()
Geocoder coder;
double latitude = 0.0;
double longitude = 0.0;
try { List<Address> address = coder.getFromLocationName(localionNameString, 5);
// if address is found
if (address.size() > 0) {
Address location = address.get(0);
latitude = location.getLatitude();
longitude = location.getLongitude();
}
} catch (Exception e) {
e.printStackTrace();
}
希望这有效
答案 2 :(得分:0)
它被称为Geocoding。假设addr在strAddres中,你可以使用下面的逻辑来获得lat和lon。
Geocoder coder = new Geocoder(this);
List<Address> address;
try {
address = coder.getFromLocationName(strAddress,5);
if (address == null) {
return null;
}
Address location = address.get(0);
location.getLatitude();
location.getLongitude();
p1 = new GeoPoint((int) (location.getLatitude() * 1E6),
(int) (location.getLongitude() * 1E6));
return p1;
}
答案 3 :(得分:0)
可能是我的MapHelper
帮助你 -
public class MapHelper {
public static void setAddress(String address, GoogleMap map){
JSONObject jsonObject = getLocationInfo(address);
Double longitute = getLong(jsonObject);
Double latitude = getLat(jsonObject);
CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(latitude, longitute));
CameraUpdate zoom = CameraUpdateFactory.zoomTo(15);
map.moveCamera(center);
map.animateCamera(zoom);
map.clear();
map.addMarker(new MarkerOptions().position(new LatLng(latitude, longitute)).title(address));
}
public static JSONObject getLocationInfo(String address) {
StringBuilder stringBuilder = new StringBuilder();
try {
address = address.replaceAll(" ", "%20");
HttpPost httppost = new HttpPost("http://maps.google.com/maps/api/geocode/json?address=" + address + "&sensor=false");
HttpClient client = new DefaultHttpClient();
HttpResponse response;
stringBuilder = new StringBuilder();
response = client.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();
int b;
while ((b = stream.read()) != -1) {
stringBuilder.append((char) b);
}
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
JSONObject jsonObject = new JSONObject();
try {
jsonObject = new JSONObject(stringBuilder.toString());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jsonObject;
}
public static Double getLong(JSONObject jsonObject) {
Double longitute = 0.0;
try {
longitute = ((JSONArray) jsonObject.get("results")).getJSONObject(0)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lng");
} catch (JSONException e) {
}
return longitute;
}
public static Double getLat(JSONObject jsonObject) {
Double latitude = 0.0;
try {
latitude = ((JSONArray) jsonObject.get("results")).getJSONObject(0)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lat");
} catch (JSONException e) {
}
return latitude;
}
}
如何使用:
MapHelper.setAddress("New York", mapFragment.getMap())