質問

MylocationOverlayが配信する経度と緯度から現在の郵便番号を取得し、これを私のアクティビティの編集に設定しようとします。

MylocationOverlayから経度と緯度を取得しようとすると、活動はcrash落します。

このコードの何が問題になっていますか?

よろしく、フロート

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を使用している場合、例外を取得している場合:

java.io.ioexception:サービスは利用できません

それは既知のバグです、参照してください サービスは利用できません

ただし、実際のデバイスとエミュレータレベル7では問題なく動作します。 (おそらく、アドレスにトラップを付ける必要がありますが、これはジオコーダーが機能することはありません!)

他のヒント

ほとんどの場合、場所は返還されています

myLocationOverlay.getMyLocation()

また

Location location = locationManager.getLastKnownLocation(provider);

無効です。おそらく、アプリケーションがまだロケーションの修正を受けておらず、以前に保存された場所がないためです。

フィックスを受け取った後に実行される実行可能にジオコーディングを実行するコードブロックを移動してみてください。このような:

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) 
            {

            }
        }
    }
});
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top