简体   繁体   English

地理编码代码无法正常工作 - Google地图

[英]Geocoding Code not Working - Googlemaps

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); 我使用下面的代码,但日志条目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);

    }

        }

Either you address is not in Google maps address database or your app does not have internet access privileges. 您的地址不在Google地图地址数据库中,或者您的应用没有互联网访问权限。

  1. Check in http://maps.google.com that the address is actually found. http://maps.google.com中检查实际找到的地址。

  2. That yor app has internet access privileges. 该应用程序具有Internet访问权限。 You must have this in ypur app manifest: 你必须在ypur应用程序清单中有这个:

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

There are definite issues with this in certain emulator API levels. 在某些仿真器API级别中存在明确的问题。 See Issue 8816: service not available . 请参见问题8816:服务不可用 For example API level 8 won't work. 例如,API级别8将不起作用。 I find that API level 7 is OK if you call the method twice. 如果您调用该方法两次,我发现API级别7是正常的。 The behaviour on real devices is not known to me. 我不知道真实设备上的行为。 I don't think Google guarantee that the service will always be available. 我认为Google不保证该服务始终可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM