簡體   English   中英

如何從用戶輸入的地址中查找地圖上的位置?

[英]How to find a location on Map from a address which has been input by user?

我想在使用虛擬設備(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);
}

}

我嘗試過,但是無法從用戶輸入的地址獲得位置! 我需要你的幫助!

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM