Hi Im having a little issue with displaying points on a map.
I use an Arraylist to store multiple lat/lng values and then do a for loop to add the point and auto zoom. Everything works fine when there are 2 or more markers. The problem is when only 1 marker is added it zooms in too close.
Anyone know how to resolve this?
public static void processMap()
{
for (int i = 0; i < lat.size(); i++)
{
MarkerOptions markerOptions = new MarkerOptions();
LatLng latLng = new LatLng(lat.get(i), lng.get(i));
markerOptions.position(latLng);
markerOptions.title("title");
markerOptions.snippet("description");
mMap.addMarker(markerOptions);
bounds.include(new LatLng(lat.get(i), lng.get(i)));
}
mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds.build(), 150));
}
Updated code
public static void processMap()
{
int num = 0;
double lat2 = 0;
double lng2 = 0;
for (int l = 0; l < lat.size(); l++)
{
lat2 = lat.get(l);
lng2 = lng.get(l);
LatLng latLng = new LatLng(lat2, lng2);
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title(title.get(l));
markerOptions.snippet(description.get(l));
mMap.addMarker(markerOptions);
bounds.include(new LatLng(lat2, lng2));
num++;
}
if (num == 1)
{
// if only 1 marker
LatLng latLng2 = new LatLng(lat2, lng2);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng2, 16));
}
else
{
// more than 1 marker
mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds.build(), 150));
}
}
Hi sorry I didn't reply earlier I was attempting to fix the problem. I managed to fix it, but is this the correct way of going about it?
The edit you made should fix your problem. Anyway you don't need to set new LatLng if you only have 1 marker. Passing the bound should do. I would make this little edit:
if (num == 1)
{
// if only 1 marker
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(bounds.build(), 16));
}
else
{
// more than 1 marker
mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds.build(), 150));
}
Hope it helps. Good luck!
Your code does not really tell why you are using .newLatLngBounds(), but this will center the map on your given bounding box and respecting the given 150px padding around the box.
The zoom is then adapted according to the elements inside the bounding box to show all of them. When there is only one marker inside the box, it will zoom to it (and probably too close). The docs say that the map will be centered "at the greatest possible zoom level".
So to prevent this, I would choose a good zoom level for the first marker, add it, move the camera with moveCamera(...) and after that continue with your code when there are other markers left.
Related
I working with maps and in my app I have a SupportMapFragment filling whole screen. When a marker is clicked a popup shows. This popup when shown get 75% of the screen.
Because of that I need to center the marker in the remaining 25%. To do that I'm using setOnClusterItemClickListener from CluserManager. But no matter what I do the marker always goes to the center of the SupportMapFragment.
I tried most solutions from this answer:
How to center the camera so that marker is at the bottom of screen? (Google map api V2 Android)
No success. Below I put some code. What I would like to know is why marker continues to go to center of the screen.
clusterManager.setOnClusterItemClickListener(new ClusterManager.OnClusterItemClickListener<MyItem>() {
#Override
public boolean onClusterItemClick(MyItem myItem) {
LatLng location = myItem.getPosition();
/*float zoom_lvl = googleMap.getCameraPosition().zoom;
double dpPerdegree = 256.0*Math.pow(2, zoom_lvl)/170.0;
double screen_height = (double) mapFragment.getView().getHeight();
double screen_height_30p = 50.0*screen_height/100.0;
double degree_30p = screen_height_30p/dpPerdegree;
LatLng centerlatlng = new LatLng( location.latitude, location.longitude + degree_30p);
googleMap.animateCamera(CameraUpdateFactory.newLatLng(centerlatlng), 1000, null);*/
Projection projection = googleMap.getProjection();
LatLng markerPosition = myItem.getPosition();
Point markerPoint = projection.toScreenLocation(markerPosition);
Point targetPoint = new Point(markerPoint.x, markerPoint.y - mapFragment.getView().getHeight() / 4);
LatLng targetPosition = projection.fromScreenLocation(targetPoint);
googleMap.animateCamera(CameraUpdateFactory.newLatLng(targetPosition), 1000, null);
}
});
Solved. I just need to return true. Returning false tells to ClusterManager always centers the mark on screen.
I'm trying to show a ground overlay with markers on it to my users. I'm trying to restrain the view to only this image placed on the map. I want the user to only see the image as a ground overlay placed on the map and not be able to go to the surrounding map. And if they go over the edge, the gestures will be blocked.
I want something like this:
I don't want this:
show-only-ground-overlays-map-android or this:
I tried to set my map.setLatLngBoundsForCameraTarget() to my image bounds but the result is something like the previous image...
The next thing I tried is to set a bunch of LatLng objects all around the ground overlay and check with curScreen.contains(customMapDetectionPoints.get(LatLng Object)) to see if they appear on the viewport. It's does work but I can't stop the camera to go over the edge...
Here my test code so far :
private GroundOverlay groundOverlay;
private GoogleMap globalMap;
private final int DETECTION_POINTS_CUSTOM_MAP = 20;
private List<LatLng> customMapDetectionPoints = new ArrayList<>();
//Fully Working as suppose to
#Override
public void onMapReady(GoogleMap map) {
//Other Stuff...
LatLngBounds mapBounds = groundOverlay.getBounds();
map.setLatLngBoundsForCameraTarget(mapBounds);
globalMap = map;
LatLng northwest = new LatLng( mapBounds.northeast.latitude, mapBounds.southwest.longitude);
LatLng northeast = mapBounds.northeast;
LatLng southeast = new LatLng( mapBounds.southwest.latitude, mapBounds.northeast.longitude);
LatLng southwest = mapBounds.southwest;
//My ground overlay is rectangle so I don't need to follow a path or something like that
setDetectionPoints(northwest, southwest);
setDetectionPoints(northeast, southeast);
setDetectionPoints(northwest, northeast);
setDetectionPoints(southwest, southeast);
map.setOnCameraMoveStartedListener(new GoogleMap.OnCameraMoveStartedListener() {
#Override
public void onCameraMoveStarted(int i) {
LatLngBounds curScreen = globalMap.getProjection().getVisibleRegion().latLngBounds;
CameraPosition cameraPosition = globalMap.getCameraPosition();
for (int x =0;x<customMapDetectionPoints.size();x++) {
if (curScreen.contains(customMapDetectionPoints.get(x))) {
cancelMapMovement(cameraPosition);
Log.d("OUT", "Ground Overlay is outside viewport");
return;
} else {
globalMap.getUiSettings().setAllGesturesEnabled(true);
Log.d("IN", "Ground Overlay is inside viewport");
}
}
}
});
//Add 20 new location between two location
//Fully Working as suppose to
public void setDetectionPoints(LatLng fromPos, LatLng toPos) {
double pointLatitude = fromPos.latitude;
double pointLongitude = fromPos.longitude;
double addingValue;
if (fromPos.latitude == toPos.latitude) {
addingValue = (toPos.longitude - fromPos.longitude)/DETECTION_POINTS_CUSTOM_MAP;
for (int i = 0; i < DETECTION_POINTS_CUSTOM_MAP; i++) {
pointLongitude += addingValue;
LatLng pointsPos = new LatLng(pointLatitude, pointLongitude);
customMapDetectionPoints.add(pointsPos);
}
} else if (fromPos.longitude == toPos.longitude) {
addingValue = (toPos.latitude - fromPos.latitude)/DETECTION_POINTS_CUSTOM_MAP;
for (int i = 0; i < DETECTION_POINTS_CUSTOM_MAP; i++) {
pointLatitude += addingValue;
LatLng pointsPos = new LatLng(pointLatitude, pointLongitude);
customMapDetectionPoints.add(pointsPos);
}
}
}
//The problem is here!
public void cancelMapMovement(CameraPosition camPos ) {
//HOW CAN I STOP THE MOVEMENT OVER THE GROUND OVERLAY EDGE
//And make sure that the user dosen't see over the edge
globalMap.getUiSettings().setAllGesturesEnabled(false);
globalMap.moveCamera(CameraUpdateFactory.newCameraPosition(camPos));
}
At this point I think I have two possible solutions:
1- Only use the setLatLngBoundsForCameraTarget() function and set a offset or margin to the camera. But is it possible and will it work for my use case ?
2- Solve my camera restriction problem with the code already written
Thanks for helping! Suggest other solution if you find one!
I will provide more info if wanted.
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)));
}
}
}
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 have a bunch of markers in which I load onto a google map. My problem is that, the camera is not ontop of the markers. Can I center it on top of all my markers?
I am adding my markers with this code:
for(int i = 0; i < jsonArray.length(); i++) {
String breweryName = jsonArray.getJSONObject(i).getString("brewery");
String lat = jsonArray.getJSONObject(i).getString("lat");
String lng = jsonArray.getJSONObject(i).getString("lng");
String bID = jsonArray.getJSONObject(i).getString("breweryID");
double latD = Double.parseDouble(lat);
double lngD = Double.parseDouble(lng);
m.addMarker(new MarkerOptions()
.position(new LatLng(latD, lngD))
.title(breweryName));
}
I looked on stack and found this question, which is the same as mine:
Android Google maps API V2 center markers
But there isnt much context behind it and am not sure how to implement it.
You can animate your camera within specific bounds. Let's say you have an ArrayList of markers, something like (this could also be a list of LatLngs, doubles, anything that contains the latitude/longitude for your marker)
List<MarkerOptions> markers = new ArrayList<MarkerOptions>();
You can then make sure they are all visible on your map by doing the following
LatLngBounds.Builder builder = new LatLngBounds.Builder();
LatLng position;
for(int i = 0; i < markers.size(); i++){
position = markers.get(i).getPosition();
builder.include(new LatLng(position.latitude, position.longitude));
}
LatLngBounds bounds = builder.build();
map.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 15));