谷歌地图MapOverlay问题

发布于 2024-12-06 06:24:07 字数 3330 浏览 0 评论 0原文

我的问题是我在特定位置上使用 PushPin 显示 Google 地图,但在我的应用程序中,如果用户可以单击图钉,位置名称将显示在 toast 或文本视图中,但我不知道该怎么做?

请帮我。

以下是我的代码:-

Map_Activity.java:-

public class Map_Activity extends MapActivity {

    String mArea;
    MapView mapView;
    MapController mc;
    GeoPoint p;
    String[] mLongitude,mLatitude,mAreaArray;
    double lat,lng;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.google_map_screen);

        Bundle bdl=getIntent().getExtras();
        mArea= bdl.getString("Area Name");

        mAreaArray=getResources().getStringArray(R.array.PlaceName);
        mLongitude=getResources().getStringArray(R.array.Longitude);
        mLatitude=getResources().getStringArray(R.array.Latitude);

        for(int i=0;i<mAreaArray.length;i++){
            if(mArea.equals(mAreaArray[i])){
                lat=Float.valueOf(mLatitude[i]);
                lng=Float.valueOf(mLongitude[i]);
            }
        }

        mapView = (MapView) findViewById(R.id.googlemap);
        LinearLayout zoomLayout = (LinearLayout) findViewById(R.id.zoom);
        @SuppressWarnings("deprecation")
        View zoomView = mapView.getZoomControls();

        zoomLayout.addView(zoomView, new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        mapView.displayZoomControls(true);

        mc = mapView.getController();

        p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));

        mc.animateTo(p);
        mc.setZoom(17);
        MapOverlay mapOverlay = new MapOverlay();
        List<Overlay> listOfOverlays = mapView.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);
        mapView.invalidate();

    }

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

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        MapController mc = mapView.getController();
        switch (keyCode) {
        case KeyEvent.KEYCODE_3:
            mc.zoomIn();
            break;
        case KeyEvent.KEYCODE_1:
            mc.zoomOut();
            break;
        }
        return super.onKeyDown(keyCode, event);
    }

    class MapOverlay extends com.google.android.maps.Overlay {
        @Override
        public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
                long when) {
            super.draw(canvas, mapView, shadow);

            // ---translate the GeoPoint to screen pixels---
            Point screenPts = new Point();
            mapView.getProjection().toPixels(p, screenPts);

            // ---add the marker---
            Bitmap bmp = BitmapFactory.decodeResource(getResources(),
                    R.drawable.pushpin);
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 50, null);
            return true;
        }

        @Override
        public boolean onTap(GeoPoint p, MapView mapView) {
            // TODO Auto-generated method stub
            Toast.makeText(Google_Map_Activity.this, "Sorry Don't Click", Toast.LENGTH_SHORT).show();
            return super.onTap(p, mapView);
        }
    }
}

My problem is I am displaying Google Map with PushPin on Particular Location but in my application if user can click on the pushpin the location name is display in toast or text-view, but i don't know how to do this?

Please Help me.

Following is my Code:-

Map_Activity.java:-

public class Map_Activity extends MapActivity {

    String mArea;
    MapView mapView;
    MapController mc;
    GeoPoint p;
    String[] mLongitude,mLatitude,mAreaArray;
    double lat,lng;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.google_map_screen);

        Bundle bdl=getIntent().getExtras();
        mArea= bdl.getString("Area Name");

        mAreaArray=getResources().getStringArray(R.array.PlaceName);
        mLongitude=getResources().getStringArray(R.array.Longitude);
        mLatitude=getResources().getStringArray(R.array.Latitude);

        for(int i=0;i<mAreaArray.length;i++){
            if(mArea.equals(mAreaArray[i])){
                lat=Float.valueOf(mLatitude[i]);
                lng=Float.valueOf(mLongitude[i]);
            }
        }

        mapView = (MapView) findViewById(R.id.googlemap);
        LinearLayout zoomLayout = (LinearLayout) findViewById(R.id.zoom);
        @SuppressWarnings("deprecation")
        View zoomView = mapView.getZoomControls();

        zoomLayout.addView(zoomView, new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        mapView.displayZoomControls(true);

        mc = mapView.getController();

        p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));

        mc.animateTo(p);
        mc.setZoom(17);
        MapOverlay mapOverlay = new MapOverlay();
        List<Overlay> listOfOverlays = mapView.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);
        mapView.invalidate();

    }

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

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        MapController mc = mapView.getController();
        switch (keyCode) {
        case KeyEvent.KEYCODE_3:
            mc.zoomIn();
            break;
        case KeyEvent.KEYCODE_1:
            mc.zoomOut();
            break;
        }
        return super.onKeyDown(keyCode, event);
    }

    class MapOverlay extends com.google.android.maps.Overlay {
        @Override
        public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
                long when) {
            super.draw(canvas, mapView, shadow);

            // ---translate the GeoPoint to screen pixels---
            Point screenPts = new Point();
            mapView.getProjection().toPixels(p, screenPts);

            // ---add the marker---
            Bitmap bmp = BitmapFactory.decodeResource(getResources(),
                    R.drawable.pushpin);
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 50, null);
            return true;
        }

        @Override
        public boolean onTap(GeoPoint p, MapView mapView) {
            // TODO Auto-generated method stub
            Toast.makeText(Google_Map_Activity.this, "Sorry Don't Click", Toast.LENGTH_SHORT).show();
            return super.onTap(p, mapView);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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