Android 上的 GoogleMaps 使用 Google API 2.3.3 Level 10,按地址查找
我尝试使用 geoCoder 显示 EditText 中的地址,并且还可以通过单击查找器按钮并更新屏幕顶部的谷歌地图显示进行编辑,这是我的代码:
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();
}
}
});
}
但它此时崩溃了:列出地址列表= geocoder.getFromLocationName(locationName, 5); 我使用谷歌 APi 2.3.3。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论