GPS tracker with GoogleApiClient - android

in the last update of google services, Google has depercated LocationClient api and now say use GoogleApiClient.
Now need create the App with GPS report any 30 seconds to my webserver but dont found (or dont understant) how work this new api.
If you have a example using GoogleApiClient please past the link to see or download.
And if have a Service with GoogleApiClient please past the link
Thanks for your help.

If you have installed android sdk then just checkout following directory \extras\google\google_play_services\samples\maps\src\com\example\mapdemo\.
It is having one example of showing current location in GoogleMap and it is using GoogleApiClient to retrieve current location on periodic interval of 5 seconds as described in following code. You can modify it according to your requirements.
MyLocationDemoActivity.java
public class MyLocationDemoActivity extends FragmentActivity
implements
ConnectionCallbacks,
OnConnectionFailedListener,
LocationListener,
OnMyLocationButtonClickListener {
private GoogleMap mMap;
private GoogleApiClient mGoogleApiClient;
private TextView mMessageView;
// These settings are the same as the settings for the map. They will in fact give you updates
// at the maximal rates currently possible.
private static final LocationRequest REQUEST = LocationRequest.create()
.setInterval(5000) // 5 seconds
.setFastestInterval(16) // 16ms = 60fps
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_location_demo);
mMessageView = (TextView) findViewById(R.id.message_text);
}
#Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
setUpGoogleApiClientIfNeeded();
mGoogleApiClient.connect();
}
#Override
public void onPause() {
super.onPause();
if (mGoogleApiClient != null) {
mGoogleApiClient.disconnect();
}
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
mMap.setMyLocationEnabled(true);
mMap.setOnMyLocationButtonClickListener(this);
}
}
}
private void setUpGoogleApiClientIfNeeded() {
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
}
/**
* Button to get current Location. This demonstrates how to get the current Location as required
* without needing to register a LocationListener.
*/
public void showMyLocation(View view) {
if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
String msg = "Location = "
+ LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
}
}
/**
* Implementation of {#link LocationListener}.
*/
#Override
public void onLocationChanged(Location location) {
mMessageView.setText("Location = " + location);
}
/**
* Callback called when connected to GCore. Implementation of {#link ConnectionCallbacks}.
*/
#Override
public void onConnected(Bundle connectionHint) {
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient,
REQUEST,
this); // LocationListener
}
/**
* Callback called when disconnected from GCore. Implementation of {#link ConnectionCallbacks}.
*/
#Override
public void onConnectionSuspended(int cause) {
// Do nothing
}
/**
* Implementation of {#link OnConnectionFailedListener}.
*/
#Override
public void onConnectionFailed(ConnectionResult result) {
// Do nothing
}
#Override
public boolean onMyLocationButtonClick() {
Toast.makeText(this, "MyLocation button clicked", Toast.LENGTH_SHORT).show();
// Return false so that we don't consume the event and the default behavior still occurs
// (the camera animates to the user's current position).
return false;
}
}

I had exactly your same problem. You need to explicitely use GoogleMap.setLocationSource().
Here is an example: Android: Google Maps location with low battery usage

Related

How to set a marker on SupportMapFragment in tabbed Activity?

