如何从地址中找到纬度和经度?

Kan*_*dha 105 android google-maps google-geocoder

我想在Google地图中显示地址的位置.

如何使用Google Maps API获取地址的纬度和经度?

ud_*_*_an 134

public GeoPoint getLocationFromAddress(String strAddress){

Geocoder coder = new Geocoder(this);
List<Address> address;
GeoPoint p1 = null;

try {
    address = coder.getFromLocationName(strAddress,5);
    if (address==null) {
       return null;
    }
    Address location=address.get(0);
    location.getLatitude();
    location.getLongitude();

    p1 = new GeoPoint((double) (location.getLatitude() * 1E6),
                      (double) (location.getLongitude() * 1E6));

    return p1;
    }
}
Run Code Online (Sandbox Code Playgroud)

strAddress是包含地址的字符串.该address变量保存转换后的地址.

  • 通过下面的@NayAneshGupte查看答案,我不认为新库中有一个`GeoPoint`类.而是使用`LatLng`.http://stackoverflow.com/a/27834110/2968401 (6认同)
  • 您需要正确的权限才能访问该服务.#<uses-permission android:name ="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name ="android.permission.INTERNET"/> (3认同)

Nay*_*pte 73

Ud_an的解决方案包含更新的API

注意:LatLng类是Google Play服务的一部分.

强制性的:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

<uses-permission android:name="android.permission.INTERNET"/>
Run Code Online (Sandbox Code Playgroud)

更新:如果您有目标SDK 23及更高版本,请确保您负责位置的运行时权限.

public LatLng getLocationFromAddress(Context context,String strAddress) {

    Geocoder coder = new Geocoder(context);
    List<Address> address;
    LatLng p1 = null;

    try {
        // May throw an IOException
        address = coder.getFromLocationName(strAddress, 5);
        if (address == null) {
            return null;
        }

        Address location = address.get(0);
        p1 = new LatLng(location.getLatitude(), location.getLongitude() );

    } catch (IOException ex) {

        ex.printStackTrace();
    }

    return p1;
}
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,它对我有用,上述解决方案不起作用。 (2认同)
  • 美观整洁的解决方案.没有一个答案提到Geocoder使用同步访问,因此强烈建议将其放在后台服务中以避免阻止UI. (2认同)

Nir*_*ngi 51

如果您想将地址放在谷歌地图中,​​那么可以轻松使用以下内容

Intent searchAddress = new  Intent(Intent.ACTION_VIEW,Uri.parse("geo:0,0?q="+address));
startActivity(searchAddress);
Run Code Online (Sandbox Code Playgroud)

要么

如果你需要从你的地址获取经纬度长然后使用谷歌将阿比以下

创建一个方法,返回带有HTTP调用响应的JSONObject,如下所示

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;
    }
Run Code Online (Sandbox Code Playgroud)

现在将JSONObject传递给getLatLong()方法,如下所示

public static boolean getLatLong(JSONObject jsonObject) {

        try {

            longitute = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
                .getJSONObject("geometry").getJSONObject("location")
                .getDouble("lng");

            latitude = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
                .getJSONObject("geometry").getJSONObject("location")
                .getDouble("lat");

        } catch (JSONException e) {
            return false;

        }

        return true;
    }
Run Code Online (Sandbox Code Playgroud)

我希望这对你和其他人有帮助.. !! 谢谢..!!


小智 6

以下代码适用于google apiv2:

public void convertAddress() {
    if (address != null && !address.isEmpty()) {
        try {
            List<Address> addressList = geoCoder.getFromLocationName(address, 1);
            if (addressList != null && addressList.size() > 0) {
                double lat = addressList.get(0).getLatitude();
                double lng = addressList.get(0).getLongitude();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } // end catch
    } // end if
} // end convertAddress
Run Code Online (Sandbox Code Playgroud)

其中address是要转换为LatLng的String(123 Testing Rd City State zip).