I have a google maps fragment with a few markers that have a marker click event, in that mapFragment I also have a map click listener that calculates the distance between 2 clicks in the map, the problem is that if I click close to the marker the marker gets the event, I tried returning true, but I still cant click behind the marker. Is there any way to completely ignore the marker if I click near it?
I have simple marker click that puts the marker in front if its clicked, I return false because I want to consume the vent myself.
gMap.setOnMarkerClickListener(marker -> {
if (mSelectedMarker != null) {
mSelectedMarker.setZIndex(marker.getZIndex());
}
marker.setZIndex(Float.MAX_VALUE);
mSelectedMarker = marker;
return false;
});
The I also have a mapClickListener that can be enabled and disabled, that checks for 2 map clicks and draws 2 markers and a polyline between them.
onMapClickListenerRegla = new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick(#NonNull LatLng latLng) {
// incomplete code
if (markerReglaInicio == null) {
markerReglaInicio = gMap
.addMarker(new MarkerOptions()
.position(latLng)
.anchor(0.5F, 0.5F)
.icon(bitmapDescriptorFromVector(requireContext(),
R.drawable.marker_utilidades_coordenadas)));
return;
}
Maps fragment with my custom marker and 2 circles from the map click event
I want to remove a Marker on long click, and place it in another place without clearing the whole map.
Right now the circle radius works fine, but the custom pin icon is not removed.
using the GoogleMap.clear() method should not be used - there are some other objects on the map I don't want refreshed.
I have also tried setPosition method and had the very same result.
How to erase this icon?
Marker declaration:
marker = googleMap.addMarker(new MarkerOptions().position(marker.getPosition()).draggable(true).title("INFO")
.infoWindowAnchor(0.5f,0.5f).visible(false)
.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.pin_icon))));
this.googleMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
#Override
public void onMapLongClick(LatLng latLng) {
if (marker != null) {
marker.remove();
}
createMarker(latLng);
}
});
private void createMarker(LatLng latLng) {
marker = googleMap.addMarker(new MarkerOptions().position(latLng).draggable(true).title("INFO")
.infoWindowAnchor(0.5f,0.5f)
.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.ic_pin_gr))));
}
I used such approach for creating markers and simply setPosition(new Latlng(...)) working.
marker = mGoogleMap.addMarker(new MarkerOptions()
.anchor(0.5f, 0.5f)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_pin))
.position(new LatLng(cab.getLatitude(), cab.getLongitude())));
Did you try to replace marker.remove(); on setPosition() inside onLongClick() method ?
Hey Stackoverflow community,
I am working on an android app for an academic project, the goal is to retrieve data using an API and display it on a map using markers.
I would like to display some details in a fragment when the user click on the marker. I use beginTransaction and setArguments to do so and it works for one markers.
My problem is that my markers are created in a for loop and I can only retrieve the arguments from the last iteration of the loop.
Here is the loop in my mainActivity:
JSONObject jsonObject = new JSONObject(res);
JSONArray results = jsonObject.getJSONArray("results");
for (int i = 0; i < results.length(); i++ ) {
String name = placeList.get(i).getName();
double lat = placeList.get(i).getLat();
double lng = placeList.get(i).getLng();
map.addMarker(new MarkerOptions()
.position(new LatLng(lat, lng))
.title());
// CustomOnMarkerClickListener(place) implements GoogleMap.OnMarkerClickListener
map.setOnMarkerClickListener(new CustomOnMarkerClickListener(place));
}
Here is my custom OnMarkerClickListener
public class CustomOnMarkerClickListener implements GoogleMap.OnMarkerClickListener {
JSONObject place;
public CustomOnMarkerClickListener(JSONObject place) {
this.place = place;
}
#Override
public boolean onMarkerClick(Marker marker) {
InfoPanelActivity ipa = new InfoPanelActivity();
FragmentManager fm = getSupportFragmentManager();
Bundle args = new Bundle();
args.putString("place", place.toString());
ipa.setArguments(args);
if(findViewById(R.id.textView2) == null) {
fm.beginTransaction()
.add(R.id.info_panel_frame, ipa)
.commit();
} else {
fm.beginTransaction()
.replace(R.id.info_panel_frame, ipa)
.commit();
}
return false;
}
}
And here is how I get the argument in my fragment
Bundle args = getArguments();
String name = args.getString("place");
TextView tv = (TextView) getView().findViewById(R.id.textView2);
tv.setText("test: " + name);
I had really appreciate if you could help, I really don't see how to solve that problem.
Thanks
When you call
map.setOnMarkerClickListener(new CustomOnMarkerClickListener(place));
You are re-setting the click listener, not adding another one. The click listener is on the map itself, not individual marker. At the end of the for loop, the click listener will have the data of "place" for only the last marker. You need to save the place data inside of each Marker and access it in your onMarkerClick() method.
Something like...
for (int i = 0; i < results.length(); i++ ) {
String name = placeList.get(i).getName();
double lat = placeList.get(i).getLat();
double lng = placeList.get(i).getLng();
map.addMarker(new CustomMarkerOptions()
.position(new LatLng(lat, lng))
.place(place)
.title());
}
// CustomOnMarkerClickListener(place) implements GoogleMap.OnMarkerClickListener
map.setOnMarkerClickListener(new CustomOnMarkerClickListener());
Then in your click listener class..
#Override
public boolean onMarkerClick(Marker marker) {
String place = marker.place();
...
}
I want to draw polygon on google maps which may be resized. Any marker removed successfully by onMarkerClick but I can't change marker position by drag it to another place.
Here is my code:
PolygonOptions polygon = new PolygonOptions();
Polygon my_polygon ;
polygon.fillColor(Color.RED);
....
#Override
public boolean onMarkerClick(Marker arg0) {
my_polygon.remove();
arg0.remove();
polygon.getPoints().remove(arg0.getPosition()); //it's ok, marker deleted
return true;
}
#Override
public void onMarkerDrag(Marker marker)
{
polygon.getPoints().remove(marker.getPosition()); //marker still in polygon list
}
#Override
public void onMarkerDragStart(Marker marker)
{
polygon.getPoints().remove(marker.getPosition()); //marker still in polygon list
}
#Override
public void onMarkerDragEnd(Marker marker)
{
my_polygon.remove();
polygon.getPoints().remove(marker.getPosition()); //marker still in polygon list
polygon.add(marker.getPosition());
my_polygon=map.addPolygon(polygon); //created new polygon which contains previous marker position
}
So, how can I remove previus marker points correctly?
First keep all the LatLng points in a arraylist of LatLng.
Dont remove marker in
onMarkerDragStart(Marker marker) even save it to a new LatLng variable. Now at onMarkerDragEnd(Marker marker) , first get the new point and then remove the old one that u were save in the new LatLng variable . At the same time you need to remove the same marker from the arraylist that u have saved and add this new value and then draw a polygon again.
for example if suppose u have declare an arraylist of latlng like this
ArrayList points = new ArrayList();
then draw a ploygon in onMarkerDragEnd(Marker marker) like the following
Polygon polygon = map.addPolygon(new PolygonOptions()
.addAll(points)
.strokeColor(Color.BLACK)
.strokeWidth(5)
.fillColor(0x884d4d4d));
try this. hope it will help. :)
I want to make marker Application using Google Maps but . I Have problem about onMarkerClick using switch Case, Iam using array to add Marker to My Application and when marker OnCLick he can call different Activity for each marker .I Have problom about that.. How I can using onMarkerClick with switch case for my application..??? Please Help . Here is My Code :
public static final String TAG = markerbanyak.TAG;
final LatLng CENTER = new LatLng(43.661049, -79.400917);
class Data {
public Data(float lng, float lat, String title, String snippet, String icon) {
super();
this.lat = (float)lat;
this.lng = (float)lng;
this.title = title;
this.snippet = snippet;
this.icon = icon;
}
float lat;
float lng;
String title;
String snippet;
String icon;
}
Data[] data = {
new Data(-79.400917f,43.661049f, "New New College Res",
"Residence building (new concrete high-rise)", "R.drawable.mr_kun"),
new Data(-79.394524f,43.655796f, "Baldwin Street",
"Here be many good restaurants!", "R.drawable.mr_kun"),
new Data(-79.402206f,43.657688f, "College St",
"Lots of discount computer stores if you forgot a cable or need to buy hardware.", "R.drawable.mr_kun"),
new Data(-79.390381f,43.659878f, "Queens Park Subway",
"Quickest way to the north-south (Yonge-University-Spadina) subway/metro line", "R.drawable.mr_kun"),
new Data(-79.403732f,43.666801f, "Spadina Subway",
"Quickest way to the east-west (Bloor-Danforth) subway/metro line", "R.drawable.mr_kun"),
new Data(-79.399696f,43.667873f, "St George Subway back door",
"Token-only admittance, else use Spadina or Bedford entrances!", "R.drawable.mr_kun"),
new Data(-79.384163f,43.655083f, "Eaton Centre (megamall)",
"One of the largest indoor shopping centres in eastern Canada. Runs from Dundas to Queen.", "R.drawable.mr_kun"),
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.peta);
SupportMapFragment supportMapFragment = (SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.map);
// Getting a reference to the map
mMap = supportMapFragment.getMap();
Marker kuningan = mMap.addMarker(new MarkerOptions()
.position(KUNINGAN)
.title("Kuningan")
.snippet("Kuningan ASRI")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.mr_kun)));
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(KUNINGAN, 2));
// Zoom in, animating the camera.
mMap.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
}
public void wisata(){
if (mMap==null) {
Log.d(TAG, "Map Fragment Not Found or no Map in it!!");
return;
}
for (Data d : data) {
LatLng location = new LatLng(d.lat, d.lng);
Marker wisata =mMap.addMarker(new MarkerOptions()
.position(location)
.title(d.title)
.snippet(d.snippet)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.mr_wis)));
// Let the user see indoor maps where available.
mMap.setIndoorEnabled(true);
// Enable my-location stuff
mMap.setMyLocationEnabled(true);
// Move the "camera" (view position) to our center point.
mMap.moveCamera(CameraUpdateFactory.newLatLng(CENTER));
// Then animate the markers while the map is drawing,
// since you can't combine motion and zoom setting!
final int zoom = 14;
mMap.animateCamera(CameraUpdateFactory.zoomTo(zoom), 2000, null);
mMap.setOnMarkerClickListener(new OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker v) {
// TODO Auto-generated method stub
//(PLEASE HELP ME !!! :))
return false;
}
});
}
}
#Override
public boolean onMarkerClick(Marker v) {
// TODO Auto-generated method stub
//(PLEASE HELP ME !!! :))
if(v.getTitle().contains("New New College Res")){
// do if marker has this title
}else if(v.getTitle().contains("Baldwin Street")){
// do if marker has this title
} // and so on
return false;
}
});
Marker class if final so you can't extend it and add your own attributes. You will have to handle this using your own logic.
You can do this in two ways.
You already have a Data list with all marker data.
1) You could try to set "snippet" attribute of Marker with your array index of Data array and on onMarkerClick you can get Snippet change it back to int and that would be your Data Array's index. So you can get that your clicked Marker and it's Data object and do whatever you want to do.
2) Use HashMap.
It will look something like this
HashMap<Marker, Integer> hashMap = new HashMap<Marker, Integer>();
// now even in your loop where you are adding markers. you can also add that marker to this hashmap with the id of Data array's index.
hashMap.add(marker, indexOfDataArray);
// final in your onMarkerClick, you can just pass marker to hashMap and get indexOfDataArray and base of that do whatever you want to do.
int id = hashMap.get(marker);