地理编码代码不起作用 - Googlemaps

发布于 2024-12-04 10:08:35 字数 1143 浏览 3 评论 0原文

我正在尝试使用地理编码来获取地址,计算出经度和纬度,然后在地图上显示叠加层。

我正在使用下面的代码,但是日志条目 Log.e("Found",""+lat);永远不会触发,我不知道为什么。有人可以帮忙吗?

谢谢 !

private void showpins() throws IOException {

    Geocoder gc = new Geocoder(this);

    String Address = "Oxford Street, London";

    Log.e("Lat",""+Address);

        List<Address> foundAdresses = gc.getFromLocationName(Address, 5); //Search addresses

        int lat;
        int lon;

        for (int i = 0; i < foundAdresses.size(); ++i) {

            Address x = foundAdresses.get(i);
            lat = (int) x.getLatitude();
            lon = (int) x.getLongitude();

            Log.e("Found",""+lat);

    List<Overlay> mapOverlays = mapView.getOverlays();
    Drawable drawable = this.getResources().getDrawable(R.drawable.pushpin);
    CustomizedItemOverlay itemizedOverlay = 
    new CustomizedItemOverlay(drawable, this);

    GeoPoint point = new GeoPoint(lat, lon);
    OverlayItem overlayitem = 
         new OverlayItem(point, "Hello", "Location");

    itemizedOverlay.addOverlay(overlayitem);
    mapOverlays.add(itemizedOverlay);

    }

        }

I am trying to use geocoding to take an address, work out the longitude and latitude and then display an overlay on the map.

I am using the below code, however the log entry Log.e("Found",""+lat); never triggers and I'm not sure why. Can anybody help ?

Thanks !

private void showpins() throws IOException {

    Geocoder gc = new Geocoder(this);

    String Address = "Oxford Street, London";

    Log.e("Lat",""+Address);

        List<Address> foundAdresses = gc.getFromLocationName(Address, 5); //Search addresses

        int lat;
        int lon;

        for (int i = 0; i < foundAdresses.size(); ++i) {

            Address x = foundAdresses.get(i);
            lat = (int) x.getLatitude();
            lon = (int) x.getLongitude();

            Log.e("Found",""+lat);

    List<Overlay> mapOverlays = mapView.getOverlays();
    Drawable drawable = this.getResources().getDrawable(R.drawable.pushpin);
    CustomizedItemOverlay itemizedOverlay = 
    new CustomizedItemOverlay(drawable, this);

    GeoPoint point = new GeoPoint(lat, lon);
    OverlayItem overlayitem = 
         new OverlayItem(point, "Hello", "Location");

    itemizedOverlay.addOverlay(overlayitem);
    mapOverlays.add(itemizedOverlay);

    }

        }

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

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

发布评论

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

评论(2

耀眼的星火 2024-12-11 10:08:35

在某些模拟器 API 级别中,这确实存在问题。请参阅问题 8816:服务不可用。例如,API 级别 8 将不起作用。我发现如果调用该方法两次,API 级别 7 就可以了。我不知道真实设备上的行为。我不认为 Google 保证该服务将始终可用。

There are definite issues with this in certain emulator API levels. See Issue 8816: service not available. For example API level 8 won't work. I find that API level 7 is OK if you call the method twice. The behaviour on real devices is not known to me. I don't think Google guarantee that the service will always be available.

过潦 2024-12-11 10:08:35

您的地址不在Google Maps地址数据库中,或者您的应用程序没有Internet访问权限。

  1. http://maps.google.com 实际上找到了地址。

  2. 您的应用程序具有Internet访问权限。您必须在YPUR应用程序清单中使用此内容:

     &lt; use-permission android:name =“ android.permission.internet” /&gt;
     

Either you address is not in Google maps address database or your app does not have internet access privileges.

  1. Check in http://maps.google.com that the address is actually found.

  2. That yor app has internet access privileges. You must have this in ypur app manifest:

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