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));
}
Related
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();
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);
In my little app I have a map and i'm parsing an XML from the web, like this:
<place>
<lat>42.827602</lat>
<lon>-1.663957</lon>
<place_name>Place one</place_name>
<snippet>Snippet de place Facebook</snippet>
<thumb>http://www.animage.com/myicon1.png</thumb>
</place>
<place>
<lat>42.830750</lat>
<lon>-1.669064</lon>
<place_name>Place two</place_name>
<snippet>Snippet de place Twitter</snippet>
<thumb>http://www.animage.com/myicon2.png</thumb>
</place>
<place>
<lat>42.825333</lat>
<lon>-1.668232</lon>
<place_name>Place Three</place_name>
<snippet>Snippet de place Skype</snippet>
<thumb>http://www.animage.com/myicon3.png</thumb>
</place>
</response>
every time I read a "place" from the xml, the onPostExecute of my AsyncTask, calls the method that creates a new marker and circle on the map of my application
For example, if the xml has seven "places", the method to create a new marker and a circle is called seven times.
Marker aMarker;
Circle aCircle;
//...
public void createNewMarkerAndCircle() {
//...
aMarker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(dLat, dLon))
.title(nombre_punto)
.snippet(introduccion_punto)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
aCircle = mMap.addCircle(new CircleOptions()
.center(new LatLng(dLat, dLon))
.radius(150)
.strokeColor(Color.RED)
}
//...
public void onPostExecute(String xml) {
xml = stringBuffer.toString();
try {
Document doc = parser.getDomElement(xml);
NodeList nl0 = doc.getElementsByTagName(KEY_PLACE);
Element e = (Element) nl0.item(lap);
theLat = parser.getValue(e, KEY_LATITUDE);
theLon = parser.getValue(e, KEY_LONGITUDE);
//...
createNewMarkerAndCircle();
//...
So far, everything works fine and markers and circles are created on the map.
My purpose is that when the user on the map comes within the radius of one of the circles, get the datas of the marker that is inside.
I show you what I'm doing:
I have a listener of my location that every time it´s updated, check the distance from the current location and radius of the circle.
GoogleMap.OnMyLocationChangeListener myLocationChangeListener = new GoogleMap.OnMyLocationChangeListener() {
#Override
public void onMyLocationChange(Location location) {
LatLng loc = new LatLng(location.getLatitude(), location.getLongitude());
float[] distance = new float[2];
try {
Location.distanceBetween(
location.getLatitude(),
location.getLongitude(),
aCircle.getCenter().latitude,
aCircle.getCenter().longitude, distance);
if (distance[0] > aCircle.getRadius()) {
Log.i("myLogs", "Outside");
} else {
String markerTitle;
markerTitle = aMarker.getTitle();
Log.i("myLogs", "I´ in the circle" + " " + markerTitle);
}
//....
This works fine, but with a big problem.
When I start to walk the route, only detected when I go into the last "place" parsed from xml, and therefore, the last marker and circle created.
I understand what the problem is, the last marker (Marker aMarker;) and circle (Circle aCircle;) created are those with the values assigned ... But i do not know how to fix it.
I would appreciate any help, several days ago I'm looking for solutions, but without success.
Thanks and regards.
More information:
I found this other way , but the problem remains exactly the same:
https://gist.github.com/saxman/5347195
the last marker (Marker aMarker;) and circle (Circle aCircle;)
created are those with the values assigned ... But i do not know how
to fix it.
In order to check the distance against all the points of interests you create you need to hold them in some sort of structure, the most simple being a list, to reference them later in the listener:
Marker aMarker;
Circle aCircle;
// this will hold the circles
List<Circle> allCircles = new ArrayList<Circle>();
// this will hold the markers
List<Marker> allMarkers = new ArrayList<Marker>();
public void createNewMarkerAndCircle() {
aMarker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(dLat, dLon))
.title(nombre_punto)
.snippet(introduccion_punto)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE )));
// store the new marker in the above list
allMarkers.add(aMarker);
aCircle = mMap.addCircle(new CircleOptions()
.center(new LatLng(dLat, dLon))
.radius(150)
.strokeColor(Color.RED)
// store the created circles
allCircles.add(aCircle);
}
// in the LocationListener iterate over all stored circles and check the distances against each one of them
// as you add the circles and markers in the same method you'll have a correspondence between them in the two lists
// another sensible approach would be to create a custom Area class
// to hold both the Marker and the circle in one single place
// so when you find yourself inside a circle the marker will be at the same position in allMarkers
for (int i = 0; i < allCircles.size(); i++) {
Circle c = allCircles.get(i);
Location.distanceBetween(location.getLatitude(), location.getLongitude(), c.getCenter().latitude, c.getCenter().longitude, distance);
if (distance[0] > c.getRadius()) {
Log.i("myLogs", "Outside");
} else {
String markerTitle;
markerTitle = allMarkers.get(i).getTitle();
Log.i("myLogs", "I´ in the circle" + " " + markerTitle);
// you found a circles so you may want to break out of the for loop, break;
}
GeofencingApi was created exactly for this purpose: https://developer.android.com/reference/com/google/android/gms/location/GeofencingApi.html
I think you make mistake createNewMarkerAndCircle(), Please make MarkerOptions and Circle local else when update map only last instance of show, that is reason that only last circle show.
public void createNewMarkerAndCircle() {
//...
MarkerOptions aMarker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(dLat, dLon))
.title(nombre_punto)
.snippet(introduccion_punto)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
Circle aCircle = mMap.addCircle(new CircleOptions()
.center(new LatLng(dLat, dLon))
.radius(150)
.strokeColor(Color.RED)
}
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.
I am adding markers on my map by fetching user locations stored in the remote server. The locations are displayed but within 3 seconds, the marker disappears. Any solution to this?? Below is my complete code.
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
for(Users u:locList)
{
MarkerOptions markerOptions = new MarkerOptions();
double latitude1 = u.getLatitude();
double longitude1 = u.getLongitude();
LatLng latLng1 = new LatLng(latitude1, longitude1);
// Animating to the touched position
mGoogleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng1));
if(userMarker!=null) userMarker.remove();
markerOptions = new MarkerOptions().position(new LatLng(latitude1, longitude1)).title(latLng1.toString());
// adding marker
userMarker = mGoogleMap.addMarker(markerOptions);
//userMarker.setVisible(true);
// Placing a marker on the touched position
mGoogleMap.addMarker(markerOptions);
markerOptions.visible(true);
}
}
please check your code do you use something like map.clear()
remove
userMarker.remove();
from your code...
I fixed the same issue by commenting out map.clear. Wow correct my grammar way to go get a life