Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Focus at Marker in google map in android

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
like image 985
user2376920 Avatar asked Jul 31 '13 13:07

user2376920


People also ask

How do you focus on Google Maps?

On your computer, open Google Maps. On your keyboard, press Tab until the map is in focus.

How do I move a marker smoothly on Google Maps?

The transition() and moveMarker() are used to move marker smoothly on click on the Google map.

How do I drag a marker on Google Maps Android?

Long press the marker to enable dragging. When you take your finger off the screen, the marker will remain in that position. Markers are not draggable by default. You must explicitly set the marker to be draggable either with MarkerOptions.


2 Answers

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);
like image 140
Vipul Purohit Avatar answered Oct 23 '22 22:10

Vipul Purohit


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));
like image 1
Flaxie Avatar answered Oct 23 '22 22:10

Flaxie