I am currently working on a Fragment with a Google Map in it. Once a user visits that fragment his map should zoom and focus on his location. However, it shows map of the world and not nearby location (near user):
@SuppressWarnings("unused")
public class DeferredMapFragment extends MapFragment implements GoogleMap.OnCameraChangeListener, OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,
LocationListener {
private static final String TAG = "DeferredMapFragment";
private Deque<Runnable> pendingActions;
private AbstractMap<Marker, Object> tags;
private GoogleMap map;
private LocationRequest mLocationRequest;
private GoogleApiClient mGoogleApiClient;
private Location mLastLocation;
private Marker marker;
private GoogleMap.OnCameraChangeListener cameraChangeListener = null;
private boolean isMapReady = false;
/*
* INTERNALS
*/
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (getMap() != null) {
map = getMap();
getMap().setOnCameraChangeListener(this);
}
}
@Override
public void onCameraChange(CameraPosition cameraPosition) {
isMapReady = true;
if (pendingActions != null) {
int i = pendingActions.size();
while (i > 0) {
pendingActions.pop().run();
--i;
}
}
if (cameraChangeListener != null) {
cameraChangeListener.onCameraChange(cameraPosition);
}
if (getMap() != null) {
getMap().setOnCameraChangeListener(cameraChangeListener);
}
}
private void execute(Runnable action) {
if (action == null) {
return;
}
if (isMapReady) {
action.run();
} else {
if (pendingActions == null) {
pendingActions = new LinkedList<>();
}
pendingActions.add(action);
}
}
/*
* TAGGING
*/
private void addTag(Marker key, Object value) {
if (tags == null) {
tags = new HashMap<>();
}
tags.put(key, value);
}
public Object getTag(Marker key) {
return tags != null ? tags.get(key) : null;
}
/*
* LISTENERS
*/
public void setOnInfoWindowClickListener(final GoogleMap.OnInfoWindowClickListener listener) {
execute(new Runnable() {
@Override
public void run() {
getMap().setOnInfoWindowClickListener(listener);
}
});
}
public void setOnCameraChangeListener(final GoogleMap.OnCameraChangeListener listener) {
cameraChangeListener = listener;
}
public void setOnMarkerClickListener(final GoogleMap.OnMarkerClickListener listener) {
execute(new Runnable() {
@Override
public void run() {
getMap().setOnMarkerClickListener(listener);
}
});
}
public void setOnMapClickListener(final GoogleMap.OnMapClickListener listener) {
execute(new Runnable() {
@Override
public void run() {
getMap().setOnMapClickListener(listener);
}
});
}
/*
* MAP OVERLAYS
*/
public void addPolyline(final PolylineOptions options) {
execute(new Runnable() {
@Override
public void run() {
getMap().addPolyline(options);
}
});
}
public void addPolygon(final PolygonOptions options) {
execute(new Runnable() {
@Override
public void run() {
getMap().addPolygon(options);
}
});
}
public void addCircle(final CircleOptions options) {
execute(new Runnable() {
@Override
public void run() {
getMap().addCircle(options);
}
});
}
public void addMarker(final MarkerOptions options) {
addMarker(options, null);
}
public void addMarker(final MarkerOptions options, final Object tag) {
execute(new Runnable() {
@Override
public void run() {
Marker marker = getMap().addMarker(options);
if (tag != null) {
addTag(marker, tag);
}
}
});
}
public void addGroundOverlay(final GroundOverlayOptions options) {
execute(new Runnable() {
@Override
public void run() {
getMap().addGroundOverlay(options);
}
});
}
public void addTileOverlay(final TileOverlayOptions options) {
execute(new Runnable() {
@Override
public void run() {
getMap().addTileOverlay(options);
}
});
}
/*
* UI SETTINGS
*/
public void setMapToolbarEnabled(final boolean enabled) {
execute(new Runnable() {
@Override
public void run() {
getMap().getUiSettings().setMapToolbarEnabled(enabled);
}
});
}
public void setPadding(final int left, final int top, final int right, final int bottom) {
execute(new Runnable() {
@Override
public void run() {
getMap().setPadding(left, top, right, bottom);
}
});
}
public void setZoomControlsEnabled(final boolean enabled) {
execute(new Runnable() {
@Override
public void run() {
getMap().getUiSettings().setZoomControlsEnabled(enabled);
}
});
}
public void setCompassEnabled(final boolean enabled) {
execute(new Runnable() {
@Override
public void run() {
getMap().getUiSettings().setCompassEnabled(enabled);
}
});
}
public void setMyLocationButtonEnabled(final boolean enabled) {
execute(new Runnable() {
@Override
public void run() {
getMap().getUiSettings().setMyLocationButtonEnabled(enabled);
}
});
}
public void setIndoorLevelPickerEnabled(final boolean enabled) {
execute(new Runnable() {
@Override
public void run() {
getMap().getUiSettings().setIndoorLevelPickerEnabled(enabled);
}
});
}
public void setScrollGesturesEnabled(final boolean enabled) {
execute(new Runnable() {
@Override
public void run() {
getMap().getUiSettings().setScrollGesturesEnabled(enabled);
}
});
}
public void setZoomGesturesEnabled(final boolean enabled) {
execute(new Runnable() {
@Override
public void run() {
getMap().getUiSettings().setZoomGesturesEnabled(enabled);
}
});
}
public void setTiltGesturesEnabled(final boolean enabled) {
execute(new Runnable() {
@Override
public void run() {
getMap().getUiSettings().setTiltGesturesEnabled(enabled);
}
});
}
public void setRotateGesturesEnabled(final boolean enabled) {
execute(new Runnable() {
@Override
public void run() {
getMap().getUiSettings().setRotateGesturesEnabled(enabled);
}
});
}
public void setAllGesturesEnabled(final boolean enabled) {
execute(new Runnable() {
@Override
public void run() {
getMap().getUiSettings().setAllGesturesEnabled(enabled);
}
});
}
public void setInfoWindowAdapter(final GoogleMap.InfoWindowAdapter adapter) {
execute(new Runnable() {
@Override
public void run() {
getMap().setInfoWindowAdapter(adapter);
}
});
}
@Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
setUpMap();
}
public void setUpMap() {
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
map.setMyLocationEnabled(true);
}
@Override
public void onConnected(Bundle bundle) {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(1000);
mLocationRequest.setFastestInterval(1000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
//mLocationRequest.setSmallestDisplacement(0.1F);
if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onLocationChanged(Location location) {
mLastLocation = location;
//remove previous current location Marker
if (marker != null){
marker.remove();
}
double dLatitude = mLastLocation.getLatitude();
double dLongitude = mLastLocation.getLongitude();
marker = map.addMarker(new MarkerOptions().position(new LatLng(dLatitude, dLongitude))
.title("My Location").icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(dLatitude, dLongitude), 8));
}
@Override
public void onPause() {
super.onPause();
if (mGoogleApiClient != null) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
}
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(getContext())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
}
Is there a zoom feature available? Also my api allows me to access zip code of user. Can I use that instead? I require user to be shown his nearby locations.