使用Google API 2.3.3等级10的Android上的Google地图,按地址查找

时间:2011-12-14 21:23:30

标签: android google-maps map geocoding

我试图使用geoCoder从EditText显示地址,并且它可以编辑点击查找器按钮并更新屏幕顶部的googlemap显示,这是我的代码:

Geocoder geocoder = null;
MapView mapView = null;
int lat, lng;
EditText loc;

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

@Override
protected void onCreate(Bundle icicle) {

    super.onCreate(icicle);
    setContentView(R.layout.main);

    mapView = (MapView) findViewById(R.id.geoMap);

    try 
    {
        loc = (EditText)findViewById(R.id.location);
        String locationName = loc.getText().toString();
        List<Address> addressList = geocoder.getFromLocationName(locationName, 5);

        if (addressList != null && addressList.size() > 0) {
            lat = (int) addressList.get(0).getLatitude() * 1000000;
            lng = (int) addressList.get(0).getLongitude() * 1000000;
            GeoPoint pt = new GeoPoint(lat, lng);
            mapView.getController().setZoom(10);
            mapView.getController().setCenter(pt);
            mapView.getController().animateTo(pt);
        }
    } 
    catch 
    (IOException e) 
    {
        e.printStackTrace();
    }



    //
    Button geoBtn = (Button) findViewById(R.id.geocodeBtn);
    geocoder = new Geocoder(this);
    //
    geoBtn.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {
            try {
                EditText loc = (EditText) findViewById(R.id.location);
                String locationName = loc.getText().toString();
                List<Address> addressList = geocoder.getFromLocationName(locationName, 5);

                if (addressList != null && addressList.size() > 0) {
                    int lat = (int) addressList.get(0).getLatitude() * 1000000;
                    int lng = (int) addressList.get(0).getLongitude() * 1000000;
                    GeoPoint pt = new GeoPoint(lat, lng);
                    mapView.getController().setZoom(10);
                    mapView.getController().setCenter(pt);
                    mapView.getController().animateTo(pt);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
}

但此时它崩溃了:List addressList = geocoder.getFromLocationName(locationName,5); 我正在使用谷歌APi 2.3.3。

0 个答案:

没有答案