android detect various circles on a map v2 - android

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

Related

Google map with 1000 marker

I am adding google map marker with 1000 marker in small boundery box, while moving map arround, app frezzing, didn't know why,
Also i have tried marker cluster . but client has refuesed it, he need all marker to be shown on map, as marker is mandetory to show to user...!
is there any way to do so with better preformance
i figure that may i zoom in to custom point and show marker only with boundry box map, and while user move map he remove marker around and put new marker
but this will come on looping in array with 1000 element in it, and this is n't better solution, any one know how to do so !!!?
for (int i = 0; i < stopDetailsModelResponses.size(); i++) {
mapHelper.setMarkerPinWithLayoutToImage(
stopDetailsModelResponses.get(i).getStopDetailsLat(),
stopDetailsModelResponses.get(i).getStopDetailsLong(),
stopDetailsModelResponses.get(i).getStopDetailsName(),
String.valueOf(stopDetailsModelResponses.get(i).getStopDetailsId()),
stopDetailsModelResponses.get(i).getStopDetailsLogoUrl(),
stopDetailsModelResponses.get(i).getStopDetailsType(),
markerOptions, googleMap, getContext()
);
}
googleMap.setOnMarkerClickListener(onMarkerClickListener);
this for looping inside list to set element to map marker
and this for adding marker to map
try {
markerOptions = new MarkerOptions()
.position(new LatLng(lat, longitude))
.title(locationName)
.draggable(false);
markerOptions.icon(BitmapDescriptorFactory.fromBitmap(defineLayoutOfPin(context, imageType, locationId)));
googleMap.addMarker(markerOptions);
} catch (Exception e) {
Log.e("setMarkerWithImage: ", e.getMessage());
}

Android - Removing markers when changing different maps

I have created an app that displays different tracks in a park. I have created two markers which represent the start and end of the track. I am getting a problem where when I change tracks, the marker from the previous track still shows. I tried map.clear() but that removed everything. I want to not show the markers from the previous track.
private void createMarker(double latitude, double longitude, String title) {
map.addMarker(new MarkerOptions()
.position(new LatLng(latitude, longitude))
.title(title)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)));
}
private void updateMapTrack() {
switch (options.getTrack()) {
case TRACK1:
createMarker(-45.85696303760779, 170.5199563062967, "Start of track1.");
createMarker(-45.85808344124618, 170.5247490755895, "End of track1.");
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(-45.856895, 170.518673), (float) 17.8));
break;
case TRACK2:
createMarker(-45.85696303760779, 170.5199563062967, "Start of track2.");
createMarker(-45.85808344124618, 170.5247490755895, "End of track2.");
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(-45.857144, 170.522713), (float) 16.32));
break;
case TRACK3:
createMarker(-45.85714008365828, 170.5193834664067, "Start of track3.");
createMarker(-45.85751258570694, 170.526808129631, "End of track3.");
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(-45.858164, 170.523111), (float) 16));
break;
}
}
You will have to keep the reference to the Marker Object you created and then you have to call
[Marker Reference].remove() method to remove it from the GoogleMap.
for Example:
Create Marker like this and save the reference:
Marker startTrackMarker = createMarker(-45.85696303760779, 170.5199563062967, "Start of track1.");
And that is how you will remove:
startTrackMarker.remove()
And your createMarker method will be changed like this:
private Marker createMarker(double latitude, double longitude, String title) {
return map.addMarker(new MarkerOptions()
.position(new LatLng(latitude, longitude))
.title(title)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)));
}
Note:
If you have multiple markers then you will need an array to hold all the references to the markets like this:
ArrayList<Marker> markers = new ArrayList<>();
and then to remove these markers you will have to iterate the array and call remove() on every marker object. (Otherwise, it all depends upon your logic).

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 show a radius in maps android?

I have a eight different Longitude and latitude values in that any one value have longitude and latitude and remaining wont have values in that case I have to show a radius for that one value. Can anyone help me to solve this problem.
Thanks
GoogleMap has method addCircle so your just need to call it with appropriate params:
GoogleMap map;
MarkerOptions markerOptions = new MarkerOptions();
LatLng position = new LatLng(50, 50);
markerOptions.position(position);
Marker marker = map.addMarker(markerOptions);
Circle circle = map.addCircle(
new CircleOptions()
.center(position)
.radius(100)
.strokeWidth(0f)
.fillColor(getActivity().getResources().getColor(R.color.someColor))
);

Google map markers are disappearing instantly. How to make a marker remain constant?

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

Categories

Resources