1
1
package com .pulkit4tech .privy ;
2
2
3
+ import android .content .Context ;
4
+ import android .content .pm .PackageManager ;
5
+ import android .location .Criteria ;
6
+ import android .location .Location ;
7
+ import android .location .LocationManager ;
3
8
import android .os .Bundle ;
9
+ import android .support .v4 .app .ActivityCompat ;
4
10
import android .support .v7 .app .ActionBarActivity ;
11
+ import android .util .Log ;
12
+ import android .widget .Toast ;
5
13
6
14
import com .google .android .gms .maps .CameraUpdateFactory ;
7
15
import com .google .android .gms .maps .GoogleMap ;
8
16
import com .google .android .gms .maps .OnMapReadyCallback ;
9
17
import com .google .android .gms .maps .SupportMapFragment ;
10
18
import com .google .android .gms .maps .model .BitmapDescriptorFactory ;
11
19
import com .google .android .gms .maps .model .LatLng ;
20
+ import com .google .android .gms .maps .model .Marker ;
12
21
import com .google .android .gms .maps .model .MarkerOptions ;
22
+ import com .pulkit4tech .privy .Utilities .LocationServices ;
23
+ import com .pulkit4tech .privy .data .LocationData ;
24
+
25
+ import static com .pulkit4tech .privy .MainActivity .DEBUG ;
13
26
14
27
public class PrivyMapsActivity extends ActionBarActivity implements OnMapReadyCallback {
15
28
16
29
private GoogleMap mMap ;
30
+ private Context mContext ;
31
+ private Marker myLocationMarker ;
32
+
33
+ // My Location
34
+ private LocationData myLocationData ;
17
35
18
36
@ Override
19
37
protected void onCreate (Bundle savedInstanceState ) {
20
38
super .onCreate (savedInstanceState );
21
39
setContentView (R .layout .activity_privy_maps );
40
+
41
+ mContext = this ;
42
+
22
43
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
23
44
SupportMapFragment mapFragment = (SupportMapFragment ) getSupportFragmentManager ()
24
45
.findFragmentById (R .id .privyMapActivity );
@@ -38,13 +59,68 @@ protected void onCreate(Bundle savedInstanceState) {
38
59
@ Override
39
60
public void onMapReady (GoogleMap googleMap ) {
40
61
mMap = googleMap ;
62
+ setUpMapInfo ();
63
+ addMarkers ();
64
+ }
65
+
66
+
67
+ private void addMarkers () {
41
68
42
69
// Add a test marker in Delhi and move the camera
43
70
LatLng delhi = new LatLng (28.633011 , 77.219373 );
44
- mMap .addMarker (new MarkerOptions ().position (delhi ).anchor (.5f ,.5f ).title ("Marker in Home" ));
45
- mMap .moveCamera (CameraUpdateFactory .newLatLngZoom (delhi ,10 .0f ));
71
+ mMap .addMarker (new MarkerOptions ().position (delhi ).anchor (.5f , .5f ).title ("Marker in Home" ));
72
+ mMap .moveCamera (CameraUpdateFactory .newLatLngZoom (delhi , 15 .0f ));
46
73
47
74
LatLng delhi2 = new LatLng (28.633511 , 77.219444 );
48
- mMap .addMarker (new MarkerOptions ().position (delhi2 ).anchor (.5f ,.5f ).title ("Test Marker in Home2" ).icon (BitmapDescriptorFactory .fromResource (R .mipmap .ic_launcher )));
75
+ mMap .addMarker (new MarkerOptions ().position (delhi2 ).anchor (.5f , .5f ).title ("Test Marker in Home2" ).icon (BitmapDescriptorFactory .fromResource (R .mipmap .ic_launcher )));
76
+
77
+ }
78
+
79
+ private void setUpMapInfo () {
80
+
81
+ if (!checkLocationEnabledPermission ())
82
+ return ;
83
+
84
+
85
+ getMyCurrentLocation ();
86
+
87
+ }
88
+
89
+ private void getMyCurrentLocation (){
90
+ mMap .setOnMyLocationButtonClickListener (new GoogleMap .OnMyLocationButtonClickListener () {
91
+ @ Override
92
+ public boolean onMyLocationButtonClick () {
93
+ LocationServices locationService = new LocationServices (mContext );
94
+ myLocationData = locationService .getCurrentLocation ();
95
+ if (myLocationData !=null ) {
96
+
97
+ // checking for previous marker and if present, replacing it with new marker
98
+ if (myLocationMarker !=null ){
99
+ myLocationMarker .remove ();
100
+ }
101
+
102
+ myLocationMarker = mMap .addMarker (new MarkerOptions ().position (myLocationData .getLatLng ()).title ("My Location" ));
103
+ mMap .moveCamera (CameraUpdateFactory .newLatLngZoom (myLocationData .getLatLng (), 15.0f ));
104
+ Log .d (DEBUG , myLocationData .getLatLng ().toString ());
105
+ }
106
+ return true ;
107
+ }
108
+ });
109
+ }
110
+
111
+ private boolean checkLocationEnabledPermission () {
112
+ if (ActivityCompat .checkSelfPermission (this , android .Manifest .permission .ACCESS_FINE_LOCATION ) != PackageManager .PERMISSION_GRANTED && ActivityCompat .checkSelfPermission (this , android .Manifest .permission .ACCESS_COARSE_LOCATION ) != PackageManager .PERMISSION_GRANTED ) {
113
+ // TODO: Consider calling
114
+ // ActivityCompat#requestPermissions
115
+ // here to request the missing permissions, and then overriding
116
+ // public void onRequestPermissionsResult(int requestCode, String[] permissions,
117
+ // int[] grantResults)
118
+ // to handle the case where the user grants the permission. See the documentation
119
+ // for ActivityCompat#requestPermissions for more details.
120
+
121
+ return false ;
122
+ }
123
+ mMap .setMyLocationEnabled (true );
124
+ return true ;
49
125
}
50
126
}
0 commit comments