Android Google Map to show as picture

后端 未结 2 1581
慢半拍i
慢半拍i 2021-01-30 09:53

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

2条回答
  •  孤城傲影
    2021-01-30 10:20

    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();
                }
            });
        }
    });
    

提交回复
热议问题