1

我想在使用虚拟设备(模拟器 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);
}

}

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

4

2 回答 2

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

使用 Android Geocoder类,

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));
于 2012-05-18T07:25:21.390 回答
0

希望对你有帮助

http://eagle.phys.utk.edu/guidry/android/mappingDemo.html

于 2012-05-18T07:27:54.233 回答