How To Refresh The Marker On The Map?
I have just 2 Markers on the Map 1- I want to show the bothe markers with max zoom as possible 2- I want after that refresh the markers position with a timer for example. I want to
Solution 1:
You need to call load_locations_from_server()
method after map ready which is in onMapReady()
method.
Read more at https://developers.google.com/maps/documentation/android-api/start
To set the new location of marker, you just need to use:
Marker.setPosition(LatLng latlng);
So you need to create two variables for the markers.
private Marker mMarkerA; private Marker mMarkerB;
Then in your onMapReady(), initialize them:
@OverridepublicvoidonMapReady(GoogleMap googleMap) {
mMap = googleMap;
...
mMarkerA = mMap.addMarker(newMarkerOptions().position(a_local).title("a")
.snippet("xxx")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_yellow))).showInfoWindow();
mMarkerB = mMap.addMarker(newMarkerOptions().position(b_local).title("b")
.snippet("yyy")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.markerb))).showInfoWindow();
...
}
Then, you can refresh them when you get the location from the server:
a_lat=respons.getString("a_lat");
a_lon=respons.getString("a_lon");
b_lat=respons.getString("b_lat");
b_lon=respons.getString("b_lon");
// refresh marker
mMarkerA.setPosition(new LatLng(a_lat, a_lon));
mMarkerA.setPosition(new LatLng(b_lat, b_lon));
Show Both Marker
To show both marker with extra padding, you can use LatLngBounds
like the following method:
privatevoidzoomToShowBothPoints(LatLng firstLatLng, LatLng secondLatLng) {
LatLngBoundsbounds=newLatLngBounds.Builder().include(firstLatLng).include(secondLatLng).build();
PointdisplaySize=newPoint();
getWindowManager().getDefaultDisplay().getSize(displaySize);
mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, displaySize.x - 20, displaySize.y - 20, 50));
}
Solution 2:
Try to store your marker on variables :
private Marker A;
private Marker B;
A = mMap.addMarker(new MarkerOptions().position(a_local).title("a")
.snippet("xxx")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_yellow))).showInfoWindow();
B = mMap.addMarker(new MarkerOptions().position(b_local).title("b")
.snippet("yyy")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.markerb)))
Then create a timer that will call your load_locations_from_server() method and refresh the marker and update the camera in the response callback function :
publicvoidonResponse(JSONObject response) {
try {
JSONArrayjsonArray= response.getJSONArray("locations");
for (inti=0; i < jsonArray.length(); i++) {
JSONObjectrespons= jsonArray.getJSONObject(i);
a_lat=respons.getString("a_lat");
a_lon=respons.getString("a_lon");
b_lat=respons.getString("b_lat");
b_lon=respons.getString("b_lon");
A.setLocation(LatLng(a_lat,a_lon));
B.setLocation(LatLng(b_lat,b_lon));
LatLngBounds.Builderbuilder=newLatLngBounds.Builder();
builder.include(A.getPosition());
builder.include(B.getPosition());
LatLngBoundsbounds= builder.build();
CameraUpdatecu= CameraUpdateFactory.newLatLngBounds(bounds, 0);
mMap..moveCamera(cu);
}
} catch (JSONException e) {
e.printStackTrace();
}
Post a Comment for "How To Refresh The Marker On The Map?"