I try to implement support map fragment inside of a fragment in tabbed activity.
I try to set a map, and set markers on the map. I succeed to set the map, but not the marker. I tried to find an answer, but I couldn't fix the problem by myself. I would love some help.
The map fragment code:
public class Map_frag extends SupportMapFragment implements OnMapReadyCallback, GoogleMap.OnMapClickListener {
private SharedPreferences sp;
private Context context;
private float myLat, myLng, locLat, locLnf;
public GoogleMap map;
private Marker marker;
// Empty constructor
public Map_frag() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.map_fragment, container,false);
context = getContext();
sp = PreferenceManager.getDefaultSharedPreferences(context);
return v;
}
#Override
public void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
if (map == null) {
getMapAsync(this);
}
}
#Override
public void onMapReady(GoogleMap map) {
this.map = map;
onLocChange();
}
public void onLocChange(){
if (map ==null) {
map.clear();
if (marker !=null){
marker.remove();
}
myLat = Float.parseFloat(sp.getString("lat", "0"));
myLng = Float.parseFloat(sp.getString("lng", "0"));
LatLng latLng = new LatLng(myLat, myLng);
marker = map.addMarker( new MarkerOptions().position(latLng).title("myLoc"));
map.animateCamera(CameraUpdateFactory.
newLatLngZoom(latLng,15));
}
return;
}
#Override
public void onMapClick(LatLng latLng) {
}
}
And the XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00d05b5b">
<fragment android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:id="#+id/fragment2"
android:layout_gravity="center_horizontal"
tools:layout="#layout/place_autocomplete_fragment"/>
</LinearLayout>
onLocChange what is this? onLocationChanged is an override method.You can use it.
Note: you have created your own onLocChange method and you call it on onMapReady so it gets only called at the First time. Then onLocChange does not get called. but if you have used onLocationChanged override method it gets called every time when the location changes!!
inside that
remove you first if block it covers all the code and only works when the map is null , when map is null there is no point of adding a marker, keep it away.. or use it like below
if(map==null){
//map null
} else {
// add code for markers
}
If you use above block this should go with else part
if (currLocationMarker != null) {
// you already have a marker so when location changes you remove it and add a new one thats why you need this
currLocationMarker.remove();
}
latLng = new LatLng(location.getLatitude(), location.getLongitude());
MarkerOptions markerOptions = new MarkerOptions(); // just added to clear thingsyou you can add this where instance creates only once
markerOptions.position(latLng);
markerOptions.title("Current Position");
markerOptions.icon(iconForVehicleMarker);
currLocationMarker = mGoogleMap.addMarker(markerOptions);
want a full example with everything you need to know(additional)? see how onMapReady onLocationChaged and relevant methods used here
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.ResultCallback;
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.location.places.PlaceLikelihood;
import com.google.android.gms.location.places.PlaceLikelihoodBuffer;
import com.google.android.gms.location.places.Places;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
/**
* An activity that displays a map showing places around the device's current location.
*/
public class MapsActivityCurrentPlaces extends AppCompatActivity implements
OnMapReadyCallback,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener {
private static final String TAG = MapsActivityCurrentPlaces.class.getSimpleName();
private GoogleMap mMap;
private CameraPosition mCameraPosition;
// The entry point to Google Play services, used by the Places API and Fused Location Provider.
private GoogleApiClient mGoogleApiClient;
// A request object to store parameters for requests to the FusedLocationProviderApi.
private LocationRequest mLocationRequest;
// The desired interval for location updates. Inexact. Updates may be more or less frequent.
private static final long UPDATE_INTERVAL_IN_MILLISECONDS = 10000;
// The fastest rate for active location updates. Exact. Updates will never be more frequent
// than this value.
private static final long FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS =
UPDATE_INTERVAL_IN_MILLISECONDS / 2;
// A default location (Sydney, Australia) and default zoom to use when location permission is
// not granted.
private final LatLng mDefaultLocation = new LatLng(-33.8523341, 151.2106085);
private static final int DEFAULT_ZOOM = 15;
private static final int PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 1;
private boolean mLocationPermissionGranted;
// The geographical location where the device is currently located.
private Location mCurrentLocation;
// Keys for storing activity state.
private static final String KEY_CAMERA_POSITION = "camera_position";
private static final String KEY_LOCATION = "location";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Retrieve location and camera position from saved instance state.
if (savedInstanceState != null) {
mCurrentLocation = savedInstanceState.getParcelable(KEY_LOCATION);
mCameraPosition = savedInstanceState.getParcelable(KEY_CAMERA_POSITION);
}
// Retrieve the content view that renders the map.
setContentView(R.layout.activity_maps);
// Build the Play services client for use by the Fused Location Provider and the Places API.
buildGoogleApiClient();
mGoogleApiClient.connect();
}
/**
* Get the device location and nearby places when the activity is restored after a pause.
*/
#Override
protected void onResume() {
super.onResume();
if (mGoogleApiClient.isConnected()) {
getDeviceLocation();
}
updateMarkers();
}
/**
* Stop location updates when the activity is no longer in focus, to reduce battery consumption.
*/
#Override
protected void onPause() {
super.onPause();
if (mGoogleApiClient.isConnected()) {
LocationServices.FusedLocationApi.removeLocationUpdates(
mGoogleApiClient, this);
}
}
/**
* Saves the state of the map when the activity is paused.
*/
#Override
protected void onSaveInstanceState(Bundle outState) {
if (mMap != null) {
outState.putParcelable(KEY_CAMERA_POSITION, mMap.getCameraPosition());
outState.putParcelable(KEY_LOCATION, mCurrentLocation);
super.onSaveInstanceState(outState);
}
}
/**
* Gets the device's current location and builds the map
* when the Google Play services client is successfully connected.
*/
#Override
public void onConnected(Bundle connectionHint) {
getDeviceLocation();
// Build the map.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
/**
* Handles failure to connect to the Google Play services client.
*/
#Override
public void onConnectionFailed(#NonNull ConnectionResult result) {
// Refer to the reference doc for ConnectionResult to see what error codes might
// be returned in onConnectionFailed.
Log.d(TAG, "Play services connection failed: ConnectionResult.getErrorCode() = "
+ result.getErrorCode());
}
/**
* Handles suspension of the connection to the Google Play services client.
*/
#Override
public void onConnectionSuspended(int cause) {
Log.d(TAG, "Play services connection suspended");
}
/**
* Handles the callback when location changes.
*/
#Override
public void onLocationChanged(Location location) {
mCurrentLocation = location;
updateMarkers();
}
/**
* Manipulates the map when it's available.
* This callback is triggered when the map is ready to be used.
*/
#Override
public void onMapReady(GoogleMap map) {
mMap = map;
// Turn on the My Location layer and the related control on the map.
updateLocationUI();
// Add markers for nearby places.
updateMarkers();
// Use a custom info window adapter to handle multiple lines of text in the
// info window contents.
mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
#Override
// Return null here, so that getInfoContents() is called next.
public View getInfoWindow(Marker arg0) {
return null;
}
#Override
public View getInfoContents(Marker marker) {
// Inflate the layouts for the info window, title and snippet.
View infoWindow = getLayoutInflater().inflate(R.layout.custom_info_contents, null);
TextView title = ((TextView) infoWindow.findViewById(R.id.title));
title.setText(marker.getTitle());
TextView snippet = ((TextView) infoWindow.findViewById(R.id.snippet));
snippet.setText(marker.getSnippet());
return infoWindow;
}
});
/*
* Set the map's camera position to the current location of the device.
* If the previous state was saved, set the position to the saved state.
* If the current location is unknown, use a default position and zoom value.
*/
if (mCameraPosition != null) {
mMap.moveCamera(CameraUpdateFactory.newCameraPosition(mCameraPosition));
} else if (mCurrentLocation != null) {
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(mCurrentLocation.getLatitude(),
mCurrentLocation.getLongitude()), DEFAULT_ZOOM));
} else {
Log.d(TAG, "Current location is null. Using defaults.");
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mDefaultLocation, DEFAULT_ZOOM));
mMap.getUiSettings().setMyLocationButtonEnabled(false);
}
}
/**
* Builds a GoogleApiClient.
* Uses the addApi() method to request the Google Places API and the Fused Location Provider.
*/
private synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this /* FragmentActivity */,
this /* OnConnectionFailedListener */)
.addConnectionCallbacks(this)
.addApi(LocationServices.API)
.addApi(Places.GEO_DATA_API)
.addApi(Places.PLACE_DETECTION_API)
.build();
createLocationRequest();
}
/**
* Sets up the location request.
*/
private void createLocationRequest() {
mLocationRequest = new LocationRequest();
/*
* Sets the desired interval for active location updates. This interval is
* inexact. You may not receive updates at all if no location sources are available, or
* you may receive them slower than requested. You may also receive updates faster than
* requested if other applications are requesting location at a faster interval.
*/
mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
/*
* Sets the fastest rate for active location updates. This interval is exact, and your
* application will never receive updates faster than this value.
*/
mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
/**
* Gets the current location of the device and starts the location update notifications.
*/
private void getDeviceLocation() {
/*
* Request location permission, so that we can get the location of the
* device. The result of the permission request is handled by a callback,
* onRequestPermissionsResult.
*/
if (ContextCompat.checkSelfPermission(this.getApplicationContext(),
android.Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
mLocationPermissionGranted = true;
} else {
ActivityCompat.requestPermissions(this,
new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
}
/*
* Get the best and most recent location of the device, which may be null in rare
* cases when a location is not available.
* Also request regular updates about the device location.
*/
if (mLocationPermissionGranted) {
mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,
mLocationRequest, this);
}
}
/**
* Handles the result of the request for location permissions.
*/
#Override
public void onRequestPermissionsResult(int requestCode,
#NonNull String permissions[],
#NonNull int[] grantResults) {
mLocationPermissionGranted = false;
switch (requestCode) {
case PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
mLocationPermissionGranted = true;
}
}
}
updateLocationUI();
}
/**
* Adds markers for places nearby the device and turns the My Location feature on or off,
* provided location permission has been granted.
*/
private void updateMarkers() {
if (mMap == null) {
return;
}
if (mLocationPermissionGranted) {
// Get the businesses and other points of interest located
// nearest to the device's current location.
#SuppressWarnings("MissingPermission")
PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi
.getCurrentPlace(mGoogleApiClient, null);
result.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() {
#Override
public void onResult(#NonNull PlaceLikelihoodBuffer likelyPlaces) {
for (PlaceLikelihood placeLikelihood : likelyPlaces) {
// Add a marker for each place near the device's current location, with an
// info window showing place information.
String attributions = (String) placeLikelihood.getPlace().getAttributions();
String snippet = (String) placeLikelihood.getPlace().getAddress();
if (attributions != null) {
snippet = snippet + "\n" + attributions;
}
mMap.addMarker(new MarkerOptions()
.position(placeLikelihood.getPlace().getLatLng())
.title((String) placeLikelihood.getPlace().getName())
.snippet(snippet));
}
// Release the place likelihood buffer.
likelyPlaces.release();
}
});
} else {
mMap.addMarker(new MarkerOptions()
.position(mDefaultLocation)
.title(getString(R.string.default_info_title))
.snippet(getString(R.string.default_info_snippet)));
}
}
/**
* Updates the map's UI settings based on whether the user has granted location permission.
*/
#SuppressWarnings("MissingPermission")
private void updateLocationUI() {
if (mMap == null) {
return;
}
if (mLocationPermissionGranted) {
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setMyLocationButtonEnabled(true);
} else {
mMap.setMyLocationEnabled(false);
mMap.getUiSettings().setMyLocationButtonEnabled(false);
mCurrentLocation = null;
}
}
}

