从给定的地址获取经度和纬度

时间:2017-04-12 13:35:16

标签: android

我尝试了这种编码 -

      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);
        location.getLatitude();
        location.getLongitude();

        p1 = new LatLng(location.getLatitude(), location.getLongitude() );

    } catch (IOException ex) {

        ex.printStackTrace();
    }

    return p1;
}

但是无法重新获得getLatitude和getLongitude的错误,strAddress我从程序中传递地址。 我启用了gradle:compile&#39; com.google.android.gms:play-services:8.4.0&#39; Android主要内容:                                                       
                  

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

1 个答案:

答案 0 :(得分:0)

您必须在清单文件中授予权限。 您可以根据自己的要求使用ACCESS_FINE_LOCATION或ACCESS_COARSE_LOCATION。

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

有关详细信息,请查看https://developer.android.com/guide/topics/location/strategies.html

相关问题