How to Focus at Marker in google map in android
Asked Answered
B

2

5

I just want to know whether we can focus at added marker in android application or not. If yes, how? or is there any alternative way to get this task done.

lets say I have added a marker using below code :

map.addMarker(new MarkerOptions()
            .title(title)
            .snippet(snippet)
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
            .position(pos)
                );

how to focus at this marker with maximum zoom. and if I will add 1 more marker it should adjust zooming(maximum possible zoom) in such a way so that both marker will display at once.
This is what I am trying to do but it is failing at line map.moveCamera(cu);.

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;

import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.CancelableCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

public class LocateUserInMap extends FragmentActivity {
    private GoogleMap map;
    private List<Marker> markers;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_locate_user_in_map);

        markers = new ArrayList<Marker>();
        Intent intent = getIntent();
        double frndLatVal = intent.getDoubleExtra("frndLatVal", 0);
        double frndLonVal = intent.getDoubleExtra("frndLonVal", 0);
        LatLng myloc = new LatLng(19.115486500000000000, 72.905544299999970000);
        LatLng friendloc = new LatLng(frndLatVal, frndLonVal);
        map = ((SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.google_map)).getMap();
        addToMap(myloc, "My location", "From here 0M", "blue");
        addToMap(friendloc, "Friend location", "From here 100M", "red");
        showMarkersAtOnce();
    }

    private void showMarkersAtOnce() {
        LatLngBounds.Builder builder = new LatLngBounds.Builder();
        for (Marker m : markers) {
            builder.include(m.getPosition());
        }
        LatLngBounds bounds = builder.build();
        int padding = 0; // offset from edges of the map in pixels
        CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
        map.moveCamera(cu);
    /*  map.getUiSettings().setScrollGesturesEnabled(false);
        map.animateCamera(cu,
                //CameraUpdateFactory.newLatLng(new LatLng(lat, lng))
                 new CancelableCallback()
        {

            @Override
            public void onFinish()
            {
                map.getUiSettings().setScrollGesturesEnabled(true);

            }

            @Override
            public void onCancel()
            {
                map.getUiSettings().setAllGesturesEnabled(true);

            }
        });*/
      //  map.animateCamera(CameraUpdateFactory.zoomBy(13));
    }

    private void addToMap(LatLng pos, String title, String snippet,
            String markercolor) {
        Marker localmarker = null;
        if (markercolor == "blue")
        {
            localmarker = map.addMarker(new MarkerOptions()
                    .title(title)
                    .snippet(snippet)
                    .icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
                    .position(pos));
        } else if (markercolor == "red") 
        {
            localmarker = map.addMarker(new MarkerOptions()
                    .title(title)
                    .snippet(snippet)
                    .icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_RED))
                    .position(pos));
        }

        markers.add(localmarker);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.locate_user_in_map, menu);
        return true;
    }

}

and Logcat

08-04 13:23:32.975: W/EGL_emulation(764): eglSurfaceAttrib not implemented
08-04 13:23:42.787: D/dalvikvm(764): GC_CONCURRENT freed 169K, 3% free 11335K/11591K, paused 130ms+40ms, total 336ms
08-04 13:23:49.812: D/dalvikvm(764): GC_CONCURRENT freed 273K, 3% free 11497K/11847K, paused 121ms+124ms, total 480ms
08-04 13:23:49.972: E/ActivityThread(764): Failed to find provider info for com.google.settings
08-04 13:23:50.259: E/ActivityThread(764): Failed to find provider info for com.google.settings
08-04 13:23:54.223: D/dalvikvm(764): GC_CONCURRENT freed 262K, 3% free 11686K/12039K, paused 115ms+110ms, total 396ms
Bozeman answered 31/7, 2013 at 13:25 Comment(0)
B
19

You have to calculate of all the markers. To do so

LatLngBounds.Builder builder = new LatLngBounds.Builder();
for each (Marker m : markers) {
    builder.include(m.getPosition());
}
LatLngBounds bounds = builder.build();

Now obtain CameraUpdateFactory:

int padding = 0; // offset from edges of the map in pixels
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);

Finally move camera on group of markers like this :

googleMap.moveCamera(cu);

Or if you want an animation:

googleMap.animateCamera(cu);
Bencion answered 31/7, 2013 at 13:49 Comment(4)
do I need to prepare markers (marker Array) or is there any inbuild method to get all added marker from map?Bozeman
also can you give any reference link which I can follow?Bozeman
I don't think there is any method to get all markers on map. You have to put all markers lat long in Bound manually. Or you can add them in an Array at the time you add them on Map. Here you can find some detail on LatLongBounds developers.google.com/maps/documentation/android/reference/com/…Bencion
I have resolved it. I need to wait for map to load in order to animateCamera to work, but here is 1 more question that how can I wait and then call method to update camera. I don't want user to click to get both marker in a single view.Bozeman
M
1

You can do something like this. What you have to do is to iterate trough your markers and find the outer most coordinates.

LatLngBounds bounds = new LatLngBounds(southWest, northEast);

mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, MAP_PADDING));
Mccartney answered 31/7, 2013 at 13:40 Comment(6)
posted in OP. Please take a look. moveCamera/animateCamera is working if I use map.animateCamera(CameraUpdateFactory.zoomBy(13));Bozeman
Can you post the stack trace too pleaseMccartney
Are you running this on emulator or phone?Mccartney
on Emulator. But I have setup my emulator using (blog-emildesign.rhcloud.com/?p=527) [this] tutorial and I can see map on emulator. I am using 'google_play_service_libs' v4.Bozeman
Try to run it on a phone anywayMccartney
I have resolved it. I need to wait for map to load in order to animateCamera to work, but here is 1 more question that how can I wait and then call method to update camera. I don't want user to click to get both marker in a single view.Bozeman

© 2022 - 2024 — McMap. All rights reserved.