gpt4 book ai didi

android - 我们如何从android中的地址找到lat long?

转载 作者:行者123 更新时间:2023-11-30 04:50:29 26 4
gpt4 key购买 nike

我有一个地址,想找到该地址的纬度和经度...任何人都知道我们如何从 android 中的地址获取经纬度...

我正在使用这个代码

Geocoder coder = new Geocoder(getApplicationContext(), Locale
.getDefault());

List<Address> address;

address = coder.getFromLocationName(
"Pallekele International Cricket Stadium,Kandy, 1);
if (address == null) {

}
Address location = address.get(0);
location.getLatitude();
location.getLongitude();

GeoPoint point = new GeoPoint((int) (location.getLatitude() * 1E6),
(int) (location.getLongitude() * 1E6));

但是我遇到了这个异常

09-02 17:46:56.086: ERROR/Exception(1724): Service not Available
09-02 17:46:56.086: ERROR/Exception(1724): java.io.IOException: Service not Available
09-02 17:46:56.086: ERROR/Exception(1724): at android.location.Geocoder.getFromLocationName(Geocoder.java:159)
09-02 17:46:56.086: ERROR/Exception(1724): at pack.sample.map.SampleMapApplication.onCreate(SampleMapApplication.java:38)
09-02 17:46:56.086: ERROR/Exception(1724): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-02 17:46:56.086: ERROR/Exception(1724): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-02 17:46:56.086: ERROR/Exception(1724): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-02 17:46:56.086: ERROR/Exception(1724): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-02 17:46:56.086: ERROR/Exception(1724): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-02 17:46:56.086: ERROR/Exception(1724): at android.os.Handler.dispatchMessage(Handler.java:99)
09-02 17:46:56.086: ERROR/Exception(1724): at android.os.Looper.loop(Looper.java:123)
09-02 17:46:56.086: ERROR/Exception(1724): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-02 17:46:56.086: ERROR/Exception(1724): at java.lang.reflect.Method.invokeNative(Native Method)
09-02 17:46:56.086: ERROR/Exception(1724): at java.lang.reflect.Method.invoke(Method.java:521)
09-02 17:46:56.086: ERROR/Exception(1724): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-02 17:46:56.086: ERROR/Exception(1724): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-02 17:46:56.086: ERROR/Exception(1724): at dalvik.system.NativeStart.main(Native Method)

我做错了什么?

最佳答案

您收到服务不可用错误。您需要一个实际的设备,而不是模拟器。 GeoCoder documentation说:

The Geocoder class requires a backend service that is not included in the core android framework. The Geocoder query methods will return an empty list if there no backend service in the platform.

issue page说:

Are you guys using this code on the Android 2.2 emulator? I'm also getting this error on the emulator but it works on the actual device (nexus one - android 2.2)

Just installed CyanogenMod 6 alfa and now GeoCoder works fine. I guess it will work on final builds of stock Froyo too.

关于android - 我们如何从android中的地址找到lat long?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3627030/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com