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.
Related
I'm having a bit of a struggle using CameraPosition because I can't have a proper zoom.
For me, a proper zoom would be the distance between a Marker and my current position.
However, I managed to get a proper zoom using CameraUpdateFactory, but the I lost all the other attributes (orientation (looking always north) and bird-eye (45 degrees view)).
I'm balanced between this (doesn't have the right zoom):
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(current_location)
.zoom(mGoogleMap.getCameraPosition().zoom)
.bearing(location.getBearing())
.tilt(birdEyesAngle)
.build();
and this (right zoom, but missing other attributes):
CameraUpdateFactory.newLatLngBounds(bounds_between_two_markers, 10);
Is there any way to have both correct zoom and correct orientation/bird-eye ?
Hope you'll help,
Thanks
Try this:
Get the CameraUpdate object first using the newLatLngBounds method:
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds_between_two_markers, 10);
The following code I put in my onMapReady() method:
LatLng pos = new LatLng(51.516667, 12.388889);
LatLng pos1 = new LatLng(53.516667, 14.388889);
MarkerOptions markerOptions = setUserMarker(pos);
if(markerOptions != null) {
markerOptions.title(campusLocationName);
mMap.addMarker(markerOptions);
}
LatLngBounds.Builder b = new LatLngBounds.Builder();
b.include(pos);
b.include(pos1);
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(b.build(), 20);
mMap.animateCamera(cu, 10, new GoogleMap.CancelableCallback() {
#Override
public void onFinish() {
Log.e(TAG, "Start animate onFinish");
CameraPosition cp = new CameraPosition.Builder()
.zoom(mMap.getCameraPosition().zoom)
.target(pos)
.tilt(45.0f)
.bearing(35.0f)
.build();
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cp));
// mMap.moveCamera(CameraUpdateFactory.newCameraPosition(cp));
}
#Override
public void onCancel() {
Log.e(TAG, "Start animate onCancel");
}
});
The result from my device:
Using the CancelableCallBack you can make the changes to the camera position as before, but if you do not change the zoom factor, the camera keeps the old zoom factor, you just set the bearing and tilt as you like.
I've looked at everything I can find on this, and haven't found a solution. I'm trying to do something very simple:
protected void moveCamera(final LatLng position, final float zoom) {
getMap(new OnMapReadyCallback() {
#Override
public void onMapReady(final GoogleMap googleMap) {
logger.debug("Moving map to " + position);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(position)
.zoom(zoom)
.build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
});
}
Nothing happens. No errors, no camera movement. I've also tried moveCamera instead. I've tried different types of calls to CameraUpdateFactory, I've tried moving and zooming separately, I've tried posting with a Handler with a delay. Sometimes this works perfectly, but if I leave the fragment with the map and come back, then it doesn't work. The map resets to latitude 0,0 and standard map type (it was set to hybrid) and I can't get it to do anything. I know the method is getting called by logging statements. Is there some kind of lifecycle thing I need to do to make sure the map can be restored?
I'd really prefer not to switch to Mapbox or something because that would be a lot of work to fix a little bug, but I need this to work correctly.
Try this code for this camera issue.
You can change zoom level, radius.
int width = getResources().getDisplayMetrics().widthPixels;
int height = getResources().getDisplayMetrics().heightPixels;
height = Math.round(0.45f*height);
int padding = (int) (width * 0.10);
LatLngBounds.Builder builder = new LatLngBounds.Builder();
LatLngBounds bounds = builder.include(new LatLng(INSERT_LAT,
INSERT_LNG)).build();
CameraUpdate cameraUpdate =
CameraUpdateFactory.newLatLngBounds(bounds,width,height,padding);
googleMap.moveCamera(cameraUpdate);
googleMap.animateCamera(CameraUpdateFactory.zoomTo(14), 1000, null);
I've used the following and it works fine:
GoogleMap mGoogleMap;
LatLngBounds bounds = builder.build();
int width = getResources().getDisplayMetrics().widthPixels;
int height = getResources().getDisplayMetrics().heightPixels;
height = Math.round(0.45f*height);
int padding = (int) (width * 0.10);
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(bounds,width,height,padding);
// Add this where you want to move map camera
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng("<pass_LatLng_here"));
mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(14)); //set the zoom level here
For me, the move camera inside the onPlaceSelected callback in Places Autocomplete api doesn't work.
I moved the moveCamera into a runOnUiThread and it's now working fine.
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.
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?