My App displays the driven route of the user. After a few seconds, the map stops loading new content and looks like this:
My Code:
XML
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
class="com.google.android.gms.maps.SupportMapFragment"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:cameraZoom="19"
android:layout_below="#+id/tracking_maps_colorgradient">
</fragment>
Java:
float zoom = 18;//googleMap.getCameraPosition().zoom;
LatLng latLng = new LatLng(lastLatitude, lastLongitude);
locations.add(latLng);
CameraPosition pos = new CameraPosition.Builder()
.target(latLng)
.bearing(bearing)
.zoom(zoom)
.tilt(googleMap.getCameraPosition().tilt)
.build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(pos));
Polyline p = googleMap.addPolyline(new PolylineOptions()
.add(latLng)
.width(15)
.color(Color.RED)
.geodesic(true));
p.setPoints(locations);
Is there a way to invalidate the Map?
Thanks for your help!
Ok, for this ones who have the same problem, heres the solution. The Problem is that the GoogleMap doenst refresh all the time, you have to change the camera position when the user out the boundaries.
LatLngBounds bounds = this.googleMap.getProjection().getVisibleRegion().latLngBounds;
if(!bounds.contains(new LatLng(gps.getLatitude(), gps.getLongitude()))) {
//Move the camera to the user's location if they are off-screen!
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(gps.getLatitude(), gps.getLongitude()))
.zoom(zoom) // Sets the zoom
.bearing(bearing)
.build(); // Creates a CameraPosition from the builder
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
Use Thread.sleep(300); after updating the map with markers, overlays, etc. Google Maps uses its own internal threads and need time to apply changes. It is possible that the thread adding stuff to the map is blocking Google Map's thread. The sleep will give it some time to make the update.
I don't know there is until you have a problem. But I solved and I wanted to share with you.
You can use handler and Thread.sleep(600). If you use just handler, you take same issue. Thread.sleep function waited for update map and than go on your marker.
Related
I'm new in this programming language so I kinda copy and paste the codes found in the net and then I've found and tried using these codes in bounding a certain area and it works:
private GoogleMap mMap;
// Create a LatLngBounds that includes the city of Adelaide in Australia.
private LatLngBounds ADELAIDE = new LatLngBounds(
new LatLng(-35.0, 138.58), new LatLng(-34.9, 138.61));
// Constrain the camera target to the Adelaide bounds.
mMap.setLatLngBoundsForCameraTarget(ADELAIDE);
But when I changed it using my campus coordinates into like this:
private LatLngBounds CPUBounds = new LatLngBounds(
new LatLng(10.732581, 122.548413), new LatLng(10.730052, 122.549958));
My app suddenly stop working. What should I do?
Try to paste the Logcat to review the Error
From If you want to show the specific position try the following code
To Move Camera Position Over a Specific Region Use this ,
mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds,
zoomWidth,
zoomHeight,
zoomPadding));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(10.732581, 122.548413),
zoomHeight));
If u want to add a marker Over region use
mMap.addMarker(new MarkerOptions()
.position(new LatLng(10.732581, 122.548413))
.title("campus name"));
To Disable the Dragging Use
mMap.getUiSettings().setScrollGesturesEnabled(false);
but it doesn't disable map zoom by touch on map. for that use Following Code
googleMap.getUiSettings().setZoomGesturesEnabled(false);
I want a Map UI like above dotted circle route path .I searched a lot through web and stack overflow i cant find any source.
I tried
addCircle polyline but the circle is large when i zoom the camera to
an extend in google map
I don't know how google map implemented it either with marker or
polyline
Provided help is appreciated rather than downvotes!!
thanks :-)
You can use Stroke Patterns for polylines like this:
#Override
public void onMapReady(GoogleMap googleMap) {
List<LatLng> sourcePoints = new ArrayList<>();
sourcePoints.add(new LatLng(22.724526, 75.825979));
sourcePoints.add(new LatLng(22.720165, 75.905953));
sourcePoints.add(new LatLng(22.766649, 76.075773));
sourcePoints.add(new LatLng(22.862032, 76.098302));
PolylineOptions polyLineOptions = new PolylineOptions();
polyLineOptions.addAll(sourcePoints);
polyLineOptions.width(50f);
polyLineOptions.color(Color.rgb(0, 178, 255));
Polyline polyline = googleMap.addPolyline(polyLineOptions);
List<PatternItem> pattern = Arrays.<PatternItem>asList(new Dot(), new Gap(10f));
polyline.setPattern(pattern);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(sourcePoints.get(2))
.zoom(14)
.build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
to get result like that:
Unfortunately it's impossible to draw bigger circles under smaller to achieve "bordered" circles path because of nonlinear dependency between Dot() and Gap() sizes.
Or you can draw whatever you want on MapVew canvas via overriding dispatchDraw() of MapView like in this answer (it's about MapBox MapView, but approach is applicable for Google Maps MapView too).
I think its overkill, but you can use some of Google APIs from https://console.developers.google.com/apis/library
and get path and make from them set of LatLngs, then draw them on googleMap just like normal marker, but with small round circle as icon.
To be honest, I am not sure what you want to achieve, I think simpler idea is to use origin and destination and open it in GoogleMaps Android App - do not reinvent the wheel.
i'm doing an app in android studio and what i'm trying to do is a route tracking app, the user touch a play button and he can see in the map a marker with his route being drawn by polylines.
I did that already but i want your help because when the user touch the stop button i want to take a Snapshot of the complete route (including the start point and the end point) and i don't know how to do that, because if the route is long and the zoom is close to the map then the starting point isn't going to show...
I hope you understand and could help me! :)
Sorry for my English, I use this example in my code to zoom all the markers, its simple i use a cycle for add the LatLng and zoom all.. with CameraUpdateFactory.newLatLngBounds(bounds_latandlog,padding);, and you take the Snapshot...
LatLngBounds.Builder builder = new LatLngBounds.Builder();
///this is an example
LatLng latLng;// create var LatLng
for(int i=0;i<=10;i++)// you will do a type of cycle, tu add lat and long
{
latLng = new LatLng(lat, lon);// in this line put you lat and long
builder.include(latLng); //add latlng to builder
}
//create a lat long bounds
LatLngBounds bounds = builder.build();
//create a padding of the points and the camera of the map
int padding = 0; // offset from edges of the map in pixels
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
//finally move the camera, to zoom all the polylines
/// move the camera
googleMap.moveCamera(cu);
//or animate the camera...
googleMap.animateCamera(cu);
This is the link, for more information
Android map v2 zoom to show all the markers
if you see some error, or you have a better syntaxis, please explane me, I want to learn it.
Hi I have created a map as well as some markers which indicate places/locations on the map. Is there any possible way of creating an animation between those location? For instance drawing a polyline between 2 different locations and create an animation from a start point to an end point ? Is there any documentation for such a thing or any tutorials ? Many thanks.
Something like this:
https://www.youtube.com/watch?v=HPWYorS68WE
use this code and you can also follow this link
https://developers.google.com/maps/documentation/android/
Flat Markers
LatLng mapCenter = new LatLng(41.889, -87.622);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(mapCenter, 13));
// Flat markers will rotate when the map is rotated,
// and change perspective when the map is tilted.
map.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.direction_arrow))
.position(mapCenter)
.flat(true)
.rotation(245));
CameraPosition cameraPosition = CameraPosition.builder()
.target(mapCenter)
.zoom(13)
.bearing(90)
.build();
// Animate the change in camera view over 2 seconds
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition),
2000, null);
}
All the basic stuff, I have a SupportMapFragment, everything works fine, I just need to know if there is a short way of making the map follow my position or do I have to move the camera every time the location changes?
Well first of all thanks for your time Naskov but this is not a mapView, it's a MapFragment. the code I am using right now (in case someone needs it) is this (onUpdateLocation)
if (mapFragment.theMap != null) {
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(getLocation().getLatitude(),
getLocation().getLongitude())).zoom(14.0f).build();
CameraUpdate cameraUpdate = CameraUpdateFactory
.newCameraPosition(cameraPosition);
mapFragment.theMap.moveCamera(cameraUpdate);
}
I was just wondering if there is some built in method for doing it, also because this doesnt go very smooth. If someone comes up with a better idea please post it
It's not exactly what you want, but you can use:
map.setMyLocationEnabled(true);
To add a button to your map at top right corner (Like in the native Google Maps application) that will allow the user change the camera position to his location when he wants.