java - Android:无法从 Google Geocoding API 获取位置名称

标签 java android google-maps google-geocoder

我正在尝试使用谷歌反向地理编码 API 从纬度和经度获取位置名称。我收到了错误。

这是我正在使用的代码:

  Geocoder geocoder= new Geocoder(MainActivity.this, Locale.ENGLISH);
         
        try {
               
              //Place your latitude and longitude
              List<Address> addresses = geocoder.getFromLocation(37.423247,-122.085469, 1);
              
              TextView myAddress=(TextView)findViewById(R.id.textView1);
				if(addresses != null) {
               
                  Address fetchedAddress = addresses.get(0);
                  StringBuilder strAddress = new StringBuilder();
                
                  for(int i=0; i<fetchedAddress.getMaxAddressLineIndex(); i++) {
                        strAddress.append(fetchedAddress.getAddressLine(i)).append("\n");
                  }
                
                  myAddress.setText("I am at: " +strAddress.toString());
               
              }
               
              else
                  myAddress.setText("No location found..!");
          
        } 
        catch (IOException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
                 Toast.makeText(MainActivity.this,"Could not get address..!", Toast.LENGTH_LONG).show();
        }

日志文件

 FATAL EXCEPTION: main
     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.get_mobiile_location/com.example.get_mobiile_location.MainActivity}: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
     	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
     	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
     	at android.app.ActivityThread.access$600(ActivityThread.java:141)
     	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
     	at android.os.Handler.dispatchMessage(Handler.java:99)
     	at android.os.Looper.loop(Looper.java:137)
     	at android.app.ActivityThread.main(ActivityThread.java:5103)
     	at java.lang.reflect.Method.invokeNative(Native Method)
     	at java.lang.reflect.Method.invoke(Method.java:525)
     	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
     	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
     	at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
     	at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
     	at java.util.ArrayList.get(ArrayList.java:308)
     	at com.example.get_mobiile_location.MainActivity.getMyLocation(MainActivity.java:79)
     	at com.example.get_mobiile_location.MainActivity.onCreate(MainActivity.java:53)
     	at android.app.Activity.performCreate(Activity.java:5133)
     	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
     	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
     	... 11 more

请检查我做错了什么。

最佳答案

基于日志

java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0

在下一行中,您将获得给定经纬度值的 0 个地址

List<Address> addresses = geocoder.getFromLocation(37.423247,-122.085469, 1);

因此,为了避免崩溃,请按如下方式在从中访问数据之前检查条件

if(addresses != null&&addresses.size()>0) {
  //fetch data from addresses
}else{
        //display Toast message
     }

更新:

如果您忘记了任何人,请添加以下权限

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<permission android:name="com.example.permission.MAPS_RECEIVE" android:protectionLevel="signature"/>
<uses-permission android:name="android.permission.INTERNET" />

引用以下教程

http://karanbalkar.com/2013/11/tutorial-63-implement-reverse-geocoding-in-android/

希望对您有所帮助。

关于java - Android:无法从 Google Geocoding API 获取位置名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30883983/

相关文章:

java - 在 GWT 中将集合转换到客户端

java - guice 的 TypeLiteral 是如何工作的?

Android 应用程序无法使用 ic_menu_refresh 进行编译

javascript - gmaps.js 添加DrawingManager控件

android - ItemizedOverlay 问题(HelloMapView 教程)

java - 我可以合理地访问扩展类的变量吗?

android - Android中的标签页切换期间ZXing QR码问题

android - 我找不到标签 fragment

javascript - 使用 divi builders map 模块时 Google Maps Javascript API 请求被拒绝

java - Android - 我无法从 SQLite 数据库更新数据