2017-04-12 86 views
1

我想這個編碼 -獲得緯度和經度從指定的地址

 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不能resloved,strAddress我從程序傳遞地址。 我的gradle啓用:編譯 'com.google.android.gms:發揮服務:8.4.0' 的Android mainfeat:

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

你需要添加位置運行時間許可,並在明顯太 – Lokesh

+1

[如何從Android上給出的地址,緯度和經度?(HTTP的可能重複:// stackoverflow.com/questions/6466369/how-to-get-latitude-longitude-from-given-address-on-android) – Simo

回答

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