我知道这个问题已被多次询问,但我找不到解决方案:无论我尝试什么,我都无法从给定地址检索LatLng。你能看看我的代码,以指导我并找到解决方案吗?我在这里变得疯狂......
以下是代码:
public LatLng getCoordinates(String address){
Geocoder geocoder = new Geocoder(mContext);
List<Address> addresses;
double latitude=0;
double longitude=0;
LatLng coordinates;
try {
if(geocoder.isPresent()){
System.out.println("Geocoder present!");
//Geocoder is actually present
}
else
System.out.println("Geocoder NOT present!");
addresses = geocoder.getFromLocationName(address, 1);
/* I tried this too, but it didn't work:
int count=0;
addresses = geocoder.getFromLocationName(address, 10);
while(addresses.size()==0 && count<10){
addresses = geocoder.getFromLocationName(address, count);
count++;
}
*/
//This instruction throws the exception
System.out.println("addresses variable = "+addresses.get(0).toString());
System.out.println("addresses size="+addresses.size());
if(addresses.size() > 0) {
latitude= addresses.get(0).getLatitude();
longitude= addresses.get(0).getLongitude();
// latitude= addresses.get(count).getLatitude();
// longitude= addresses.get(count).getLongitude();
}
}catch (Exception e){
e.printStackTrace();
}
coordinates = new LatLng(latitude,longitude);
return coordinates;
}
这是printStackTrace结果:
02-08 17:33:50.922 9152-9152/com.clb.apokalos.todolistas W/System.err: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
02-08 17:33:50.922 9152-9152/com.clb.apokalos.todolistas W/System.err: at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
02-08 17:33:50.932 9152-9152/com.clb.apokalos.todolistas W/System.err: at java.util.ArrayList.get(ArrayList.java:304)
02-08 17:33:50.932 9152-9152/com.clb.apokalos.todolistas W/System.err: at com.clb.apokalos.todolistas.GPSTracker.getCoordinates(GPSTracker.java:178)
02-08 17:33:50.932 9152-9152/com.clb.apokalos.todolistas W/System.err: at com.clb.apokalos.todolistas.GeofenceMainActivity.populateGeofenceList(GeofenceMainActivity.java:365)
02-08 17:33:50.942 9152-9152/com.clb.apokalos.todolistas W/System.err: at com.clb.apokalos.todolistas.GeofenceMainActivity.onCreate(GeofenceMainActivity.java:100)
02-08 17:33:50.942 9152-9152/com.clb.apokalos.todolistas W/System.err: at android.app.Activity.performCreate(Activity.java:5024)
02-08 17:33:50.942 9152-9152/com.clb.apokalos.todolistas W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
02-08 17:33:50.952 9152-9152/com.clb.apokalos.todolistas W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2042)
02-08 17:33:50.952 9152-9152/com.clb.apokalos.todolistas W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2103)
02-08 17:33:50.952 9152-9152/com.clb.apokalos.todolistas W/System.err: at android.app.ActivityThread.access$600(ActivityThread.java:137)
02-08 17:33:50.952 9152-9152/com.clb.apokalos.todolistas W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1211)
02-08 17:33:50.962 9152-9152/com.clb.apokalos.todolistas W/System.err: at android.os.Handler.dispatchMessage(Handler.java:99)
02-08 17:33:50.972 9152-9152/com.clb.apokalos.todolistas W/System.err: at android.os.Looper.loop(Looper.java:137)
02-08 17:33:50.972 9152-9152/com.clb.apokalos.todolistas W/System.err: at android.app.ActivityThread.main(ActivityThread.java:4842)
02-08 17:33:50.972 9152-9152/com.clb.apokalos.todolistas W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
02-08 17:33:50.972 9152-9152/com.clb.apokalos.todolistas W/System.err: at java.lang.reflect.Method.invoke(Method.java:511)
02-08 17:33:50.972 9152-9152/com.clb.apokalos.todolistas W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
02-08 17:33:50.982 9152-9152/com.clb.apokalos.todolistas W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
02-08 17:33:50.982 9152-9152/com.clb.apokalos.todolistas W/System.err: at dalvik.system.NativeStart.main(Native Method)
谢谢大家!
编辑(已解决):发现问题!我以“ ShopName,地址,城市”的形式提供地址(我使用的信息从查询门户到谷歌)并且它不起作用,所以我只是简单地在“地址,城市”的形式,现在它的作品!再次感谢!