Guest User

android code error with Google Map Markers

a guest
Aug 30th, 2016
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.05 KB | None | 0 0
  1. package com.test.logintest;
  2.  
  3. import android.Manifest;
  4. import android.content.pm.PackageManager;
  5. import android.location.Location;
  6. import android.os.Build;
  7. import android.support.v4.app.ActivityCompat;
  8. import android.support.v4.app.FragmentActivity;
  9. import android.os.Bundle;
  10. import android.support.v4.content.ContextCompat;
  11. import android.util.Log;
  12. import android.widget.Toast;
  13.  
  14. import com.google.android.gms.common.ConnectionResult;
  15. import com.google.android.gms.common.api.GoogleApiClient;
  16. import com.google.android.gms.location.LocationListener;
  17. import com.google.android.gms.location.LocationRequest;
  18. import com.google.android.gms.location.LocationServices;
  19. import com.google.android.gms.maps.CameraUpdateFactory;
  20. import com.google.android.gms.maps.GoogleMap;
  21. import com.google.android.gms.maps.OnMapReadyCallback;
  22. import com.google.android.gms.maps.SupportMapFragment;
  23. import com.google.android.gms.maps.model.BitmapDescriptorFactory;
  24. import com.google.android.gms.maps.model.LatLng;
  25. import com.google.android.gms.maps.model.Marker;
  26. import com.google.android.gms.maps.model.MarkerOptions;
  27.  
  28.  
  29. public class MapsActivity extends FragmentActivity
  30.         implements OnMapReadyCallback,
  31.                     GoogleApiClient.ConnectionCallbacks,
  32.                     GoogleApiClient.OnConnectionFailedListener,
  33.                     LocationListener{
  34.  
  35.     private GoogleMap mGoogleMap;
  36.     private SupportMapFragment mapFrag;
  37.     private LocationRequest mLocationRequest;
  38.     private GoogleApiClient mGoogleApiClient;
  39.     protected Location mLastLocation;
  40.     private Marker mCurrLocationMarker;
  41.  
  42.     @Override
  43.     protected void onCreate(Bundle savedInstanceState)
  44.     {
  45.         super.onCreate(savedInstanceState);
  46.         setContentView(R.layout.activity_maps);
  47.         Log.v("Creation","Complete");
  48.  
  49.         if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  50.             Log.v("Permissions","Checking");
  51.             checkLocationPermission();
  52.             Log.v("Permissions","Complete");
  53.         }
  54.  
  55.         mapFrag = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
  56.         mapFrag.getMapAsync(this);
  57.     }
  58.  
  59.     @Override
  60.     public void onPause() {
  61.         super.onPause();
  62.  
  63.         //stop location updates when Activity is no longer active
  64.         if (mGoogleApiClient != null) {
  65.             LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
  66.         }
  67.     }
  68.  
  69.     @Override
  70.     public void onMapReady(GoogleMap googleMap)
  71.     {
  72.         Log.v("Map","Ready");
  73.         mGoogleMap=googleMap;
  74.         mGoogleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
  75.  
  76.         //Initialize Google Play Services
  77.         if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  78.             if (ContextCompat.checkSelfPermission(this,
  79.                     Manifest.permission.ACCESS_FINE_LOCATION)
  80.                     == PackageManager.PERMISSION_GRANTED) {
  81.                 buildGoogleApiClient();
  82.                 mGoogleMap.setMyLocationEnabled(true);
  83.             }
  84.         }
  85.         else {
  86.             buildGoogleApiClient();
  87.             mGoogleMap.setMyLocationEnabled(true);
  88.         }
  89.     }
  90.  
  91.     protected synchronized void buildGoogleApiClient() {
  92.         mGoogleApiClient = new GoogleApiClient.Builder(this)
  93.                 .addConnectionCallbacks(this)
  94.                 .addOnConnectionFailedListener(this)
  95.                 .addApi(LocationServices.API)
  96.                 .build();
  97.         mGoogleApiClient.connect();
  98.     }
  99.  
  100.     @Override
  101.     public void onConnected(Bundle bundle) {
  102.         Log.v("Map","Connected");
  103.         mLocationRequest = new LocationRequest();
  104.         mLocationRequest.setInterval(1000);
  105.         mLocationRequest.setFastestInterval(1000);
  106.         mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
  107.         if (ContextCompat.checkSelfPermission(this,
  108.                 Manifest.permission.ACCESS_FINE_LOCATION)
  109.                 == PackageManager.PERMISSION_GRANTED) {
  110.             LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
  111.         }
  112.     }
  113.  
  114.     @Override
  115.     public void onConnectionSuspended(int i) {}
  116.  
  117.     @Override
  118.     public void onConnectionFailed(ConnectionResult connectionResult) {}
  119.  
  120.     @Override
  121.     public void onLocationChanged(Location location)
  122.     {
  123.  
  124.         Log.v("Location","Changed");
  125.         mLastLocation = location;
  126.         if (mCurrLocationMarker != null) {
  127.             mCurrLocationMarker.remove();
  128.         }
  129.  
  130.         //Place current location marker
  131.         LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
  132.         MarkerOptions markerOptions = new MarkerOptions();
  133.         markerOptions.position(latLng);
  134.         markerOptions.title("Current Position");
  135.         markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
  136.         mCurrLocationMarker = mGoogleMap.addMarker(markerOptions);
  137.  
  138.         //move map camera
  139.         mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
  140.         mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(11));
  141.  
  142.         //stop location updates
  143.         if (mGoogleApiClient != null) {
  144.             LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
  145.         }
  146.  
  147.     }
  148.  
  149.     public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;
  150.     public boolean checkLocationPermission(){
  151.         if (ContextCompat.checkSelfPermission(this,
  152.                 Manifest.permission.ACCESS_FINE_LOCATION)
  153.                 != PackageManager.PERMISSION_GRANTED) {
  154.  
  155.             // Should we show an explanation?
  156.             if (ActivityCompat.shouldShowRequestPermissionRationale(this,
  157.                     Manifest.permission.ACCESS_FINE_LOCATION)) {
  158.  
  159.                 // Show an expanation to the user *asynchronously* -- don't block
  160.                 // this thread waiting for the user's response! After the user
  161.                 // sees the explanation, try again to request the permission.
  162.  
  163.                 //Prompt the user once explanation has been shown
  164.                 ActivityCompat.requestPermissions(this,
  165.                         new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
  166.                         MY_PERMISSIONS_REQUEST_LOCATION);
  167.  
  168.  
  169.             } else {
  170.                 // No explanation needed, we can request the permission.
  171.                 ActivityCompat.requestPermissions(this,
  172.                         new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
  173.                         MY_PERMISSIONS_REQUEST_LOCATION);
  174.             }
  175.             return false;
  176.         } else {
  177.             return true;
  178.         }
  179.     }
  180.  
  181.     @Override
  182.     public void onRequestPermissionsResult(int requestCode,
  183.                                            String permissions[], int[] grantResults) {
  184.         switch (requestCode) {
  185.             case MY_PERMISSIONS_REQUEST_LOCATION: {
  186.                 // If request is cancelled, the result arrays are empty.
  187.                 if (grantResults.length > 0
  188.                         && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  189.  
  190.                     // permission was granted, yay! Do the
  191.                     //task you need to do.
  192.                     if (ContextCompat.checkSelfPermission(this,
  193.                             Manifest.permission.ACCESS_FINE_LOCATION)
  194.                             == PackageManager.PERMISSION_GRANTED) {
  195.  
  196.                         if (mGoogleApiClient == null) {
  197.                             buildGoogleApiClient();
  198.                         }
  199.                         mGoogleMap.setMyLocationEnabled(true);
  200.                     }
  201.  
  202.                 } else {
  203.  
  204.                     // permission denied, boo! Disable the
  205.                     // functionality that depends on this permission.
  206.                     Toast.makeText(this, "permission denied", Toast.LENGTH_LONG).show();
  207.                 }
  208.                 return;
  209.             }
  210.  
  211.             // other 'case' lines to check for other
  212.             // permissions this app might request
  213.         }
  214.     }
  215.  
  216. }
Add Comment
Please, Sign In to add comment