17
17
import android .widget .ImageView ;
18
18
import android .widget .LinearLayout ;
19
19
20
+ import com .google .android .gms .maps .CameraUpdate ;
20
21
import com .google .android .gms .maps .CameraUpdateFactory ;
21
22
import com .google .android .gms .maps .GoogleMap ;
22
23
import com .google .android .gms .maps .OnMapReadyCallback ;
23
24
import com .google .android .gms .maps .SupportMapFragment ;
24
25
import com .google .android .gms .maps .model .BitmapDescriptorFactory ;
25
26
import com .google .android .gms .maps .model .LatLng ;
27
+ import com .google .android .gms .maps .model .LatLngBounds ;
26
28
import com .google .android .gms .maps .model .Marker ;
27
29
import com .google .android .gms .maps .model .MarkerOptions ;
28
30
import com .google .android .gms .tasks .OnCompleteListener ;
@@ -44,6 +46,7 @@ public class HomeScreen extends FragmentActivity implements OnMapReadyCallback{
44
46
int COUNT_THRESHOLD = 10 ;
45
47
Location loc1 ;
46
48
FirebaseFirestore firestore ;
49
+ MarkerOptions marker ;
47
50
private Double currentUserLatitude , currentUserLongitude ;
48
51
49
52
List <Location > closeUsersLocations = new ArrayList <>();
@@ -61,15 +64,14 @@ protected void onCreate(Bundle savedInstanceState) {
61
64
62
65
loc1 = new Location ("" );
63
66
64
-
65
67
helpImageView = findViewById (R .id .help_imageView );
66
68
layoutHide = findViewById (R .id .layout_tohide );
67
69
layoutShow = findViewById (R .id .layout_toShow );
68
70
69
71
layoutShow .setVisibility (View .GONE );
70
72
71
73
72
- loadUsersLocation ();
74
+
73
75
74
76
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
75
77
SupportMapFragment mapFragment = (SupportMapFragment ) getSupportFragmentManager ()
@@ -100,19 +102,33 @@ public void onLocationChanged(Location location) {
100
102
String result = addresses .get (0 ).getLocality ()+":" ;
101
103
result += addresses .get (0 ).getCountryName ();
102
104
LatLng latLng = new LatLng (latitude , longitude );
105
+ loadUsersLocation ();
106
+ marker = new MarkerOptions ().position (latLng );
103
107
mMap .clear ();
104
- mMap .addMarker (new MarkerOptions (). position ( latLng ). title ( result ) );
108
+ mMap .addMarker (marker );
105
109
mMap .setMaxZoomPreference (20 );
106
110
mMap .moveCamera (CameraUpdateFactory .newLatLngZoom (latLng , 12.0f ));
107
111
112
+ LatLngBounds .Builder builder = new LatLngBounds .Builder ();
113
+
114
+
115
+ Log .i ("xxx Size" ,"" +closeUsersLocations .size ());
108
116
for (int i = 0 ; i < closeUsersLocations .size () ; i ++) {
117
+ builder .include (marker .getPosition ());
109
118
LatLng latLng1 = new LatLng (closeUsersLocations .get (i )
110
119
.getLatitude (), closeUsersLocations .get (i ).getLongitude ());
111
120
mMap .addMarker (new MarkerOptions ().position (latLng1 ).title ("Cool" ));
112
121
}
113
122
123
+ if (closeUsersLocations .size () > 0 ) {
124
+ LatLngBounds bounds = builder .build ();
114
125
126
+ int padding = 0 ; // offset from edges of the map in pixels
127
+ CameraUpdate cu = CameraUpdateFactory .newLatLngBounds (bounds , padding );
115
128
129
+ mMap .moveCamera (cu );
130
+
131
+ }
116
132
117
133
} catch (IOException e ) {
118
134
e .printStackTrace ();
@@ -154,6 +170,7 @@ public void onProviderDisabled(String provider) {
154
170
public void onMapReady (GoogleMap googleMap ) {
155
171
mMap = googleMap ;
156
172
173
+
157
174
}
158
175
159
176
public void requestHelp (View view ){
@@ -175,11 +192,11 @@ private void loadUsersLocation(){
175
192
.addOnCompleteListener (new OnCompleteListener <QuerySnapshot >() {
176
193
@ Override
177
194
public void onComplete (@ NonNull Task <QuerySnapshot > task ) {
178
- closeUsersLocations .clear ();
195
+ // closeUsersLocations.clear();
179
196
if (task .isSuccessful ()) {
180
197
for (QueryDocumentSnapshot document : task .getResult ()) {
181
198
182
- // Log.d("xxx", document.getId() + " => " + document.getData().get("loc"));
199
+ Log .d ("xxx" , document .getId () + " => " + document .getData ().get ("loc" ));
183
200
Map <String ,Object > map = (HashMap ) document .getData ().get ("loc" );
184
201
Double latitude = (Double ) map .get ("_latitude" );
185
202
Double longitude = (Double ) map .get ("_longitude" );
0 commit comments