Wednesday 20 April 2016

Lecture 3: Maps and Navigation

Adding Maps

Adding maps is simple with Android studio.

Step 1






Step 2: Generate maps api key. 

Copy the link the starts like https://console.developers.google.com/flows/enableapi?apiid=maps_android_backend&keyType=CLIENT_SIDE_ANDROID

and paste in browser. 


you will get an api as shown in screen shot . Paste in xml file and run project . 

Google Fused Location API provider

Follow steps below to add Fused Location API.

Step 1: Add Location project to gradle and sync

compile 'com.google.android.gms:play-services-location:8.4.0'
compile 'com.google.android.gms:play-services-maps:8.4.0'

Step 2:Add Permissions to Manifest

  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
Step 3:Implement Interfaces

implements
        ConnectionCallbacks, OnConnectionFailedListener{
Step 4: Implement Methods 

@Overridepublic void onConnected(Bundle connectionHint) {
    

}

@Overridepublic void onConnectionSuspended(int i) {
}

@Overridepublic void onConnectionFailed(ConnectionResult connectionResult) {

}

Step 5: Connect with google api

if (mGoogleApiClient == null) {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
}

protected void onStart() {
    mGoogleApiClient.connect();
    super.onStart();
}

protected void onStop() {
    mGoogleApiClient.disconnect();
    super.onStop();
}

Step 6: On Successful Connection start tracking location in on connected method of implemented interface

@Overridepublic void onConnected(Bundle connectionHint) {
    PublicMethods.writeTextToFile(context, "onConnected");
    
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
            mGoogleApiClient);
    if (mLastLocation != null) {

        Toast.makeText(context, mLastLocation.getLongitude() + " , " + mLastLocation.getLatitude(), Toast.LENGTH_LONG).show();
        createLocationRequest();
    }

}

protected void createLocationRequest() {
    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(10000);
    mLocationRequest.setFastestInterval(5000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    startLocationUpdates();
}

protected void startLocationUpdates() {
    
    LocationServices.FusedLocationApi.requestLocationUpdates(
            mGoogleApiClient, mLocationRequest, new LocationListener() {
                @Override                public void onLocationChanged(Location location) {
                    Toast.makeText(context, location.getLongitude() + " , " + location.getLatitude(), Toast.LENGTH_LONG).show();
                    LatLng sydney = new LatLng(location.getLatitude(), location.getLongitude());
                    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker is here"));
                    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
                }
            });
}


No comments:

Post a Comment