How do i zoom in automatically to the current location in Google map api for android? -
i have integrated google maps api in android studio , , finds current location , want increase zoom level , can view current location more , precised. how increase zoom level?
you can find code below :
package com.example.umar.testgoogle; import android.location.location; import android.content.intentsender; import android.location.location; import android.os.bundle; import android.support.v4.app.fragmentactivity; import android.util.log; import com.google.android.gms.common.connectionresult; import com.google.android.gms.common.api.googleapiclient; import com.google.android.gms.location.locationlistener; import com.google.android.gms.location.locationrequest; import com.google.android.gms.location.locationservices; import com.google.android.gms.maps.cameraupdatefactory; import com.google.android.gms.maps.googlemap; import com.google.android.gms.maps.supportmapfragment; import com.google.android.gms.maps.model.latlng; import com.google.android.gms.maps.model.markeroptions; public class mapsactivity extends fragmentactivity implements googleapiclient.connectioncallbacks, googleapiclient.onconnectionfailedlistener, locationlistener { public static final string tag = mapsactivity.class.getsimplename(); /* * define request code send google play services * code returned in activity.onactivityresult */ private final static int connection_failure_resolution_request = 9000; private googlemap mmap; // might null if google play services apk not available. private googleapiclient mgoogleapiclient; private locationrequest mlocationrequest; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_maps); setupmapifneeded(); mgoogleapiclient = new googleapiclient.builder(this) .addconnectioncallbacks(this) .addonconnectionfailedlistener(this) .addapi(locationservices.api) .build(); // create locationrequest object mlocationrequest = locationrequest.create() .setpriority(locationrequest.priority_high_accuracy) .setinterval(10 * 1000) // 10 seconds, in milliseconds .setfastestinterval(1 * 1000); // 1 second, in milliseconds } @override protected void onresume() { super.onresume(); setupmapifneeded(); mgoogleapiclient.connect(); } @override protected void onpause() { super.onpause(); if (mgoogleapiclient.isconnected()) { locationservices.fusedlocationapi.removelocationupdates(mgoogleapiclient, this); mgoogleapiclient.disconnect(); } } /** * sets map if possible (i.e., google play services apk correctly * installed) , map has not been instantiated.. ensure ever * call {@link #setupmap()} once when {@link #mmap} not null. * <p/> * if isn't installed {@link supportmapfragment} (and * {@link com.google.android.gms.maps.mapview mapview}) show prompt user * install/update google play services apk on device. * <p/> * user can return fragmentactivity after following prompt , correctly * installing/updating/enabling google play services. since fragmentactivity may not * have been destroyed during process (it * stopped or paused), {@link #oncreate(bundle)} may not called again should call * method in {@link #onresume()} guarantee called. */ private void setupmapifneeded() { // null check confirm have not instantiated map. if (mmap == null) { // try obtain map supportmapfragment. mmap = ((supportmapfragment) getsupportfragmentmanager().findfragmentbyid(r.id.map)) .getmap(); // check if successful in obtaining map. if (mmap != null) { setupmap(); } } } /** * can add markers or lines, add listeners or move camera. in case, * add marker near africa. * <p/> * should called once , when sure {@link #mmap} not null. */ private void setupmap() { mmap.addmarker(new markeroptions().position(new latlng(0, 0)).title("marker")); } private void handlenewlocation(location location) { log.d(tag, location.tostring()); double currentlatitude = location.getlatitude(); double currentlongitude = location.getlongitude(); latlng latlng = new latlng(currentlatitude, currentlongitude); //mmap.addmarker(new markeroptions().position(new latlng(currentlatitude, currentlongitude)).title("current location")); markeroptions options = new markeroptions() .position(latlng) .title("i here!"); mmap.addmarker(options); mmap.movecamera(cameraupdatefactory.newlatlng(latlng)); } @override public void onconnected(bundle bundle) { location location = locationservices.fusedlocationapi.getlastlocation(mgoogleapiclient); if (location == null) { locationservices.fusedlocationapi.requestlocationupdates(mgoogleapiclient, mlocationrequest, this); } else { handlenewlocation(location); } } @override public void onconnectionsuspended(int i) { } @override public void onconnectionfailed(connectionresult connectionresult) { /* * google play services can resolve errors detects. * if error has resolution, try sending intent * start google play services activity can resolve * error. */ if (connectionresult.hasresolution()) { try { // start activity tries resolve error connectionresult.startresolutionforresult(this, connection_failure_resolution_request); /* * thrown if google play services canceled original * pendingintent */ } catch (intentsender.sendintentexception e) { // log error e.printstacktrace(); } } else { /* * if no resolution available, display dialog * user error. */ log.i(tag, "location services connection failed code " + connectionresult.geterrorcode()); } } @override public void onlocationchanged(location location) { handlenewlocation(location); }
}
change from:
mmap.movecamera(cameraupdatefactory.newlatlng(latlng));
to:
float zoomlevel = 16.0; //this goes 21 mmap.movecamera(cameraupdatefactory.newlatlngzoom(latlng, zoomlevel));
public static cameraupdate newlatlngzoom (latlng latlng, float zoom)
returns cameraupdate moves center of screen latitude , longitude specified latlng object, , moves given zoom level.
parameters latlng latlng object containing desired latitude , longitude. zoom desired zoom level, in range of 2.0 21.0. values below range set 2.0, , values above set 21.0. increase value zoom in. not areas have tiles @ largest zoom levels. returns cameraupdate containing transformation.
Comments
Post a Comment