Android maps v2 - get map zoom - android

I am trying to get the android map to zoom on my marker & leave the marker in the center of the screen.
public void onNewLocation(Location location, String name) {
if (marker == null){
marker = new MarkerOptions().position(new LatLng(
location.getLatitude(),
location.getLongitude()))
.title(preferencesUtils.getName(getApplicationContext(), preName, key));
}
map.clear();
marker.position(new LatLng(location.getLatitude(), location.getLongitude()));
map.addMarker(marker);
}
this is the code - what should i add to zoom my marker

To simply zoom in, use:
float zoomLevel = 20.0f;
map.animateCamera(CameraUpdateFactory.zoomTo(zoomLevel);
To zoom to the marker, use:
LatLng l = new LatLng(LATPOSITION, LONGPOSITION);
float zoomLevel = 20.0f;
map.animateCamera(CameraUpdateFactory.newLatLngZoom(l, zoomLevel));
Add these where you want the zoom to happen.

Related

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));
}

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();

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

Android map pin move position during zoom in / zoom out

In my android app, I have added google map and I load dynamically more points using specific lat / long values and specific pins
This is my code:
GoogleMap map = holder.mapView.getMap();
if(map != null)
{
try {
MapsInitializer.initialize(activity);
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
map.clear();
map.getUiSettings().setZoomControlsEnabled(true);
LatLng latLng=new LatLng(gS.getLat(), gS.getLon());
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 10);
CameraUpdate center=
CameraUpdateFactory.newLatLng(latLng);
CameraUpdate zoom=CameraUpdateFactory.zoomTo(10);
//map.animateCamera(cameraUpdate);
map.moveCamera(cameraUpdate);
map.animateCamera(zoom);
if(time_diff > day3 && magnitude >= 0 && magnitude < 2)
map.addMarker(new MarkerOptions()
.draggable(false)
.anchor(0, 1)
.position(new LatLng(gS.getLat(), gS.getLon()))
.title("").icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_lt2m)));
The code works fine and loads the data into map with custom pin, but I have seen that when I zoom out or zoom in the map, the marker moves its position instead of remaining in its actual lat long position.
Any help?
Thanks
I use API V2. it's possible that a google play service old version create this?

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