使用地理编码与myLocationOverlay



我尝试从经度和纬度得到当前的邮政编码,MyLocationOverlay提供并将其设置为我的活动上的EditView。

当我试图从MyLocationOverlay获取经度和纬度时,活动崩溃了。

这段代码有什么问题?

问候,浮动

LogCat输出:http://codepaste.net/vs6itk第59行是以下行:

 double currentLatitude = myLocationOverlay.getMyLocation().getLatitudeE6(); 

下面是我的代码:

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.partner_suche);
    final EditText tView = (EditText) findViewById(R.id.editTextPLZ);
    final MapView mView = (MapView) findViewById(R.id.mapview);
    mView.getController().setZoom(14);
    List<Overlay> mapOverlays = mView.getOverlays();
    myLocationOverlay = new MyCustomLocationOverlay(this, mView);
    mapOverlays.add(myLocationOverlay);
    myLocationOverlay.enableMyLocation();
    myLocationOverlay.enableCompass();
    myLocationOverlay.runOnFirstFix(new Runnable() {
        public void run() {
            mView.getController().animateTo(myLocationOverlay.getMyLocation());
        }
    });
    Geocoder gc = new Geocoder(this, Locale.getDefault());
    double currentLatitude = myLocationOverlay.getMyLocation().getLatitudeE6();
    double currentLongitute = myLocationOverlay.getMyLocation().getLongitudeE6();
    try 
    {
        List<Address> addresses = gc.getFromLocation(currentLatitude, currentLongitute, 1);
        if (addresses.size() > 0) 
        {
            tView.setText(addresses.get(0).getLocality());
        }
    } catch (IOException e) 
    {
    }
}

编辑:我创建了一个LocationListener来获取我的当前位置。现在我试图运行gc的部分崩溃了。getFromLocation(纬度,经度,1);我读不懂异常?:/

LocationManager locationManager;
    String context = Context.LOCATION_SERVICE;
    locationManager = (LocationManager)getSystemService(context);
    String provider = LocationManager.GPS_PROVIDER;
    LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            updateWithNewLocation(location);
            } 
        public void onProviderDisabled(String provider){
            updateWithNewLocation(null);
            }
        public void onProviderEnabled(String provider){ }
        public void onStatusChanged(String provider, int status,Bundle extras){ }
        };
    locationManager.requestLocationUpdates(provider, 0, 0, locationListener);
private void updateWithNewLocation(Location location) {
    final EditText tView = (EditText) findViewById(R.id.editTextPLZ);
    double latitude = location.getLatitude();
    double longitude = location.getLongitude();
    Geocoder gc = new Geocoder(this, Locale.getDefault());
    if (location != null) {
        try 
        {
            List<Address> addresses = gc.getFromLocation(latitude, longitude, 1);
            if (addresses.size() > 0) 
            {
                Address address = addresses.get(0);
                for (int i = 0; i < address.getMaxAddressLineIndex(); i++){
                    tView.setText(address.getPostalCode());
                }
            }
        } catch (Exception e) 
        {
        }
    }
}

如果您正在使用模拟器API级别8或9,并获得异常:

. io .IOException: Service not Available

则是已知bug,见service not available

它在真实设备和模拟器级别7上工作正常。(您可能也应该在地址为空时设置陷阱,尽管这不会使地理编码器工作!)

最有可能的是从

返回的位置
myLocationOverlay.getMyLocation()

Location location = locationManager.getLastKnownLocation(provider);

是NULL。可能是由于您的应用程序尚未收到位置修复,并且没有先前保存的位置。

尝试将您在其中进行地理编码的代码块移动到收到修复后运行的可运行程序中。这样的:

myLocationOverlay.runOnFirstFix(new Runnable() {
    public void run() {
        Location loc = myLocationOverlay.getMyLocation();
        if (loc != null) {
            mView.getController().animateTo(loc);
            Geocoder gc = new Geocoder(this, Locale.getDefault());
            double currentLatitude = loc.getLatitudeE6();
            double currentLongitute = loc.getLongitudeE6();
            try 
            {
                List<Address> addresses = gc.getFromLocation(currentLatitude, currentLongitute, 1);
                if (addresses.size() > 0) 
                {
                    tView.setText(addresses.get(0).getLocality());
                }
            } catch (IOException e) 
            {
            }
        }
    }
});

相关内容

  • 没有找到相关文章

最新更新