安卓;地理编码器,为什么我收到“服务不可用”?

发布于 2024-09-17 05:59:13 字数 2753 浏览 5 评论 0 原文

我想在 Android 应用程序中使用地理编码器,我有以下代码片段对其进行采样:

public class LocatorGeo extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        Geocoder geo = new Geocoder(getApplicationContext());

        List<Address> myAddrs = new ArrayList<Address>();

        try {
            myAddrs = geo.getFromLocationName("CO100AR", 1);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

我得到以下堆栈跟踪:

09-01 15:52:38.809: WARN/System.err(334): java.io.IOException: Service not Available
09-01 15:52:38.819: WARN/System.err(334):     at android.location.Geocoder.getFromLocationName(Geocoder.java:159)
09-01 15:52:38.829: WARN/System.err(334):     at com.jameselsey.LocatorGeo.onCreate(LocatorGeo.java:25)
09-01 15:52:38.829: WARN/System.err(334):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-01 15:52:38.839: WARN/System.err(334):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-01 15:52:38.849: WARN/System.err(334):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-01 15:52:38.849: WARN/System.err(334):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-01 15:52:38.868: WARN/System.err(334):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-01 15:52:38.868: WARN/System.err(334):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-01 15:52:38.879: WARN/System.err(334):     at android.os.Looper.loop(Looper.java:123)
09-01 15:52:38.889: WARN/System.err(334):     at android.app.ActivityThread.main(ActivityThread.java:4627)
09-01 15:52:38.899: WARN/System.err(334):     at java.lang.reflect.Method.invokeNative(Native Method)
09-01 15:52:38.909: WARN/System.err(334):     at java.lang.reflect.Method.invoke(Method.java:521)
09-01 15:52:38.919: WARN/System.err(334):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-01 15:52:38.929: WARN/System.err(334):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-01 15:52:38.929: WARN/System.err(334):     at dalvik.system.NativeStart.main(Native Method)

为什么服务不可用?我的清单中有以下内容

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

文档 状态: Geocoder类需要一个未包含在核心android框架中的后端服务,我如何/在哪里可以获得这样的服务?

I want to use the Geocoder in an android application, I've got the following piece of code to sample it :

public class LocatorGeo extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        Geocoder geo = new Geocoder(getApplicationContext());

        List<Address> myAddrs = new ArrayList<Address>();

        try {
            myAddrs = geo.getFromLocationName("CO100AR", 1);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

I get the following stack trace :

09-01 15:52:38.809: WARN/System.err(334): java.io.IOException: Service not Available
09-01 15:52:38.819: WARN/System.err(334):     at android.location.Geocoder.getFromLocationName(Geocoder.java:159)
09-01 15:52:38.829: WARN/System.err(334):     at com.jameselsey.LocatorGeo.onCreate(LocatorGeo.java:25)
09-01 15:52:38.829: WARN/System.err(334):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-01 15:52:38.839: WARN/System.err(334):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-01 15:52:38.849: WARN/System.err(334):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-01 15:52:38.849: WARN/System.err(334):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-01 15:52:38.868: WARN/System.err(334):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-01 15:52:38.868: WARN/System.err(334):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-01 15:52:38.879: WARN/System.err(334):     at android.os.Looper.loop(Looper.java:123)
09-01 15:52:38.889: WARN/System.err(334):     at android.app.ActivityThread.main(ActivityThread.java:4627)
09-01 15:52:38.899: WARN/System.err(334):     at java.lang.reflect.Method.invokeNative(Native Method)
09-01 15:52:38.909: WARN/System.err(334):     at java.lang.reflect.Method.invoke(Method.java:521)
09-01 15:52:38.919: WARN/System.err(334):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-01 15:52:38.929: WARN/System.err(334):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-01 15:52:38.929: WARN/System.err(334):     at dalvik.system.NativeStart.main(Native Method)

Why is the service unavailable? I have the following in my manifest

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

The documentation states : The Geocoder class requires a backend service that is not included in the core android framework, how/where can I obtain such a service?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

遗弃M 2024-09-24 05:59:13

这是这个问题的唯一解决方案......

public static JSONObject getLocationInfo(String address) {

        HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?address=" +address+"&ka&sensor=false");
        HttpClient client = new DefaultHttpClient();
        HttpResponse response;
        StringBuilder stringBuilder = new StringBuilder();

        try {
            response = client.execute(httpGet);
            HttpEntity entity = response.getEntity();
            InputStream stream = entity.getContent();
            int b;
            while ((b = stream.read()) != -1) {
                stringBuilder.append((char) b);
            }
        } catch (ClientProtocolException e) {
        } catch (IOException e) {
        }

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject = new JSONObject(stringBuilder.toString());
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return jsonObject;
    }

    public static GeoPoint getGeoPoint(JSONObject jsonObject) {

        Double lon = new Double(0);
        Double lat = new Double(0);

        try {

            lon = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
                .getJSONObject("geometry").getJSONObject("location")
                .getDouble("lng");

            lat = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
                .getJSONObject("geometry").getJSONObject("location")
                .getDouble("lat");

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return new GeoPoint((int) (lat * 1E6), (int) (lon * 1E6));

    }


GeoPoint srcGeoPoint =getGeoPoint(getLocationInfo(fromAddress.replace("\n"," ").replace(" ", "%20")));
            GeoPoint destGeoPoint =getGeoPoint(getLocationInfo(CalDescription.toAddress.replace("\n"," ").replace(" ", "%20")));

This is only solution for this problem....

public static JSONObject getLocationInfo(String address) {

        HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?address=" +address+"&ka&sensor=false");
        HttpClient client = new DefaultHttpClient();
        HttpResponse response;
        StringBuilder stringBuilder = new StringBuilder();

        try {
            response = client.execute(httpGet);
            HttpEntity entity = response.getEntity();
            InputStream stream = entity.getContent();
            int b;
            while ((b = stream.read()) != -1) {
                stringBuilder.append((char) b);
            }
        } catch (ClientProtocolException e) {
        } catch (IOException e) {
        }

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject = new JSONObject(stringBuilder.toString());
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return jsonObject;
    }

    public static GeoPoint getGeoPoint(JSONObject jsonObject) {

        Double lon = new Double(0);
        Double lat = new Double(0);

        try {

            lon = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
                .getJSONObject("geometry").getJSONObject("location")
                .getDouble("lng");

            lat = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
                .getJSONObject("geometry").getJSONObject("location")
                .getDouble("lat");

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return new GeoPoint((int) (lat * 1E6), (int) (lon * 1E6));

    }


GeoPoint srcGeoPoint =getGeoPoint(getLocationInfo(fromAddress.replace("\n"," ").replace(" ", "%20")));
            GeoPoint destGeoPoint =getGeoPoint(getLocationInfo(CalDescription.toAddress.replace("\n"," ").replace(" ", "%20")));
好多鱼好多余 2024-09-24 05:59:13

我也遇到过此类问题,例如 geo.getFromLocationName 返回 null 或此类异常,因此找到更改网络解决方案是我使用 Google api 获取纬度/经度,这是 链接供参考.

I also have faced this kind of issues like geo.getFromLocationName return null or this kind of exception so find alter net solution is i have used Google api to get lat/long and here is the link for reference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文