Advertisement
Mirnijat

Map Demo

Apr 4th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.35 KB | None | 0 0
  1. package com.jabbarli.mirnicat.mapdemo;
  2.  
  3. import android.Manifest;
  4. import android.content.pm.PackageManager;
  5. import android.os.Build;
  6. import android.support.annotation.NonNull;
  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.  
  12. import com.google.android.gms.maps.CameraUpdateFactory;
  13. import com.google.android.gms.maps.GoogleMap;
  14. import com.google.android.gms.maps.OnMapReadyCallback;
  15. import com.google.android.gms.maps.SupportMapFragment;
  16. import com.google.android.gms.maps.model.LatLng;
  17. import com.google.android.gms.maps.model.MarkerOptions;
  18.  
  19. public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
  20.  
  21.     private GoogleMap mMap;
  22.  
  23.     @Override
  24.     public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[]grantResults) {
  25.         super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  26.  
  27.         if (requestCode == 1){
  28.  
  29.             if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
  30.  
  31.                 if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
  32.  
  33.                     mMap.setMyLocationEnabled(true);
  34.  
  35.                 }
  36.  
  37.             }
  38.  
  39.         }
  40.  
  41.     }
  42.  
  43.     @Override
  44.     protected void onCreate(Bundle savedInstanceState) {
  45.         super.onCreate(savedInstanceState);
  46.         setContentView(R.layout.activity_maps);
  47.         // Obtain the SupportMapFragment and get notified when the map is ready to be used.
  48.         SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
  49.                 .findFragmentById(R.id.map);
  50.         mapFragment.getMapAsync(this);
  51.     }
  52.  
  53.  
  54.     /**
  55.      * Manipulates the map once available.
  56.      * This callback is triggered when the map is ready to be used.
  57.      * This is where we can add markers or lines, add listeners or move the camera. In this case,
  58.      * we just add a marker near Sydney, Australia.
  59.      * If Google Play services is not installed on the device, the user will be prompted to install
  60.      * it inside the SupportMapFragment. This method will only be triggered once the user has
  61.      * installed Google Play services and returned to the app.
  62.      */
  63.     @Override
  64.     public void onMapReady(GoogleMap googleMap) {
  65.         mMap = googleMap;
  66.         //mMap.setMyLocationEnabled(true); in if enable button
  67.         //mMap.getUiSettings().setMyLocationButtonEnabled(true);
  68.  
  69.         if (Build.VERSION.SDK_INT >= 23) {
  70.             if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
  71.                     != PackageManager.PERMISSION_GRANTED) {
  72.  
  73.                 ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
  74.  
  75.             } else {
  76.  
  77.                 mMap.setMyLocationEnabled(true);
  78.             }
  79.         }
  80.         else {
  81.  
  82.             mMap.setMyLocationEnabled(true);
  83.  
  84.         }
  85.         mMap.setOnMyLocationButtonClickListener(new GoogleMap.OnMyLocationButtonClickListener() {
  86.             @Override
  87.             public boolean onMyLocationButtonClick() {
  88.  
  89.                 //on click listener
  90.  
  91.                 return false;
  92.             }
  93.         });
  94.  
  95.  
  96.  
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement