Look if current location is near marker - android

I have a Google Maps API Activity and want the markes only to be clickable if the user is in a certain radius to them.
How is the best way to do this?
This is how I set my markers:
Marker marker1 = googleMap.addMarker(new MarkerOptions()
.position(new LatLng(49.793012, 9.926201))
.title(getString(R.string.Title1))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)));
Marker marker2 = googleMap.addMarker(new MarkerOptions()
.position(new LatLng(49.792742, 9.939118))
.title(getString(R.string.Title2))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)));
Marker marker3 = googleMap.addMarker(new MarkerOptions()
.position(new LatLng(49.793349, 9.932558))
.title(getString(R.string.Title3))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)));
Solution code:
LatLng markerPosition = marker.getPosition();
myLocation = locationManager.getLastKnownLocation(provider);
Location markerLoc = new Location("marker");
markerLoc.setLatitude(markerPosition.latitude);
markerLoc.setLongitude(markerPosition.longitude);
float meters = myLocation.distanceTo(markerLoc);

You can get the LatLng of the markers by marker.getPosition(); then you can compare it with the CurrentLocation. To do this, you can check this post, and next you can make clickable or notclickable a marker as follows:
getMap().setOnMarkerClickListener(new OnMarkerClickListener() {
public boolean onMarkerClick(Marker marker) {
onMapClick(marker.getPosition());
return true;
}
});
If you return true for the function, it means that you've accepted the occurred click event on your marker; otherwise, you've not accepted it.
Moreover, you can do it with:
MarkerOptions.clickable and Marker.setClickable.
This is just a guide who how I would do what you want. hope it helps. :)

Related

How to plot the marker in the middle point of the polyline in Google Maps?

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();

Show marker infoWindow

In onMapReady I am defining a few markers
First I declare the marker and his icon that is common for all markers and then each marker with it's own attributes. The problem is that when I declare the marker I cannot declare it as Marker beerMarker = new Marker() options as it makes me cast the Marker into MarkerOptions. What I would like is to call the info window for every marker but I cant as the beerMarker.showInfoWindow() is not acceptable for MarkerOptions. What am I doing wrong and what are the alternatives?
MarkerOptions beerMarker = new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.beer_marker));
//Carciuma
LatLng carciuma = new LatLng(43.604892, 1.476562);
mMap.addMarker(beerMarker.position(carciuma).title("Carciuma"));
//Boca
LatLng boca = new LatLng(43.604496, 1.474924);
mMap.addMarker(beerMarker.position(boca).title("Boca"));
//Bar Acasa
LatLng barAcasa = new LatLng(43.604781, 1.474502);
mMap.addMarker(beerMarker.position(barAcasa).title("Bar Acasa"));
Here is the updated version of the marker that meens I have t have to add for every individual marker the same icon, no?
LatLng barAcasa = new LatLng(43.604781, 1.474502);
Marker beerMarkerAcasa = mMap.addMarker(new MarkerOptions()
.position(barAcasa)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
.title("Bar Acasa"));
beerMarkerAcasa.showInfoWindow();
You can get this way Marker marker = mMap.addMarker(markeroption);
you can pass marker option for particulate marker in addMarker() method, they return marker instance which you can use.

How to put marker on android map location?

After I find this Location mCurrentLocation = mLocationClient.getLastLocation(); how can I add a marker to that that location?
Making a marker like
MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude))
requires a position object, is there a way that I can retrieve a position from the Location object? or is this done in some other way?
You can use the getLatitude() and getLongitude() of the Location class which will return double which by then you can pass it in your new LatLong constructor.
sample:
MarkerOptions marker = new MarkerOptions().position(new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude()))

Create Marker but not show it on the map

I would like to know how I can create a Marker but not to show on the map, I'm saving the Markers in an array but it is showing me on the map, this is my code
if (c4!= null ) {
if (c4.moveToFirst()) {
do {
LatLng posicion = new LatLng(
Double.parseDouble(c4.getString(0)),
Double.parseDouble(c4.getString(1)));
punto = googleMap.addMarker(new MarkerOptions() .position(posicion)
.title(c4.getString(2))
.snippet(year)
.anchor(0.5f, 0.5f));
mMarkers.add(punto);
}while (c4.moveToNext());
}
}
I've tried and used this
MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude));
but this does not work for me as I need to be type Marker.
Anyone know how could i save spots without paint on the map? thank you very much and sorry for my bad English :)
Try using:
punto = googleMap.addMarker(new MarkerOptions() .position(posicion)
.title(c4.getString(2))
.snippet(year)
.anchor(0.5f, 0.5f).visible(false));
instead of
punto = googleMap.addMarker(new MarkerOptions() .position(posicion)
.title(c4.getString(2))
.snippet(year)
.anchor(0.5f, 0.5f));
Notice the .visible(false) part.
For further info regarding the same refer: http://developer.android.com/reference/com/google/android/gms/maps/model/MarkerOptions.html
save the markers in this list
List<Marker> markers = new ArrayList<Marker>();
markers.add(marker); // here the marker is of google map
Then afterwords use Clear() on your google map so that the markers that got add should be deleted
EDIT=
Also you can separately remove a single marker by
marker.remove();
you can also try to save the latitude and longitude in array list and then later as you like it, show them through marker.

Showing multiple markers in google map v2

i have a set of lat long coordinates. i want to display all of those markers at a time on google map. I know how to display a single marker. But don't know how to display multiples at once. Anybody please help me.
Thank you.
i have lat long points.
like
id = 123 lat = 12.00 lon = 77.00
id = 124 lat = 12.01 lon = 77.01
etc.
May This Help You
for(int pin=0; pin<pins.size(); pin++)
{
LatLng pinLocation = new LatLng(Float.parseFloat(pins.get(pin).latitude), Float.parseFloat(pins.get(pin).longitude));
Marker storeMarker = map.addMarker(new MarkerOptions()
.position(pinLocation)
.title(pins.get(pin).pinname)
.snippet(pins.get(pin).address)
);
}
Try this:
LatLng one = new LatLng(2.40744, 77.014702);//Latitude and long points
LatLng two = new LatLng(2.407440, 77.014702);
.......
Similarly u can use more lat and long
myMarkerOne = gm.addMarker(new MarkerOptions()
.position(one)//use LatLng obj
.title("C")
.snippet("dsfd")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
myMarkerTwo = gm.addMarker(new MarkerOptions()
.position(two)
.title("C")
.snippet("dsfds")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
refer this: How to add multiple marker and overlay in v2 google map?
Also check this link:
Create a LatLng object by using following format:
LatLng location = new LatLng(latitude, longitude);
Then call the method with your LatLng object along with your GoogleMap object:
private Marker setCurrentLocation(LatLng location, GoogleMap map) {
BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory
.fromResource(R.drawable.user_location_pin);
Marker marker = map.addMarker(new MarkerOptions().position(location)
.icon(bitmapDescriptor).title(location.toString()));
return marker;
}

Categories

Resources