如何在地图上从用户输入的地址中找到位置?

时间:2012-05-18 07:19:47

标签: android android-emulator google-maps-mobile

我想在使用虚拟设备(Emulator Android)时在地图上找到该位置,因为我没有实际的设备。 谢谢观看!

这是我的代码:

enter code herepublic class GoogleMapExample3Activity extends MapActivity {
/** Called when the activity is first created. */
MapView mapView;
TextView txtView;
LinearLayout zoomControl;
GeoPoint geoPoint;
MapController mapController;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mapView=(MapView) findViewById(R.id.mapView);
    zoomControl=(LinearLayout) findViewById(R.id.zoomControl);
    View view= mapView.getZoomControls();
    zoomControl.addView(view,new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    mapView.displayZoomControls(true);
    mapView.setSatellite(true);
    String address="New York";
    Geocoder geoCoder=new Geocoder(this);
    List<Address> list;
    try {
        list = geoCoder.getFromLocationName(address, 1);
        Address a=list.get(0);
        Double latitude=a.getLatitude();
        Double longitude=a.getLongitude();
        geoPoint=new GeoPoint( (int)(latitude*1E6),(int)(longitude*1E6));
        mapController=mapView.getController();
        mapController.animateTo(geoPoint);
        mapController.setZoom(18);

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

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

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    MapController mapController=mapView.getController();
    switch (keyCode) {
    case KeyEvent.KEYCODE_I:
        mapController.zoomIn();
        break;
    case KeyEvent.KEYCODE_O:
        mapController.zoomOut();
        break;
    default:
        break;
    }
    return super.onKeyDown(keyCode, event);
}

}

我尝试但无法从用户输入的地址获取位置! 我需要你的帮助!

2 个答案:

答案 0 :(得分:0)

How to find a location on Map from a address which was be input by user?

使用Android 地理编码器类,

Geocoder geoCoder = new Geocoder(this);
List<Address> listAddress;
GeoPoint geoPoint;
try {
    listAddress = geoCoder.getFromLocationName(userAddress,1);
    if (listAddress == null) {
        return null;
    }
    Address location = listAddress.get(0);
    geoPoint = new GeoPoint((int) (location.getLatitude() * 1E6),
                      (int) (location.getLongitude() * 1E6));

答案 1 :(得分:0)