泡泡弹出框

时间:2011-12-11 10:40:31

标签: android google-maps

谷歌地图,它允许我们精确定位我们想要的任何位置。在引脚指向后,推针顶部会出现这个“气泡”弹出框。我可以知道如何弹出窗口框吗?任何代码显示?我现在正在使用警告对话框,但我想让它成为“泡泡”弹出窗口。

List<Overlay> mapOverlays = mapView.getOverlays();
        MapOverlay mapOverlay = new MapOverlay();

        mapOverlays.add(mapOverlay);

        // obtain gps location
        lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        locationListener = new MyLocationListener();

        lm.requestLocationUpdates(
        // LocationManager.GPS_PROVIDER,
                LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
    }

    private class MyLocationListener implements LocationListener {

        @Override
        public void onLocationChanged(Location loc) {

            k = new GeoPoint((int) (loc.getLatitude() * 1E6),
                    (int) (loc.getLongitude() * 1E6));
            mc.animateTo(k);
            mc.setZoom(18);

            // Add a location marker
            MapOverlay mapOverlay = new MapOverlay();
            List<Overlay> listofOverlays = mapView.getOverlays();
            listofOverlays.clear();
            listofOverlays.add(mapOverlay);

            // invalidate() method forces the MapView to be redrawn
            mapView.invalidate();
        }

        @Override
        public void onProviderDisabled(String provider) {
        }

        @Override
        public void onProviderEnabled(String provider) {
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }

    class MapOverlay extends com.google.android.maps.Overlay {

        @Override
        public boolean onTap(final GeoPoint p, MapView mapView) {
            // TODO Auto-generated method stub

            k = p;
            mc = mapView.getController();
            mc.animateTo(p);

            mapView.invalidate();

            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";
                    txtAddress.setText("Address: " + add);
                }
@Override
        public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
                long when) {
            super.draw(canvas, mapView, shadow);
            if (k != null) {
                // ---translate the GeoPoint to screen pixels---
                Point screenPts = new Point();
                mapView.getProjection().toPixels(k, screenPts);

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

3 个答案:

答案 0 :(得分:1)

我目前正在制作地图应用程序,我还需要显示“泡泡”弹出窗口,以下博客文章对我有所帮助。

This first post显示了如何使用嵌入在视图中的9补丁图像来生成弹出窗口。代码还可以,但确实留下了许多问题,一些评论者要求提供一些额外的源代码以便澄清。

This post from the Android Developers blog解释了9补丁图像是如何创建的。

使用这两个帖子,我能够将一些代码拉到一起,效果很好,所以希望你能够操作它以满足你的需求。

答案 1 :(得分:0)

听起来你正在寻找InfoWindow。顺便说一下,在较新版本的API中,它们没有圆角(它们看起来更像是盒子而不像气泡)。您可以通过在加载maps API的javascript URL中请求API的先前版本来恢复旧版本。

答案 2 :(得分:0)

Google没有提供API来执行此操作,请查看this。 Victor的回答提供了适合该法案的开源代码的链接。