问题描述:

Im trying to display the address from an EditText using geoCoder and also it would be able editable to click on the finder button and update the googlemap display on the top of the screen, this is my code:

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

}

}

});

}

But it crashes at this point: List addressList = geocoder.getFromLocationName(locationName, 5);

Im using googles APi 2.3.3.

相关阅读:
Top