Android 上的 GoogleMaps 使用 Google API 2.3.3 Level 10,按地址查找

发布于 2024-12-21 09:31:17 字数 2283 浏览 3 评论 0原文

我尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文