0

任何人都可以解释如何使用位置名称获取位置的纬度和经度值

4

4 回答 4

1

这应该可以帮助您Android:反向地理编码 - getFromLocation

于 2013-10-21T08:48:19.580 回答
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())
于 2013-10-21T09:20:10.073 回答
0

它称为地理编码。假设地址在 strAddres 中,您可以使用以下逻辑来获取纬度和经度。

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;
}
于 2013-10-21T09:05:08.717 回答
0

使用以下代码通过使用反向地理编码 getFromLocationName() 从位置名称获取 lat long 值

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

希望这有效

于 2013-10-21T08:49:24.773 回答