LocationServices.FusedLocationApi.getLastLocation Always gets null

I'm trying to mark current user location in a maps activity but the call of LocationServices.FusedLocationApi.getLastLocation always returns null
public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback,GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener {
private GoogleMap mMap;
private GoogleApiClient mGoogleApiClient=null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
mGoogleApiClient.connect();
}
#Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
#Override
public void onConnectionSuspended(int i) {
}
#Override
public void onConnected(Bundle bundle) {
}
#Override
protected void onStart() {
if(mGoogleApiClient.isConnected())
Toast.makeText(this,"Client Connect",Toast.LENGTH_SHORT).show();
else
mGoogleApiClient.connect();
super.onStart();
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
LatLng myLocation = null;
if(mLastLocation!=null) {
myLocation = new LatLng(mLastLocation.getLatitude(),
mLastLocation.getLongitude());
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(myLocation,
mMap.getMaxZoomLevel() - 5));
}
} else {
// Show rationale and request permission.
}
}
}
Also i have added the permission in manifest file
when checking for mGoogleApiClient for connectivity it's always not connected
You need to register a listener to the location events, and start listening for a short period of time to several locations received, and compare their accuracy , source, etc, to determine which is the best one, and use that one.
Read Location strategies google documentation, which is pretty useful and has code examples of what you need to do to get accurate locations.
Also add this permission to the manifest:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

