Android谷歌地图显示为图片

Android Google Map to show as picture(Android谷歌地图显示为图片)

本文介绍了Android谷歌地图显示为图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的 Android 应用中实现了 Google Maps V2,现在我想做一些不同的事情.我正在用标记显示一个位置(经度、纬度).问题是这在我的活动中显示为地图.我想将它显示为 ImageView,并且只有当有人点击它时才会显示地图界面.

I've implemented Google Maps V2 in my android app and now I would like to do something differently. I'm showing a location (longitude, latitude) with a marker. The problem is that this is showing in my activity as a Map. I would like to show it as a ImageView and only if someone clicks on it show the map interface.

基本上我想要做的是位置的静态预览并将其显示为图片(带有框架和圆角)单击时打开地图并提供所有功能.

Basically what I want to do is a static preview of the location and display it as a picture (with a frame and rounded corners) When clicked then open maps and give all functionality.

有没有办法将地图片段放入 ImageView 中?

Is there a way to put a map fragment into a ImageView?

您能提供的任何帮助将不胜感激.

Any help you can provide will greatly appreciated.

谢谢

推荐答案

您可以使用 Google Maps 内置的快照方法,捕获预览并将其显示在 ImageView 中.您可以以任何您喜欢的方式设置 ImageView 的样式,还可以设置一个 clickListener 在单击预览时执行某些操作.这是一个例子:

You can use the Google Maps built-in snapshot method, to capture a preview and display it in an ImageView. You can style the ImageView in any way you like, and also set a clickListener that does something, when the preview is clicked. Here's an example:

LatLng latLng = new LatLng(35.0116363, 135.7680294);
// Add Marker
mMap.addMarker(new MarkerOptions().position(latLng));
// Center map on the marker
CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(latLng, 4.0f);
mMap.animateCamera(yourLocation);

final ImageView mapPreview = (ImageView) findViewById(R.id.mapPreview);
mapPreview.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        // Hide the preview, to reveal the map
        mapPreview.setImageBitmap(null);
        mapPreview.setLayoutParams(new RelativeLayout.LayoutParams(0, 0));

        // Or start Google Maps app
//      String uri = String.format(Locale.ENGLISH, "geo:%f,%f", 50.0, 0.1);
//      Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
//      startActivity(intent);
    }
});

mMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
    @Override
    public void onMapLoaded() {
        // Make a snapshot when map's done loading
        mMap.snapshot(new GoogleMap.SnapshotReadyCallback() {
            @Override
            public void onSnapshotReady(Bitmap bitmap) {
                mapPreview.setLayoutParams(new RelativeLayout.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT));
                mapPreview.setImageBitmap(bitmap);

                // If map won't be used afterwards, remove it's views
//              ((FrameLayout)findViewById(R.id.map)).removeAllViews();
            }
        });
    }
});

这篇关于Android谷歌地图显示为图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Android谷歌地图显示为图片