Create Marker but not show it on the map - android

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.

Related

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.

Look if current location is near marker

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. :)

How to clear Google map v2 except few markers?

Suppose i have 100 markers on the map what i want is when i apply GoogleMap.clear(); to clear the map it clear all the other markers on the map except 2 markers and 1 polyline between them a path.
say
marker1 = GoogleMap .addMarker(new MarkerOptions().position(latLng1).title(A));
marker2 = GoogleMap .addMarker(new MarkerOptions().position(latLng2).title(B));
line = GoogleMap.addPolyline(options1);
I don't want to clear these three. I want this so user don't have to experience a blink.
There is no way to clear everything except some things. However, you can keep a reference to any markers that you want to clear and loop over them.
ArrayList<Marker> markersToClear = new ArrayList<Marker>();
marker1 = GoogleMap.addMarker(new MarkerOptions().position(latLng1).title(A));
marker2 = GoogleMap.addMarker(new MarkerOptions().position(latLng2).title(B));
marker3 = GoogleMap.addMarker(new MarkerOptions().position(latLng3).title(C));
markersToClear.add(marker2);
markersToClear.add(marker3);
for (Marker marker : markersToClear) {
marker.remove();
}
markersToClear.clear();
// marker1 left on map

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

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