|
1 | 1 | package com.example.webapplicationwithspring; |
2 | | - |
| 2 | +import android.*; |
3 | 3 | import android.Manifest; |
4 | | -import android.app.Activity; |
5 | | -import android.app.AppComponentFactory; |
6 | | -import android.content.Context; |
7 | 4 | import android.content.pm.PackageManager; |
| 5 | +import android.location.Location; |
8 | 6 | import android.os.Bundle; |
9 | | -import android.util.Log; |
10 | | -import android.widget.Toast; |
| 7 | +import org.jetbrains.annotations.Nullable; |
11 | 8 |
|
12 | 9 | import androidx.annotation.NonNull; |
13 | | -import androidx.annotation.Nullable; |
14 | 10 | import androidx.appcompat.app.AppCompatActivity; |
15 | 11 | import androidx.core.app.ActivityCompat; |
16 | 12 | import androidx.core.content.ContextCompat; |
| 13 | + |
| 14 | +import android.util.Log; |
| 15 | +import android.widget.Toast; |
| 16 | + |
| 17 | +import com.google.android.gms.location.FusedLocationProviderClient; |
| 18 | +import com.google.android.gms.location.LocationServices; |
| 19 | +import com.google.android.gms.maps.CameraUpdateFactory; |
17 | 20 | import com.google.android.gms.maps.GoogleMap; |
18 | 21 | import com.google.android.gms.maps.OnMapReadyCallback; |
19 | 22 | import com.google.android.gms.maps.SupportMapFragment; |
| 23 | +import com.google.android.gms.maps.model.LatLng; |
| 24 | +import com.google.android.gms.tasks.OnCompleteListener; |
| 25 | +import com.google.android.gms.tasks.Task; |
| 26 | + |
| 27 | +/** |
| 28 | + * Created by User on 10/2/2017. |
| 29 | + */ |
| 30 | + |
| 31 | +public class MapActivity extends AppCompatActivity implements OnMapReadyCallback { |
20 | 32 |
|
21 | | -public class MapActivity extends AppCompatActivity implements OnMapReadyCallback{ |
| 33 | + @Override |
| 34 | + public void onMapReady(GoogleMap googleMap) { |
| 35 | + Toast.makeText(this, "Map is Ready", Toast.LENGTH_SHORT).show(); |
| 36 | + Log.d(TAG, "onMapReady: map is ready"); |
| 37 | + mMap = googleMap; |
| 38 | + |
| 39 | + if (mLocationPermissionsGranted) { |
| 40 | + getDeviceLocation(); |
| 41 | + |
| 42 | + if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) |
| 43 | + != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, |
| 44 | + Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { |
| 45 | + return; |
| 46 | + } |
| 47 | + mMap.setMyLocationEnabled(true); |
| 48 | + mMap.getUiSettings().setMyLocationButtonEnabled(false); |
| 49 | + |
| 50 | + } |
| 51 | + } |
22 | 52 |
|
23 | 53 | private static final String TAG = "MapActivity"; |
| 54 | + |
24 | 55 | private static final String FINE_LOCATION = Manifest.permission.ACCESS_FINE_LOCATION; |
25 | | - private static final String COARSE_LOCATION = Manifest.permission.ACCESS_COARSE_LOCATION; |
26 | | - private Boolean mLocationPermissionGranted = false; |
27 | | - private static final int L_PERMISSION_REQUEST_CODE = 1234; |
28 | | - private GoogleMap gMap; |
| 56 | + private static final String COURSE_LOCATION = Manifest.permission.ACCESS_COARSE_LOCATION; |
| 57 | + private static final int LOCATION_PERMISSION_REQUEST_CODE = 1234; |
| 58 | + private static final float DEFAULT_ZOOM = 15f; |
| 59 | + |
| 60 | + //vars |
| 61 | + private Boolean mLocationPermissionsGranted = false; |
| 62 | + private GoogleMap mMap; |
| 63 | + private FusedLocationProviderClient mFusedLocationProviderClient; |
29 | 64 |
|
30 | 65 | @Override |
31 | | - public void onCreate(@Nullable Bundle savedInstanceState){ |
| 66 | + protected void onCreate(@Nullable Bundle savedInstanceState) { |
32 | 67 | super.onCreate(savedInstanceState); |
33 | 68 | setContentView(R.layout.activity_map); |
| 69 | + |
34 | 70 | getLocationPermission(); |
35 | 71 | } |
36 | 72 |
|
| 73 | + private void getDeviceLocation(){ |
| 74 | + Log.d(TAG, "getDeviceLocation: getting the devices current location"); |
| 75 | + |
| 76 | + mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this); |
| 77 | + |
| 78 | + try{ |
| 79 | + if(mLocationPermissionsGranted){ |
| 80 | + |
| 81 | + final Task location = mFusedLocationProviderClient.getLastLocation(); |
| 82 | + location.addOnCompleteListener(new OnCompleteListener() { |
| 83 | + @Override |
| 84 | + public void onComplete(@NonNull Task task) { |
| 85 | + if(task.isSuccessful()){ |
| 86 | + Log.d(TAG, "onComplete: found location!"); |
| 87 | + Location currentLocation = (Location) task.getResult(); |
| 88 | + |
| 89 | + moveCamera(new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude()), |
| 90 | + DEFAULT_ZOOM); |
| 91 | + |
| 92 | + }else{ |
| 93 | + Log.d(TAG, "onComplete: current location is null"); |
| 94 | + Toast.makeText(MapActivity.this, "unable to get current location", Toast.LENGTH_SHORT).show(); |
| 95 | + } |
| 96 | + } |
| 97 | + }); |
| 98 | + } |
| 99 | + }catch (SecurityException e){ |
| 100 | + Log.e(TAG, "getDeviceLocation: SecurityException: " + e.getMessage() ); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + private void moveCamera(LatLng latLng, float zoom){ |
| 105 | + Log.d(TAG, "moveCamera: moving the camera to: lat: " + latLng.latitude + ", lng: " + latLng.longitude ); |
| 106 | + mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, zoom)); |
| 107 | + } |
| 108 | + |
37 | 109 | private void initMap(){ |
38 | | - Log.d(TAG, "initMAp : initialization"); |
| 110 | + Log.d(TAG, "initMap: initializing map"); |
39 | 111 | SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); |
| 112 | + |
40 | 113 | mapFragment.getMapAsync(MapActivity.this); |
41 | 114 | } |
42 | 115 |
|
43 | 116 | private void getLocationPermission(){ |
44 | | - Log.d(TAG, "getLocation permission : gettionf location permissions"); |
45 | | - String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION, |
46 | | - Manifest.permission.ACCESS_FINE_LOCATION}; |
47 | | - |
48 | | - if(ContextCompat.checkSelfPermission(this.getApplicationContext(), FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){ |
49 | | - if(ContextCompat.checkSelfPermission(this.getApplicationContext(), COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) |
50 | | - mLocationPermissionGranted = true; |
51 | | - else{ |
52 | | - ActivityCompat.requestPermissions(this, permissions, L_PERMISSION_REQUEST_CODE); |
| 117 | + Log.d(TAG, "getLocationPermission: getting location permissions"); |
| 118 | + String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION, |
| 119 | + Manifest.permission.ACCESS_COARSE_LOCATION}; |
| 120 | + |
| 121 | + if(ContextCompat.checkSelfPermission(this.getApplicationContext(), |
| 122 | + FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){ |
| 123 | + if(ContextCompat.checkSelfPermission(this.getApplicationContext(), |
| 124 | + COURSE_LOCATION) == PackageManager.PERMISSION_GRANTED){ |
| 125 | + mLocationPermissionsGranted = true; |
| 126 | + initMap(); |
| 127 | + }else{ |
| 128 | + ActivityCompat.requestPermissions(this, |
| 129 | + permissions, |
| 130 | + LOCATION_PERMISSION_REQUEST_CODE); |
53 | 131 | } |
| 132 | + }else{ |
| 133 | + ActivityCompat.requestPermissions(this, |
| 134 | + permissions, |
| 135 | + LOCATION_PERMISSION_REQUEST_CODE); |
54 | 136 | } |
55 | 137 | } |
56 | 138 |
|
57 | 139 | @Override |
58 | 140 | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { |
59 | | - Log.d(TAG, "onRequestPermissionResult : called"); |
60 | | - mLocationPermissionGranted = false; |
61 | | - switch (requestCode){ |
62 | | - case L_PERMISSION_REQUEST_CODE: { |
63 | | - if(grantResults.length>0){ |
64 | | - for(int i =0; i < grantResults.length; i++){ |
| 141 | + Log.d(TAG, "onRequestPermissionsResult: called."); |
| 142 | + mLocationPermissionsGranted = false; |
| 143 | + |
| 144 | + switch(requestCode){ |
| 145 | + case LOCATION_PERMISSION_REQUEST_CODE:{ |
| 146 | + if(grantResults.length > 0){ |
| 147 | + for(int i = 0; i < grantResults.length; i++){ |
65 | 148 | if(grantResults[i] != PackageManager.PERMISSION_GRANTED){ |
66 | | - mLocationPermissionGranted = false; |
67 | | - Log.d(TAG, "onRequestPermissionResult : remission failed"); |
| 149 | + mLocationPermissionsGranted = false; |
| 150 | + Log.d(TAG, "onRequestPermissionsResult: permission failed"); |
68 | 151 | return; |
69 | 152 | } |
70 | 153 | } |
71 | | - Log.d(TAG, "onRequestPermissionResult : remission fgranted"); |
72 | | - mLocationPermissionGranted = true; |
| 154 | + Log.d(TAG, "onRequestPermissionsResult: permission granted"); |
| 155 | + mLocationPermissionsGranted = true; |
| 156 | + //initialize our map |
73 | 157 | initMap(); |
74 | 158 | } |
75 | 159 | } |
76 | 160 | } |
77 | 161 | } |
78 | 162 |
|
79 | | - @Override |
80 | | - public void onMapReady(GoogleMap googleMap) { |
81 | | - Toast.makeText(this, "Map is ready", Toast.LENGTH_SHORT).show(); |
82 | | - Log.d(TAG, "On map: MAP is ready here"); |
83 | | - gMap = googleMap; |
84 | | - } |
| 163 | + |
85 | 164 | } |
| 165 | + |
0 commit comments