ユーザー入力を受け取り、ユーザーが入力した内容をマップの中心に置くアプリに取り組んでいます。Google Maps API を適切にインストールしました。マップが表示されます。また、エミュレーターが google 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();
}
}