centering the camera in Google Maps V2 - android

In my android app I have the following code...
CameraUpdate center= CameraUpdateFactory.newLatLng(new LatLng(Lat,Lon));
DebugLog.debugLog("centered camera on " + Lat + " and " + Lon, false);
CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);
map.moveCamera(center);
map.animateCamera(zoom);
map.addMarker(new MarkerOptions()
.position(new LatLng(Lat, Lon))
.title("Phone Location")
);
Lat is 31.7898
Lon is -111.0354
The marker is exactly at the proper location. However the camera is centered about 5 miles north of that location on the v2 map.
Why?
Thanks
Gary

After reading some additional docs, I found that the following code worked...
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(Lat, Lon))
.zoom(15)
.bearing(0)
.tilt(45)
.build();
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
map.addMarker(new MarkerOptions()
.position(new LatLng(Lat, Lon))
.title("Phone Location")
);
I think my original code should have also worked.
Gary

Keep the perspective the same, but center camera at the currently clicked marker:
obj_map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker arg0) {
CameraPosition obj_cam = new CameraPosition.Builder()
.target(new LatLng(arg0.getPosition().latitude, arg0.getPosition().longitude))
.zoom(obj_map.getCameraPosition().zoom)
.tilt(obj_map.getCameraPosition().tilt)
.bearing(obj_map.getCameraPosition().bearing)
.build();
obj_map.animateCamera(CameraUpdateFactory.newCameraPosition(obj_cam));
return true;
}

Related

Update Marker position with Animation in Android Studio

I have successfully fetched the Latitude and Longitude from some data stored in the Database. But I am unable to update the marker location on the map. Can please someone help me with the code or as to how to do that.
Try this using CameraUpdate ,
LatLng coordinate = new LatLng(lat, lng); //Store these lat lng values somewhere. These should be constant.
CameraUpdate location = CameraUpdateFactory.newLatLngZoom(coordinate, 15);
mMap.animateCamera(location);
Or can also use this,
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title(totalAddress); //Here Total Address is address which you want to show on marker
mMap.clear();
markerOptions.icon(
BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
markerOptions.getPosition();
mCurrLocationMarker = mMap.addMarker(markerOptions);
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(11));

How to plot the marker in the middle point of the polyline in Google Maps?

I plotted a polyline in a Google Map and I want a marker in the polyline when the polyline is clicked. Then only the marker should appear in the middle of the polyline.
#Override
public void onPolylineClick(Polyline polyline) {
double latitude = polyline.getPoints().get(0).latitude;
double longitude = polyline.getPoints().get(0).longitude;
mMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(latitude, longitude))
.zoom(18).build();
}
With the code above the marker gets plotted only at the first point, but I need it in the middle of the polyline. How can I do that?
You can find the center of a polyline by getting the bound's center.
I haven't executed the code but I hope it will work fine.
public LatLng getPolylineCentroid(Polyline p) {
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for(int i = 0; i < p.getPoints().size(); i++){
builder.include(p.getPoints().get(i));
}
LatLngBounds bounds = builder.build();
return bounds.getCenter();
}
You can use the extrapolate function that I propose in this solution: How can I extrapolate farther along a road?
double middleDistance = SphericalUtil.computeLength(polyline.getPoints());
LatLng markerPosition = extrapolate(polyline.getPoints(), polyline.getPoints().get(0), middleDistance);
mMap.addMarker(new MarkerOptions().position(markerPosition).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(markerPosition)
.zoom(18).build();

SupportMapFragment set google maps Zoom level

how can i set the zoom level of a SupportMapFragment ?
I tried some answers I found in similar questions, like this one answered buy a guy named Rob who proposed:
LatLng currentLocation = mService.getCurrentLocation();
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(currentLocation) // Sets the center of the map to the current location of the user
.zoom(17) // Sets the zoom
.bearing(90) // Sets the orientation of the camera to east
.tilt(30) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
mGoogleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
But this solution didn't work. So, how can I set the Zoom level knowing that I use a SupportMapFragment.
if your current location is not null then try this code it will work
LatLng currentLocation = mService.getCurrentLocation();
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(currentLocation , 16);
mMap.addMarker(new MarkerOptions().position(location).title(""));
mMap.moveCamera(CameraUpdateFactory.newLatLng(currentLocation ));
mMap.animateCamera(cameraUpdate)
Marker currentMarker;
MarkerOptions marker = new MarkerOptions().position(new
LatLng(Double.
parseDouble(lat),Double.parseDouble(lon))).title(title);
marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_schoolbus));
currentMarker = map.addMarker(marker);
currentMarker.showInfoWindow();
currentMarker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.ic_schoolbus));
map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(Double.parseDouble(lat),Double.parseDouble(lon)), 15));

How to fix google map to a location

I want to set a google map location with lat/long so that it can't be changed by user. How to do it?
double latitude = 35.6961;
double longitude = 51.4231;
MarkerOptions marker = new MarkerOptions().position(
new LatLng(latitude, longitude)).title("Hello Maps");
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
googleMap.addMarker(marker);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(35.6961, 51.4231)).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
Try to add this line below your code.
googleMap.getUiSettings().setScrollGesturesEnabled(false);
This will forbid the user to scroll the map.

Look if current location is near marker

I have a Google Maps API Activity and want the markes only to be clickable if the user is in a certain radius to them.
How is the best way to do this?
This is how I set my markers:
Marker marker1 = googleMap.addMarker(new MarkerOptions()
.position(new LatLng(49.793012, 9.926201))
.title(getString(R.string.Title1))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)));
Marker marker2 = googleMap.addMarker(new MarkerOptions()
.position(new LatLng(49.792742, 9.939118))
.title(getString(R.string.Title2))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)));
Marker marker3 = googleMap.addMarker(new MarkerOptions()
.position(new LatLng(49.793349, 9.932558))
.title(getString(R.string.Title3))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)));
Solution code:
LatLng markerPosition = marker.getPosition();
myLocation = locationManager.getLastKnownLocation(provider);
Location markerLoc = new Location("marker");
markerLoc.setLatitude(markerPosition.latitude);
markerLoc.setLongitude(markerPosition.longitude);
float meters = myLocation.distanceTo(markerLoc);
You can get the LatLng of the markers by marker.getPosition(); then you can compare it with the CurrentLocation. To do this, you can check this post, and next you can make clickable or notclickable a marker as follows:
getMap().setOnMarkerClickListener(new OnMarkerClickListener() {
public boolean onMarkerClick(Marker marker) {
onMapClick(marker.getPosition());
return true;
}
});
If you return true for the function, it means that you've accepted the occurred click event on your marker; otherwise, you've not accepted it.
Moreover, you can do it with:
MarkerOptions.clickable and Marker.setClickable.
This is just a guide who how I would do what you want. hope it helps. :)

Categories

Resources