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

Android 如何从用户输入的地址中找到地图上的位置?,android,android-emulator,google-maps-mobile,Android,Android Emulator,Google Maps Mobile,我想在使用虚拟设备(仿真器Android)时在地图上找到位置,因为我没有实际的设备。 谢谢收看 这是我的密码: enter code herepublic class GoogleMapExample3Activity extends MapActivity { /** Called when the activity is first created. */ MapView mapView; TextView txtView; LinearLayout zoomControl; GeoPoin

我想在使用虚拟设备(仿真器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);
}
在此输入代码公共类GoogleMapExample3Activity扩展了MapActivity{
/**在首次创建活动时调用*/
地图视图;
TextView-txtView;
线性布局动物控制;
地质点;
地图控制器;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView=(mapView)findViewById(R.id.mapView);
zoomControl=(LinearLayout)findViewById(R.id.zoomControl);
View=mapView.GetZoomControl();
添加视图(视图,新的LayoutParams(LayoutParams.WRAP_内容,LayoutParams.WRAP_内容));
mapView.DisplayZoomControl(真);
mapView.setSatellite(真);
字符串地址=“纽约”;
Geocoder Geocoder=新的Geocoder(本);
名单;
试一试{
list=geoCoder.getFromLocationName(地址,1);
地址a=list.get(0);
双纬度=a.getLatitude();
双经度=a.getLongitude();
地质点=新的地质点((int)(纬度*1E6),(int)(经度*1E6));
mapController=mapView.getController();
mapController.animateTo(地质点);
mapController.setZoom(18);
}捕获(例外e){
e、 printStackTrace();
}
}
@凌驾
受保护的布尔值isRouteDisplayed(){
返回false;
}
@凌驾
公共布尔onKeyDown(int-keyCode,KeyEvent事件){
MapController MapController=mapView.getController();
开关(钥匙代码){
案例KeyEvent.KEYCODE_I:
mapController.zoomIn();
打破
case KeyEvent.KEYCODE_O:
mapController.zoomOut();
打破
违约:
打破
}
返回super.onKeyDown(keyCode,event);
}
}

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

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

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));
Geocoder-Geocoder=新的地理编码器(本);
列表地址;
地质点;
试一试{
listAddress=geoCoder.getFromLocationName(用户地址,1);
if(listAddress==null){
返回null;
}
地址位置=listAddress.get(0);
地质点=新的地质点((int)(location.getLatitude()*1E6),
(int)(location.getLongitude()*1E6));

希望这对您有所帮助


我也是,但getFromLocationName(“用户输入地址”,1)方法不起作用!Log cat输出:服务不可用:(我尝试了,但没有!调试收到消息:“java.io.IOException:服务不可用”我不能发布我的代码,因为我没有足够的声誉,你能发布你的代码给我参考吗?你的代码在我的情况下运行良好…谷歌API 2.2..在清单文件中有你的加载项应用程序标记。另外?我的项目不包含你在代码中提到的任何内容。甚至类名都一样..只需添加你的Android Manife我没有使用getfromlocationnam方法,我使用parse json来获取用户输入的位置!