【问题标题】:Get the address of a location获取某个位置的地址
【发布时间】:2012-04-01 07:38:47
【问题描述】:

我正在制作一个 Google 地图应用。当我点击我的屏幕时,我想获取一个点的地址,但是当我点击屏幕时,它没有运行,没有任何反应。我该如何解决这个问题?

这是我的代码:

package app.googlemap;

public class GoogleMapActivity extends MapActivity {    
    MapView mapView;
    View zoomView;
    MapController mc;
    GeoPoint p;
    private List<Overlay> mapOverlays;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);      
        mapView=(MapView)findViewById(R.id.simple_map);
        LinearLayout zoomLayout=(LinearLayout)findViewById(R.id.zoom);
        zoomView = mapView.getZoomControls();
        zoomLayout.addView(zoomView, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        mapView.displayZoomControls(true);

        mapView.setTraffic(true);
        String coordinates[]={"21.036074","105.833636"};
        double latiTude=Double.parseDouble(coordinates[0]);
        double longiTude=Double.parseDouble(coordinates[1]);
        p=new GeoPoint((int)(latiTude*1E6), (int)(longiTude*1e6));

        MapOverlay mapOverlay = new MapOverlay();
        List<Overlay> listOfOverlays = mapView.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay); 

        mc=mapView.getController();
        mc.animateTo(p);
        mc.setZoom(17);
        mapView.invalidate();
    }

    public void draw(GeoPoint g,int a ){
        mapOverlays = mapView.getOverlays();
        Drawable drawable = this.getResources().getDrawable(a);
        HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable);
        OverlayItem overlayitem = new OverlayItem(p, "", "");
        itemizedoverlay.addOverlay(overlayitem);
        mapOverlays.add(itemizedoverlay); 
    }

    public class MapOverlay extends com.google.android.maps.Overlay{
        private Bitmap bmp;

        public boolean draw(Canvas canvas, MapView mapView, boolean shadow ,long when){
            super.draw(canvas, mapView, shadow);
            Point screenPts = new Point();
            mapView.getProjection().toPixels(p, screenPts);
            bmp=BitmapFactory.decodeResource(getResources(), R.drawable.t5);
            canvas.drawBitmap(bmp, screenPts.x,screenPts.y-50, null);
            return true; 
        }

        @Override
        public boolean onTouchEvent(MotionEvent event, MapView mapView) 
        {   
            if (event.getAction() == 1) {                
                GeoPoint p = mapView.getProjection().fromPixels(
                        (int) event.getX(),
                        (int) event.getY());

                Geocoder geoCoder = new Geocoder(
                        getBaseContext(), Locale.getDefault());
                try {
                    List<Address> addresses = geoCoder.getFromLocation(
                            p.getLatitudeE6()  / 1E6, 
                            p.getLongitudeE6() / 1E6, 1);

                    String add = "";
                    if (addresses.size() > 0) 
                    {
                        for (int i=0; i<addresses.get(0).getMaxAddressLineIndex(); i++)
                            add += addresses.get(0).getAddressLine(i) + "\n";
                    }

                    Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show();
                }
                catch (IOException e) {                
                    e.printStackTrace();
                }   
                return true;
            }
            else                                 
                return false;
        }        

    }

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }
}

【问题讨论】:

  • 而不是 onTouchEvent() 尝试覆盖 onTap() 事件....查看该方法是否有效...

标签: android google-maps map google-maps-api-3 street-address


【解决方案1】:

调用此方法onTap of Map。

/** * 从特定纬度和经度获取地址的方法。 * */ 公共静态字符串 getAddressFromLocation(双纬度, 双经度,上下文上下文){

    String locationAddress = "";

    Geocoder geocoder = new Geocoder(context, Locale.getDefault());
    try {

        List<Address> addresses = geocoder.getFromLocation(latitude,
                longitude, 1);

        if (addresses != null && !addresses.isEmpty()) {
            Address returnedAddress = addresses.get(0);
            StringBuilder strReturnedAddress = new StringBuilder(
                    "Address:\n");
            for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) {

                strReturnedAddress
                        .append(returnedAddress.getAddressLine(i)).append(
                                "\n");
            }
            locationAddress = strReturnedAddress.toString();

            locationAddress = locationAddress.replace("Address:", "");
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return locationAddress;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-13
    • 2011-01-06
    • 1970-01-01
    • 2012-04-29
    • 1970-01-01
    • 2021-11-17
    • 2017-10-21
    • 2012-02-26
    相关资源
    最近更新 更多