我正在开发一个应用程序,该应用程序接受用户输入,然后将地图以用户输入的内容居中。我已经正确安装了谷歌地图 API。地图出现。我还确保模拟器是谷歌 API。我已经测试了我的代码,并且 geocoder.getFromLocationName 返回 null。我已经查找了这个问题并尝试了不同的解决方案,但都没有奏效。这是我的代码。
public class GetURL extends MapActivity {
Geocoder geocoder;
MapView mapView = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView)findViewById(R.id.geoMap);
mapView.setBuiltInZoomControls(true);
int lat = (int)(30.334954*1000000);
int lng = (int)(-81.5625*1000000);
GeoPoint pt = new GeoPoint(lat,lng);
mapView.getController().setZoom(10);
mapView.getController().setCenter(pt);
mapView.getController().animateTo(pt);
geocoder = new Geocoder(this);
}
public void first_location(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(15);
mapView.getController().setCenter(pt);
mapView.getController().animateTo(pt);
}
} catch (IOException e) {
e.printStackTrace();
}
}