Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Google Map to show as picture

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.

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

Any help you can provide will greatly appreciated.

Thanks

like image 565
tzuvy Avatar asked Nov 15 '14 13:11

tzuvy


People also ask

How do I view photos on Google Maps on Android?

On your Android phone or tablet, open the Google Maps app . Search for a place or tap it on the map. To see more info, at the bottom, tap the place's name or address. Scroll to the right. Tap Photos.

How does Google Maps for Android work?

The Google Maps app for Android supports several different intents, allowing you to launch the Google Maps app and perform one of four actions: Display a map at a specified location and zoom level. Search for locations or places, and display them on a map.

How do I create an Android app that displays a map?

Create an Android app that displays a map by using the Google Maps template for Android Studio. If you have an existing Android Studio project that you'd like to set up, see Project Configuration. This quickstart is intended for developers who are familiar with basic Android development with Java or Kotlin.

How do I add or edit photos on Google Maps?

Add photos from "Contribute". 1 On your Android phone or tablet, open the Google Maps app . 2 Tap Contribute . 3 Under the "Contribute" tab, tap Add photo . 3.1 To post a photo, tap one or more photos. 3.2 To edit a photo’s location, tap the place name and select a different place.


2 Answers

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();
            }
        });
    }
});
like image 197
Simas Avatar answered Sep 18 '22 12:09

Simas


There is a slightly different option that can achieve the same result and requires very little coding, provided your app can use the network: you can call the Google Static Maps API to ask for an map image with arbitrary center, zoom level, size, and other features such as markers and paths.

For example the link:

https://maps.googleapis.com/maps/api/staticmap?center=40.714728,-73.998672&zoom=14&size=600x300

produces the following image:

enter image description here

This makes it very easy to display a map in an ImageView widget: you just need to build the appropriate URL from the location data and dimensions, and then display the image with any of the image loading frameworks such as Picasso, Volley, &c. We've using this feature for a couple of years now.

This is especially useful when displaying, say, a ListView of little maps. In those situations, asking for n snapshots will be problematic, while n image downloads should work just fine.


A third option, which will become available in the next week or so, according to this announcement, is the new Lite Maps mode which will be released as part of Google Play Services 6.5:

In addition, there is also a new ‘lite mode’ map option, ideal for situations where you want to provide a number of smaller maps, or a map that is so small that meaningful interaction is impractical, such as a thumbnail in a list. A lite mode map is a bitmap image of a map at a specified location and zoom level.

In lite mode, markers and shapes are drawn client-side on top of the static image, so you still have full control over them. Lite mode supports all of the map types, the My Location layer, and a subset of the functionality of a fully-interactive map.

Sounds exactly like what you need, provided you can wait a few days for the updated SDK. :)

like image 23
matiash Avatar answered Sep 17 '22 12:09

matiash