MapView.moveCamera() show always the same camera - android

Hi guys my problem is about mapView. I want to set the camera to a new position but when i call SetNewPosition the log tell to me that coordinates are changed but the mapview on the screen shows always the same place.
public void SetNewPosition(double log,double lat)
{
Log.i("Current position",""+mappa.getCameraPosition());
LatLng latLng= new LatLng(log, lat);
CameraUpdate cameraUpdate= CameraUpdateFactory.newLatLng(latLng);
mappa.moveCamera(cameraUpdate);
mappa.clear();
map.invalidate();
map.postInvalidate();
Log.i("Changed position",""+mappa.getCameraPosition());
}
Can anyone tell where is my error?

This is what you may want to use :
LatLng latLng = new LatLng (log, lat);
LatLngBounds.Builder builder = new LatLngBounds.Builder ();
builder.include (latLng);
LatLngBounds bounds = builder.build ();
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds (bounds, 0);
googleMap.animateCamera (cameraUpdate);

Related

CameraPosition with bounds using GoogleMap

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.

Android Map Fragment Inconsistent Camera/Markers

My map fragment seems to be having some issues. I'm calling it in my 2nd activity from the first which is my custom adapter set to a recyclerView.
I am adding 2 markers to the map from coordinates that change depending on the item clicked in the adapter.
What I want to happen is for the camera to move the map to show each pair of coordinates with padding around them, as in the image below.
The problem is that it only appears this way maybe 10% of the time. The rest of the time the map is zoomed all the way out like in the below image.
Am I not building my bounds or moving the camera correctly or getting the coordinates correctly or what? I can't figure it out. Any ideas what I am doing wrong? This is how I am sending the lat/lng from my adapter:
holder.routeinfo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view){
Intent intent = new Intent(context, RouteDetailsActivity.class);
intent.putExtra("lat1", routeList.get(position).getLat1());
intent.putExtra("lng1", routeList.get(position).getLng1());
intent.putExtra("lat2", routeList.get(position).getLat2());
intent.putExtra("lng2", routeList.get(position).getLng2());
context.startActivity(intent);
}
});
And here is my onMapReady() from my RouteDetailsActivity:
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
Double lat1 = getIntent().getExtras().getDouble("lat1");
Double lng1 = getIntent().getExtras().getDouble("lng1");
Double lat2 = getIntent().getExtras().getDouble("lat2");
Double lng2 = getIntent().getExtras().getDouble("lng2");
//Origin Marker
LatLng origin = new LatLng(lat1, lng1);
mMap.addMarker(new MarkerOptions()
.position(origin)
.title("Origin"));
//Destination Marker
LatLng destination = new LatLng(lat2, lng2);
mMap.addMarker(new MarkerOptions()
.position(destination)
.title("Destination"));
LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(origin);
builder.include(destination);
LatLngBounds bounds = builder.build();
mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 5));
}

Unable to remove polyline when another marker in google map is clicked

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.

Android: How can I use the Google maps

Can someone explain how I can use the Google maps API in my project?
Cause I want to draw a marker on the map while moving the map with my finger.
But I don't know how to achieve this.
I am using the following code:
camCenter = mMap.getCameraPosition().target;
if (camCenter != null && camCenter != camCenterFirst) {
CameraUpdate center =
CameraUpdateFactory.newLatLng(new LatLng(camCenter.latitude,camCenter.longitude));
CameraUpdate zoom = CameraUpdateFactory.zoomTo(16);
mMap.moveCamera(center);
mMap.animateCamera(zoom);
camCenterFirst = camCenter;
}
First you have to add a marker to your Map like this:
LatLng pos = new LatLng(53.557115, 10.023159);
Marker m = mMap.addMarker(new MarkerOptions().position(pos).title(""));
The example above, gives you a default position of the marker.
Then you can change the position of the marker with following command:
m.setPosition(new LatLng(latitude,longitude));
Hope this helps!
public void showOnMap(Double lat,Double lng,String place)
MarkerOptions marker = new MarkerOptions().position(new LatLng(lat,
lng)).title(""+place);
mMap.addMarker(marker);
CameraPosition cameraPosition = new CameraPosition.Builder().target(
new LatLng(lat, lng)).zoom(12).build();
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
double radiusInMeters = 5000.0;
int strokeColor = 0xffff0000; //red outline
int shadeColor = 0x44ff0000; //opaque red fill
CircleOptions circleOptions = new CircleOptions().center(new LatLng(lat,lng)).radius(radiusInMeters).fillColor(shadeColor).strokeColor(strokeColor).strokeWidth(8);
mMap.addCircle(circleOptions);
}
I have done a complete demo of what you are looking for,
Check this Github code
https://github.com/cseshaiban/GeoApp

Set LatLangBound in GoogleMap

I am trying to set LatLangBound in map.When I give SouthWest LatLang & NorthEast Latlang of India, I am getting an error message that latitude of SouthWest > latitude of NorthEast (28.5827577 > 20.593684).
Is there any other way to display India regions when I start MapFragment.
My code snippet is.
private LatLngBounds India=new LatLngBounds(new LatLng(28.5827577,77.0334179),new LatLng(20.593684,78.96288));
mapView = (MapView) rootview.findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mMap = mapView.getMap();
try {
mMap.setMyLocationEnabled(true);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, latitude), 6));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(India.getCenter(),6));
CameraPosition cameraPosition =new CameraPosition.Builder().target(new LatLng(latitude,latitude)).zoom(2).bearing(0).tilt(90).build();
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}catch (Exception e) {
e.printStackTrace();
}
return rootview;
use the com.google.android.gms.maps.model.LatLngBounds.Builder
Adapted to your source code it should look something like this:
Builder builder = new LatLngBounds.Builder();
builder .include(currentLocationLatLng);
builder .include(targetLocationLatLng);
// pan to see all markers on map:
LatLngBounds bounds = builder .build();
googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 3));
I have solved my problem at the end .
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude,longitude),7.0f));
It will move the camera focus at the given LatLng() and will zoom the map.

Categories

Resources