Android 地图,在该点上有一个文本视图并带有一个按钮

发布于 2024-12-15 00:38:24 字数 1430 浏览 2 评论 0原文

在我的应用程序中,我想显示一个带有指针的地图,在指针上方我想显示带有按钮的文本视图。使用该按钮我想转到下一个活动。以下是我显示该点的代码,但如何显示带有文本和右侧按钮的点。以及如何为其编写 btn 操作

class MapOverlay extends com.google.android.maps.Overlay
 {
    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);
        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.marker);           
        canvas.drawBitmap(bmp, screenPts.x, screenPts.y-100, null);        

        mapView.getProjection().toPixels(q, screenPts);
        @SuppressWarnings("unused")
        Bitmap bmp1 = BitmapFactory.decodeResource(getResources(), R.drawable.blue);           
        canvas.drawBitmap(bmp, screenPts.x, screenPts.y-100, null);        
        return true;
    }
 }



p = new GeoPoint((int) (latPt * 1E6),(int) (lngPt * 1E6));
    Log.e("point p ",""+p);

    mapController.animateTo(p);
    mapController.setZoom(16);
    mapView.invalidate();
    mapView.setTraffic(true);
    mapController = mapView.getController();

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

in my app i want to show a map with a pointer and above the pointer i want to show a text view with a button. Using that button i want to move to next activity. Following is my code to show the point, but how to show a point with text and button to right of it. And how to write the btn action for it

class MapOverlay extends com.google.android.maps.Overlay
 {
    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);
        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.marker);           
        canvas.drawBitmap(bmp, screenPts.x, screenPts.y-100, null);        

        mapView.getProjection().toPixels(q, screenPts);
        @SuppressWarnings("unused")
        Bitmap bmp1 = BitmapFactory.decodeResource(getResources(), R.drawable.blue);           
        canvas.drawBitmap(bmp, screenPts.x, screenPts.y-100, null);        
        return true;
    }
 }



p = new GeoPoint((int) (latPt * 1E6),(int) (lngPt * 1E6));
    Log.e("point p ",""+p);

    mapController.animateTo(p);
    mapController.setZoom(16);
    mapView.invalidate();
    mapView.setTraffic(true);
    mapController = mapView.getController();

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

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑红尘 2024-12-22 00:38:24

我通过使用视图而不是叠加来完成此操作,因此您可以使用 addItem 为地图中的每个项目添加一个新视图。这是代码:

AnnotationView

public AnnotationView(final Context context,final Object object) {
    super(context);
    setOrientation(VERTICAL);
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.map_overlay, this, true);

    // Set the textview

    lBubble = (LinearLayout) findViewById(R.id.lAnnotation);
    txtPlace = ((TextView) (findViewById(R.id.txtPlace)));
    txtPlace.setText(object.getName());

    // set button touch listener

    ((ImageButton) (findViewById(R.id.btnPlace)))
            .setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    //Do anything
                };
            });
    // add your overlay on mapview by geopoint

    btnMarker = ((ImageButton) (findViewById(R.id.btnMarker)));
    btnMarker.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
//Do anything           };
    });

    setLayoutParams(new MapView.LayoutParams(
            MapView.LayoutParams.WRAP_CONTENT,
            MapView.LayoutParams.WRAP_CONTENT, new GeoPoint(
                    (int) (object.getLatitude() * 1E6),
                    (int) (object.getLongitude() * 1E6)),
            MapView.LayoutParams.BOTTOM_CENTER));
}

然后我刚刚创建它并添加到地图中,如下所示:

overlay = new AnnotationView(this, object);
mapView.addView(overlay);

map_overlay 布局只是您想要在地图上显示为叠加层(图像、按钮...)的 linealayout

我只有一个问题缩放比例,因为缩放时点会发生一些变化,我正在尝试解决它。

希望有帮助

干杯

I did it by using views instead of overlays, so you add a new view for each item in your map with addItem. Here it is the code:

AnnotationView

public AnnotationView(final Context context,final Object object) {
    super(context);
    setOrientation(VERTICAL);
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.map_overlay, this, true);

    // Set the textview

    lBubble = (LinearLayout) findViewById(R.id.lAnnotation);
    txtPlace = ((TextView) (findViewById(R.id.txtPlace)));
    txtPlace.setText(object.getName());

    // set button touch listener

    ((ImageButton) (findViewById(R.id.btnPlace)))
            .setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    //Do anything
                };
            });
    // add your overlay on mapview by geopoint

    btnMarker = ((ImageButton) (findViewById(R.id.btnMarker)));
    btnMarker.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
//Do anything           };
    });

    setLayoutParams(new MapView.LayoutParams(
            MapView.LayoutParams.WRAP_CONTENT,
            MapView.LayoutParams.WRAP_CONTENT, new GeoPoint(
                    (int) (object.getLatitude() * 1E6),
                    (int) (object.getLongitude() * 1E6)),
            MapView.LayoutParams.BOTTOM_CENTER));
}

Then I just created it and add to the map like:

overlay = new AnnotationView(this, object);
mapView.addView(overlay);

The map_overlay layout is just the linealayout you want to display over the map as a overlay (images, buttons...)

I have only a problem with zoom scales, because the point changes a bit while zooming I am trying to solve it.

Hope that helps

Cheers

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文