如何从Android上的位置获取实际地址

时间:2011-10-23 02:23:35

标签: android

我想获取位置地址,我试过了:

package com.spot.prototype;

import android.os.Bundle;
import android.view.KeyEvent;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;

import android.content.Context;
import android.view.MotionEvent;
import android.widget.Toast;

import android.location.Geocoder;
import android.location.Address;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

public class SpotActivity extends MapActivity {
    MapView mapView;
    MapController mc;
    GeoPoint p;

    private LocationManager lm;
    private LocationListener locationListener;

    @Override
    public void onCreate( Bundle savedInstanceState ) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.main );
        mapView = ( MapView ) findViewById( R.id.mapView );
        mapView.setBuiltInZoomControls( true );
        // mapView.setSatellite(true);
        // mapView.setStreetView(true);
        mc = mapView.getController();
        String coordinates[] = { "1.352566007", "103.78921587" };
        double lat = Double.parseDouble( coordinates[0] );
        double lng = Double.parseDouble( coordinates[1] );
        p = new GeoPoint( ( int ) ( lat * 1E6 ), ( int ) ( lng * 1E6 ) );
        mc.animateTo( p );
        mc.setZoom( 13 );

        lm = ( LocationManager ) getSystemService( Context.LOCATION_SERVICE );
        locationListener = new MyLocationListener();
        lm.requestLocationUpdates( LocationManager.GPS_PROVIDER, 200, 10, locationListener );
    }

    public boolean onKeyDown( int keyCode, KeyEvent event ) {
        MapController mc = mapView.getController();
        switch( keyCode ) {
        case KeyEvent.KEYCODE_3 :
            mc.zoomIn();
            break;
        case KeyEvent.KEYCODE_1 :
            mc.zoomOut();
            break;
        }
        return super.onKeyDown( keyCode, event );
    }



    private class MyLocationListener implements LocationListener {
        @Override
        public void onLocationChanged( Location loc ) {
            if ( loc != null ) {
                Geocoder geoCoder = new Geocoder( getBaseContext(), Locale.getDefault() );
                try {
                    List<Address> addresses = geoCoder.getFromLocation( loc.getLatitude(), loc.getLongitude(), 1 );
                    System.out.println( loc.getLatitude() );
                    System.out.println( loc.getLongitude() );
                    String add = "";
                    if ( addresses.size() > 0 ) {
                        for( int i = 0; i < addresses.get( 0 ) .getMaxAddressLineIndex(); ++i ) {
                            add += addresses.get( 0 ).getAddressLine( i ) + "\n";
                        }
                    }

                    Toast.makeText( getBaseContext(), add, Toast.LENGTH_SHORT ) .show();

                }  
                catch( IOException e ) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        public void onProviderDisabled( String provider ) {
        }

        @Override
        public void onProviderEnabled( String provider ) {
        }

        @Override
        public void onStatusChanged( String provider, int status, Bundle extras ) {
        }
    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }
}

在向我的模拟器发送信号时,我看到位置实际上已更新。但是,地址列表始终为null。谁能让我对此有所了解?提前谢谢。

1 个答案:

答案 0 :(得分:1)

来自getFromLocation()的文档:

  

返回Address对象列表。如果未找到匹配项或没有可用的后端服务,则返回null或空列表。

因此,您提供的位置实际上并不对应于真实地址,或者为了获取地址而需要运行的后端服务未运行。

同样来自geocoder documentation

  

Geocoder类需要一个未包含在核心android框架中的后端服务。如果平台中没有后端服务,则Geocoder查询方法将返回空列表。使用isPresent()方法确定是否存在Geocoder实现。

您是否尝试过调用isPresent方法来查看后端是否正常运行?