I want to show polyline between start point and end point of particular route in mapview with lite mode in recycler view.for that I used latlngbounds to include all the route point but when route is so long say for 20 km one of two point is not showing in mapview ..any idea how to achieve this..any help is appreciated
I had managed this scenario by setting the padding on four sides by following code snippet.
mGoogleMap.setPadding(100, 100, 100, 100); //GoogleMap mGoogleMap
Then add two latlng to LatLngBounds.Builder.
Add builder to bounds, then to Map.
public void setViewPortToBounds(LatLng point1, LatLng point2) {
mGoogleMap.setPadding(100, 100, 100, 100);
mapBuilder = new LatLngBounds.Builder();
mapBuilder.include(point1);
mapBuilder.include(point2);
mapBounds = mapBuilder.build();
mGoogleMap.setOnCameraChangeListener(onCameraChangeListener);
}
GoogleMap.OnCameraChangeListener onCameraChangeListener = new GoogleMap
.OnCameraChangeListener() {
#Override
public void onCameraChange(CameraPosition cameraPosition) {
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(mapBounds, 0));
mGoogleMap.setOnCameraChangeListener(null);
}
};
i solved it by including all route points on polyline and then move camera to that bounds,and then zoom out the camera..
Related
I am designing an Android app that should show multiple markers on map. I can do that easily. My problem is that I need to show all the markers around my position, and the CameraPosition needs to be in focused in my position. I can easily do each on them separately, but what I need is showing them together, my location in the middle of the map, and the markers around my location.
Any idea how can I achieve that? Thanks in advance!
Set your zoom to a higher level so it can zoom out and see more of your map around your current location.
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(yourLatLng).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
The Functionality you need is to keep all the selected markers on the maps and it it should be visible at a single time. you have to keep all those points in the visible region with myLlocation (your current location) at the center of the screen always.
First keep your myLocation inside an latlng variable and all the other latlng which are to be inside the bounds inside another ArrayList of LatLng. Also define some variables to keep track of operation.
LatLng myLocation=null;
ArrayList<LatLng> latlngs = null; // the arraylist of other latlng;
boolean are_all_inside_viewport = false;
float current_zoom = googleMap.getCameraPosition().zoom;
//initialise these variables myLocation and latlngs;
Then animate your camera to myLocation which will be placed into the center of the map. put a low zoom level here.
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(myLocation)
.zoom(14)
.build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
Now check for each latlng in the ArrayList of they are in the google maps view port. if they are inside then its ok. if not then decrease a zoom level of the maps and animate the camera again.
while (!are_all_inside_viewport) {
are_all_inside_viewport = true;
for (LatLng latLng : latlngs) {
if (!bounds.contains(latLng)) {
are_all_inside_viewport = false;
break;
}
}
bounds = googleMap_local.getProjection().getVisibleRegion().latLngBounds;
cameraPosition = new CameraPosition.Builder()
.target(myLocation)
.zoom(current_zoom - 1)
.build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
This will go on checking for each latlng inside the ArrayList until all the latlng are inside the viewport.
Hope this answer helps you.
I have a simple Google Map (V2) polyline, based on 5 LanLong objects. And i need:
1) Make this clickable (which i assume can be done by implementing onClickListener interface on subclass of Polyline class)
ArrayList<LatLng> coordinates = new ArrayList<>();
coordinates.add(new LatLng(37.345, 56.432));
coordinates.add(new LatLng(39.234, 50.451));
coordinates.add(new LatLng(45.798, 24.345));
coordinates.add(new LatLng(34.783, 70.345));
coordinates.add(new LatLng(14.234, 23.453));
PolylineOptions path = new PolylineOptions();
path.addAll(coordinates);
path.width(5);
path.color(Color.RED);
path.geodesic(true);
2) If click was on polyline - set a marker at nearest vertex of this polyline. I think i can get screen coordinates of touch event, but how can i get screen coordinates of vertex point of this polyline?
Or maybe there is simpler approach?
I've found a sulution to my question. I post code of onMapClick() method for anyone who struggling:
#Override
public void onMapClick(LatLng clickCoordinates) {
for (LatLng pathCoordinates : path.getPoints()) {
float[] results = new float[1];
Location.distanceBetween(clickCoordinates.latitude, clickCoordinates.longitude,
pathCoordinates.latitude, pathCoordinates.longitude, results);
if (results[0] < 100) {
if (currentMarker != null) {
currentMarker.remove();
}
currentMarker = googleMap.addMarker(new MarkerOptions().position(new LatLng(pathCoordinates.latitude, pathCoordinates.longitude)));
}
}
}
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
Please help!!!
i have read - How to get all visible markers on current zoom level , but i have over than 2000 markers on map, and my app works very slowly.
Is there another solution ???
code -
public boolean isVisibleArea(final Marker marker) {
final LatLngBounds.Builder bld = new LatLngBounds.Builder();
final VisibleRegion visibleRegion = mMap.getProjection().getVisibleRegion();
bld.include(visibleRegion.farLeft)
.include(visibleRegion.farRight)
.include(visibleRegion.nearLeft)
.include(visibleRegion.nearRight);
return bld.build().contains(marker.getPosition());
}
I am not sure if it is really faster, but this code is definitely cleaner:
public boolean isVisibleOnMap(LatLng latLng) {
VisibleRegion vr = mMap.getProjection().getVisibleRegion();
return vr.latLngBounds.contains(latLng);
}
I'm trying to draw some markers and a polyline in the same map this way
MarkerOptions options = new MarkerOptions();
Marker marker;
PolylineOptions polyLineOptions = new PolylineOptions();
polyLineOptions.color(context.getResources().getColor(R.color.mm_red));
if (route != null) {
for(Entry entry : route.getEntries().getEntryList()){
LatLng pos = new LatLng(entry.getGeolocation().getLatitude(), entry.getGeolocation().getLongitude());
options.position(pos);
polyLineOptions.add(pos);
marker = mMap.addMarker(options.icon(BitmapDescriptorFactory.fromResource(R.drawable.im_map_marker_main_num)));
marker.setVisible(true);
}
Polyline polyline = mMap.addPolyline(polyLineOptions);
polyline.setVisible(true);
}
However, here you can see the result I get:
As you can see, even if I pass the same LatLng position to both MarkerOptions and PolylineOptions, there is some kind of offset.
As extra information, I get this behaviour in a Nexus 7.
Thank you
Edit: The marker icon's pin location is the default one, so the center of the bottom side of it
Edit: Not bitmap creation problem. If I use canvas.drawColor, this is what I get (different size, because now I'm with a Nexus 10)
The problem here is that by default, the pin location of the Marker is at the bottom center of the image.
You will have to fiddle with anchor(float, float) and / or make sure the sharp edge is the last pixel on the bottom center (with no transparent pixels below).