I've got GoogleMap in my android app and what I want to do is to animate camera to be exacly above items in my cluster. It works when the map is first lunched but never again in this session. This is how I want to update my map view:
List<LatLng> pointsList = new ArrayList<LatLng>();
for(MyObject myObject: ObjectsList) {
pointsList.add(new LatLng(myObject.getLatitude(), myObject
.getLongitude()));
}
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (LatLng latLng : pointsList) {
builder.include(latLng);
}
LatLngBounds bounds = builder.build();
int padding = 100;
mCameraUpdate = CameraUpdateFactory.newLatLngBounds(bounds, padding);
mGoogleMap.animateCamera(mCameraUpdate);
I call this whenever I've got new ObjectList to set new boundries. But except first time it never works. I also can show how i put my objects into cluster:
mClusterManager = new ClusterManager<MyClusterItem>(mActivity, mGoogleMap);
mGoogleMap.setOnCameraChangeListener(mClusterManager);
mGoogleMap.setOnMarkerClickListener(mClusterManager);
mClusterManager.setRenderer(new MyClusterRenderer(mActivity, mGoogleMap, mClusterManager));
for (MyObject p : ObjectsList) {
MyClusterItem myClusterItem = new MyClusterItem(p);
mClusterManager.addItem(myClusterItem);
}
Can anybody explain me how to set camera to fit my objects and nothing else every time my objects list has changed?
Related
i am initiating direction api to draw polyline,calling asyncTask in oncreate, but its not getting called second time onwords even opening activity again and again. after removing app from recent apps and opening it again works for a single time.here is my code.
private void moveCamera(LatLng originLatLng, LatLng endLatLng) {
mMap.addMarker(new MarkerOptions().position(currentLatLng).title("Starting Location").icon(BitmapDescriptorFactory.fromResource(R.mipmap.pickup_location)));
HelperFunctions.addOverlay(currentLatLng, mMap, SpeedMediatorActivity.this);
if (endLatLng != null) {
mMap.addMarker(new MarkerOptions().position(destLatlng).title("Destination Location").icon(BitmapDescriptorFactory.fromResource(R.mipmap.pickup_location)));
LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(originLatLng);
builder.include(endLatLng);
LatLngBounds bounds = builder.build();
int padding = 50; // offset from edges of the map in pixels
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
mMap.animateCamera(cu);
String url = getDirectionsUrl(currentLatLng, destLatlng);
// Start downloading json data from Google Directions API
new DownloadTask().execute(url);
} else {
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(originLatLng, 16));
}
// new FetchURL(SpeedMediatorActivity.this).execute(getUrl(originLatLng, endLatLng, "driving"), "driving");
}
Can someone help me please.
I have cordinates from json and I want to show them all in map , here it shows markers but some of them not appear I want to set zoom to fit appearing all markers in map ,how can I do it ?
JSONArray postsJson = success.getJSONArray("now");
for (int i = 0; i < postsJson.length(); i++) {
Order_Start_Lat=c.getDouble("order_start_latitude");
Order_Start_Lng=c.getDouble("order_start_longitude");
MarkerOptions m = new MarkerOptions()
.title(client_name)
.position(new LatLng(Order_Start_Lat,Order_Start_Lng));
map.addMarker(m);
LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(new LatLng(Order_Start_Lat,Order_Start_Lng));
LatLngBounds bounds = builder.build();
map.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 20));
You should use the CameraUpdate class to do (probably) all programmatic map movements.
To do this, first calculate the bounds of all the markers like so:
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (Marker marker : markers) {
builder.include(marker.getPosition());
}
LatLngBounds bounds = builder.build();
Then obtain a movement description object by using the factory: CameraUpdateFactory:
int padding = 0; // offset from edges of the map in pixels
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
Finally move the map:
googleMap.moveCamera(cu);
I am working on google map where i am using google map api to add multiple markers on the google map for ATMs .It means i have multiple markers corresponding to ATMs in my google map.When i click on a marker,a polyline is drawn between my current location and marker.When i click on another marker ,a new polyline is drawn but the previous one is not deleted.Please find below my code:
#Override
protected void onPostExecute(String parseData) {
super.onPostExecute(parseData);
dialog.dismiss();
//map.clear();
ArrayList<LatLng> allpooints = CommonUtility.decodePoly(parseData);
if(prevPolyLine!=null) {
prevPolyLine.remove();
//map.clear();
Log.e(" ","polyline");
PolylineOptions options = new PolylineOptions();
options.width(7);
options.color(Color.BLUE);
options.geodesic(true);
options.visible(true);
for (LatLng temp : allpooints) {
options.add(temp);
}
prevPolyLine = map.addPolyline(options);
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (LatLng latLng : allpooints) {
builder.include(latLng);
}
final LatLngBounds bounds = builder.build();
//BOUND_PADDING is an int to specify padding of bound.. try 100.
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, 20);
map.animateCamera(cu);
}else{
Log.e("polylineTAG","----------------------");
PolylineOptions options = new PolylineOptions();
options.width(7);
options.color(Color.BLUE);
options.geodesic(true);
options.visible(true);
for (LatLng temp : allpooints) {
options.add(temp);
}
prevPolyLine = map.addPolyline(options);
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (LatLng latLng : allpooints) {
builder.include(latLng);
}
final LatLngBounds bounds = builder.build();
//BOUND_PADDING is an int to specify padding of bound.. try 100.
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, 20);
map.animateCamera(cu);
}
}
Screenshot
Please help me to fix the issue.I want to remove polyline drawn between my current location and previous marker,when i click on new marker in the map.
I am facing the problem that I need to update a map on the screen such that the all points from the route the user has taken is visible.
In the code below, I count the number of times I request a map update, but I noticed that sometimes the number of requests doesnt match the number of callbacks. So waiting for 'mapLoaded' to become 0 is not a good idea.
Therefore I have added a time limit of 10 seconds, but this is arbitrary and sometimes just not enough.
So, how can I know for sure that all map update has been completed?
private void adjustMapCompleteSO(LatLng from, LatLng to){//3.3.17 show all points for screenshot
double x1=(from.latitude+to.latitude)/2;
double x2=(from.longitude+to.longitude)/2;
LatLng del=new LatLng(x1,x2);
map.moveCamera(CameraUpdateFactory.newLatLng(del));
mapLoaded=0;
for(Polyline pol : allcrumbs){
List<LatLng> points = pol.getPoints();
for (LatLng point : points){
LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(point);
LatLngBounds bounds = builder.build();
int padding = 40; // offset from edges of the map in pixels
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
mapLoaded++;
map.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
public void onMapLoaded() {
mapLoaded--;
}
});
map.moveCamera(cu);
}
}
Date started = new Date();
while (mapLoaded !=0 && new Date().getTime() - started.getTime() < 10000){
try {//wait until map has loaded, but max 10 seconds
Thread.sleep(500);//wait half a second before tyring again
} catch (InterruptedException e) {}
}
}
Show all polylines on the map.
Create a builder
LatLngBounds.Builder builder = new LatLngBounds.Builder();
Iterate over all the points in the polylines sending them to lat long bounds builder.
for(Polyline pol : allcrumbs){
List<LatLng> points = pol.getPoints();
for (LatLng point : points){
// dude never initialize variables in a loop again
// its automatic fail for speed of execution.
// String never = "Do this in a loop";
// int padding = 40; // offset from edges of the map in pixels
builder.include(point);
}
}
Now move the camera
LatLngBounds bounds = builder.build();
int padding = 40; // offset from edges of the map in pixels
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
map.moveCamera(cu);
IDK what your doing in the maploaded callback so its not in the above code.
Tip: populate latlngbounds.builder as you create the polylines the simply move the camera when your finished loading the polylines.
LatLngBounds bounds = builder.build();
int padding = 40; // offset from edges of the map in pixels
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
map.moveCamera(cu);
Note: moving the camera along a route would be similar to your code but you would typically only update the camera when the camera is finished for each point.
actually I'm working on an android application using the Google Map APIv2, so far I'm trying to display some markers on a the map, also i want to have all the markers inside my camera view, for this I'm using
LatLngBounds.Builder builder = new LatLngBounds.Builder();
LatLngBounds bounds;
int padding = 0;
//markMap is a hashmap populated with my markers positions.
HashMap<String, LatLng> markMap = new HashMap<String, LatLng>();
for (Entry<String, LatLng> entry : markMap.entrySet())
{
map.addMarker(new MarkerOptions().position(entry.getValue()));
builder.include(entry.getValue());
}
bounds = builder.build();
CameraUpdate update = CameraUpdateFactory.newLatLngBounds(bounds, padding);
map.animateCamera(update);
This works perfectly fine, but there is only one problem with it, and that is : my current camera zoom level only includes the marker points, but the marker icon itself is usually, totally or partially, out of bounds.
Any suggestions on what i should do so i can have a the markers icons included inside my camera view ?
Why not just add some padding:
CameraUpdate update = CameraUpdateFactory.newLatLngBounds(bounds, 50);
I don't see why CameraUpdate should take into consideration the width of the marker itself. It includes the coordinates not the marker itself.
Another option could be:
CameraPosition cameraPosition =
new CameraPosition.Builder().target(ll).zoom(16).build();
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
You can see more about "Camera Position" here