Android Studio Emulator crashes on LocationServices.fusedLocationApi

So I have a Google Maps application in android studio and I am trying to set it up. I looked at Treehouse how they set it up with googles own location provider and everything that is necessary http://blog.teamtreehouse.com/beginners-guide-location-android
I have done everything it says and it works fine on my cellphone but the emulator crashes on startup and it says the fault is in the line
LocationServices.FusedLocationApi.requestLocationUpdates(this.mGoogleApiClient, this.mLocationRequest, this);
I dont know what I should do. I've even used the console to geo fix a location to the gps in the emulator but nothing. Here is my complete code
public class Guide extends AppCompatActivity implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
private LatLng currentPos;
private boolean centerCamera;
private boolean isSatelliteChecked = false;
public static final String TAG = Guide.class.getSimpleName();
private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_guide);
this.centerCamera = true;
setUpMapIfNeeded();
// Create the Google API Client
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
// Create the LocationRequest object
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(10 * 1000) // 10 seconds, in milliseconds
.setFastestInterval(1000); // 1 second, in milliseconds
}
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{
MenuItem checkable = menu.findItem(R.id.map_type);
checkable.setChecked(isSatelliteChecked);
return true;
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{// Inflate the menu; this adds items to the action bar if it is
// present.
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle item selection
switch (item.getItemId()) {
case R.id.menu_settings:
return true;
case R.id.map_type:
isSatelliteChecked = !item.isChecked();
item.setChecked(isSatelliteChecked);
if(isSatelliteChecked)
{
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
}
else
{
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
default:
return super.onOptionsItemSelected(item);
}
}
#Override
protected void onResume()
{
super.onResume();
setUpMapIfNeeded();
mGoogleApiClient.connect();
this.mMap.setMyLocationEnabled(true);
if (this.currentPos != null)
{
this.mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(currentPos, 16));
}
}
#Override
protected void onPause()
{
if (this.mGoogleApiClient.isConnected())
{
LocationServices.FusedLocationApi.removeLocationUpdates(this.mGoogleApiClient, this);
this.mGoogleApiClient.disconnect();
}
if (this.mMap!=null)
{
mMap.setMyLocationEnabled(false);
this.centerCamera = true;
}
super.onPause();
}
/**
* Sets up the map if it is possible to do so (i.e., the Google Play services APK is correctly
* installed) and the map has not already been instantiated.. This will ensure that we only ever
* call {#link #setUpMap()} once when {#link #mMap} is not null.
* <p/>
* If it isn't installed {#link SupportMapFragment} (and
* {#link com.google.android.gms.maps.MapView MapView}) will show a prompt for the user to
* install/update the Google Play services APK on their device.
* <p/>
* A user can return to this FragmentActivity after following the prompt and correctly
* installing/updating/enabling the Google Play services. Since the FragmentActivity may not
* have been completely destroyed during this process (it is likely that it would only be
* stopped or paused), {#link #onCreate(Bundle)} may not be called again so we should call this
* method in {#link #onResume()} to guarantee that it will be called.
*/
private void setUpMapIfNeeded()
{
// Do a null check to confirm that we have not already instantiated the map.
if (this.mMap == null)
{
// Try to obtain the map from the SupportMapFragment.
this.mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (this.mMap != null)
{
setUpMap();
}
}
}
/**
* This is where we can add markers or lines, add listeners or move the camera. In this case, we
* just add a marker near Africa.
* <p/>
* This should only be called once and when we are sure that {#link #mMap} is not null.
*/
private void setUpMap() {
this.mMap.setMyLocationEnabled(true);
this.mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}
private void handleNewLocation(Location location)
{
Log.d(this.TAG, location.toString());
}
#Override
public void onConnected(Bundle bundle)
{
Log.i(this.TAG, "Location services connected.");
Location location = LocationServices.FusedLocationApi.getLastLocation(this.mGoogleApiClient);
if (location == null)
{
LocationServices.FusedLocationApi.requestLocationUpdates(this.mGoogleApiClient, this.mLocationRequest, this);
}
else
{
handleNewLocation(location);
}
}
#Override
public void onConnectionSuspended(int i)
{
Log.i(this.TAG, "Location services suspended. Please reconnect.");
}
#Override
public void onConnectionFailed(ConnectionResult connectionResult)
{
if (connectionResult.hasResolution())
{
try {
// Start an Activity that tries to resolve the error
connectionResult.startResolutionForResult(this, CONNECTION_FAILURE_RESOLUTION_REQUEST);
} catch (IntentSender.SendIntentException e)
{
e.printStackTrace();
}
}
else
{
Log.i(this.TAG, "Location services connection failed with code " + connectionResult.getErrorCode());
}
}
#Override
public void onLocationChanged(Location location)
{
handleNewLocation(location);
}
Your emulator might not have google play services.
This may help:
How to download Google Play Services in an Android emulator?
This solved the problem for me.
In gradle build I check that both play-services have the same version. Here 10.2.6
compile 'com.google.android.gms:play-services-maps:10.2.6'
compile 'com.google.android.gms:play-services-location:10.2.6'

Change zoom level Google Maps Api v2 on app start

Is it possible to change the zoom level as soon as the map is ready? When I open the app it shows the map and the blue dot for my location. However, the zoom level is the default 3. How can I change this? I know how to do it when the 'MyLocationButton'is clicked but not when the app starts.
This is my class
public class MainPhoneActivity extends FragmentActivity implements ConnectionCallbacks, OnConnectionFailedListener, LocationSource, LocationListener, OnMyLocationButtonClickListener, OnMapReadyCallback{
private GoogleApiClient mGoogleApiClient;
private OnLocationChangedListener mMapLocationListener = null;
// location accuracy settings
private static final LocationRequest REQUEST = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_phone);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.mapView);
mapFragment.getMapAsync(this);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
#Override
protected void onResume() {
super.onResume();
mGoogleApiClient.connect();
}
#Override
public void onPause() {
super.onPause();
mGoogleApiClient.disconnect();
}
#Override
public void onMapReady(GoogleMap map) {
map.setLocationSource(this);
map.setMyLocationEnabled(true);
map.setOnMyLocationButtonClickListener(this);
}
public void showMyLocation(View view) {
if (mGoogleApiClient.isConnected()) {
String msg = "Location = "
+ LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
}
}
#Override
public void onLocationChanged(Location location) {
if (mMapLocationListener != null) {
mMapLocationListener.onLocationChanged(location);
}
}
#Override
public void onConnected(Bundle connectionHint) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, REQUEST,this);
}
#Override
public void onConnectionSuspended(int cause) {
// Do nothing
}
#Override
public void onConnectionFailed(ConnectionResult result) {
// Do nothing
}
#Override
public boolean onMyLocationButtonClick() {
return false;
}
#Override
public void activate(OnLocationChangedListener onLocationChangedListener) {
mMapLocationListener = onLocationChangedListener;
}
#Override
public void deactivate() {
mMapLocationListener = null;
}
}
You can set zoom level like following:
map.animateCamera(CameraUpdateFactory.newLatLngZoom(point,15));
Where point is your LatLng position. In this way you can center the map in that point with given zoom level.
Complete method:
#Override
public void onMapReady(GoogleMap map) {
map.setLocationSource(this);
map.setMyLocationEnabled(true);
map.setOnMyLocationButtonClickListener(this);
//newLatLngZoom(LatLng , ZoomLevel) -> choose your zoom level
// and change my 'point' with yours
map.animateCamera(CameraUpdateFactory.newLatLngZoom(point,15));
}
EDIT:
If you want to get the dot coords, you can try this:
Location loc = map.getMyLocation();
LatLng point = new LatLng(loc.getlatitude() , loc.getLongitude());
and use that point as center.
Complete method:
#Override
public void onMapReady(GoogleMap map) {
map.setLocationSource(this);
map.setMyLocationEnabled(true);
map.setOnMyLocationButtonClickListener(this);
Location loc = map.getMyLocation();
if(loc != null){
LatLng point = new LatLng(loc.getLatitude() , loc.getLongitude());
map.animateCamera(CameraUpdateFactory.newLatLngZoom(point,15));
}
}
This could cause a NullPointerException beacuse loc could be null.
OTHER SOLUTION
If you want to get only first time the coordinates, you should work in onLocationChanged, using a boolean variable to set first call.
Declare it CRDS_CALL = false;
#Override
public void onLocationChanged(Location location) {
if (mMapLocationListener != null) {
mMapLocationListener.onLocationChanged(location);
if(!CRDS_CALL){
LatLng point = new LatLng(location.getLatitude(), location.getLognitude());
map.animateCamera(CameraUpdateFactory.newLatLngZoom(point,15));
CRDS_CALL = true;
}
}
}
In this answer i use map, but you have to use your mapFragment, but if you want to use it in other methods over onCreate, you have to declare outside of it.
Add this just before the onCreate
SupportMapFragment mapFragment;
And inside it, use it like follwing:
mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.mapView);
mapFragment.getMapAsync(this);
In that way, you can use mapFragment in other methods
Solution
The solution was to do everything #MikeKeepsOnShine said but remove the
Location loc = map.getMyLocation();
LatLng point = new LatLng(loc.getLatitude() , loc.getLongitude());
map.animateCamera(CameraUpdateFactory.newLatLngZoom(point,15));
part from onMapReady. Works absolutely perfectly now!
You should use this. And you should put this request in onMapReady() callback. Something like this:
#Override
public void onMapReady(GoogleMap map) {
map.setLocationSource(this);
map.setMyLocationEnabled(true);
map.setOnMyLocationButtonClickListener(this);
map.animateCamera(CameraUpdateFactory.zoomTo(14), 2000, null);
}
you have already done the new "MapAsync" way so you are one step forward to the result.
In order to zoom, you can use the animateCamera method for googleMap object to zoom to a specific location:
https://developers.google.com/android/reference/com/google/android/gms/maps/GoogleMap.html#animateCamera(com.google.android.gms.maps.CameraUpdate)
for example, a zoomin:
map.animateCamera(CameraUpdateFactory.zoomIn());
or if you already know "where":
map.animateCamera(CameraUpdateFactory.newLatLngZoom(knownlocation,17));
and you will be very close to the location.
If you need to do at the first "locationUpdate" you should keep a flag to false and set to true the first time you receive a location, at that time you perform the zoom to location.
EDIT: If you want to zoom only the first time, where you receive location updates (which is not really clear from your code), you can do, assuming you have a class variable like:
private boolean FIRST_TIME = true;
the following code:
if(FIRST_TIME){
myMap.animateCamera(CameraUpdateFactory.newLatLngZoom(location,17));
FIRST_TIME = false;
}
The best option is to remove the listener but it seems you use location updates as "blue dot" updates.
What worked for me was:
mMap.setMinZoomPreference(6.0f);
mMap.setMaxZoomPreference(20.0f);
You can modify the zoom preference values to your taste.

google api client callback is never called

I am trying to get the last known location using google services API, but after I build the GoogleApiClient, no callback method is ever fired.
My activity looks like that :
public class MainActivity extends Activity implements FragmentObserver, SessionSpotListObserver,
ConnectionCallbacks, OnConnectionFailedListener{
//Objects used for the location API
private Location mLastLocation;
private GoogleApiClient mGoogleApiClient;
// Request code to use when launching the resolution activity
private static final int REQUEST_RESOLVE_ERROR = 1001;
// Unique tag for the error dialog fragment
private static final String DIALOG_ERROR = "dialog_error";
// Bool to track whether the app is already resolving an error
private boolean mResolvingError = false;
public static final String STATE_RESOLVING_ERROR = "resolving_state";
//Request code to use when launching the activity to fix the connection to google API
private static final int REQUEST_SOLVE_CONNEXION = 999;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//We make sure that google play service is available on the device
if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(this) == ConnectionResult.SUCCESS){
//We get a connection to the Google Play Service API to get the location of the user
buildGoogleApiClient();
}
else {
GooglePlayServicesUtil.getErrorDialog(GooglePlayServicesUtil.isGooglePlayServicesAvailable(this),
this,
REQUEST_SOLVE_CONNEXION);
}
}
#Override
public void onConnectionFailed(ConnectionResult result) {
if (mResolvingError) {
// Already attempting to resolve an error.
return;
} else if (result.hasResolution()) {
try {
mResolvingError = true;
result.startResolutionForResult(this, REQUEST_RESOLVE_ERROR);
} catch (SendIntentException e) {
// There was an error with the resolution intent. Try again.
mGoogleApiClient.connect();
}
} else {
// Show dialog using GooglePlayServicesUtil.getErrorDialog()
showErrorDialog(result.getErrorCode());
mResolvingError = true;
}
}
// The rest of this code is all about building the error dialog
/* Creates a dialog for an error message */
private void showErrorDialog(int errorCode) {
// Create a fragment for the error dialog
ErrorDialogFragment dialogFragment = new ErrorDialogFragment();
// Pass the error that should be displayed
Bundle args = new Bundle();
args.putInt(DIALOG_ERROR, errorCode);
dialogFragment.setArguments(args);
dialogFragment.show(getFragmentManager(), "errordialog");
}
/* Called from ErrorDialogFragment when the dialog is dismissed. */
public void onDialogDismissed() {
mResolvingError = false;
}
/* A fragment to display an error dialog */
public static class ErrorDialogFragment extends DialogFragment {
public ErrorDialogFragment() { }
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Get the error code and retrieve the appropriate dialog
int errorCode = this.getArguments().getInt(DIALOG_ERROR);
return GooglePlayServicesUtil.getErrorDialog(errorCode,
this.getActivity(), REQUEST_RESOLVE_ERROR);
}
#Override
public void onDismiss(DialogInterface dialog) {
((MainActivity)getActivity()).onDialogDismissed();
}
}
#Override
public void onConnected(Bundle arg0) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
Log.d("API Connection", "The API has connected and the last location is :" + mLastLocation);
if (mLastLocation != null) {
}
}
#Override
public void onConnectionSuspended(int arg0) {
// TODO Auto-generated method stub
}
/**
* Creates the connexion to the Google API. Once the API is connected, the
* onConnected method is called.
*/
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
I placed breakpoints on all callback methods, that is how I know that none is called.
Because at this stage I am not using Google Map Api, I did not register my app to get a key. Do I need to do that even if I just get the location ?
Don't hesitate to tell me if you need more info.
Thank you all.
You never call mGoogleApiClient.connect() after building your GoogleApiClient. Your onCreate() should instead be:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
buildGoogleApiClient();
mGoogleApiClient.connect();
}
Note that there is no need to call GooglePlayServicesUtil.isGooglePlayServicesAvailable() if you are using GoogleApiClient as connect() includes that check as well.
consider calling onLocationChanged() and passing it's Location parameter to mLastLocation for continuous location update when the user location changes. Also you might want to reduce the drain on your battery by setting LocationRequest() interval and distance to a small value.

Categories

Resources