I want to open a new Activity about which clicked Marker. But i can't. Because markers are adding automatically and i am getting coordinate from the database.
How do I know which marker clicked??
for(int i=0; i<all.size();i=i+4){
// her kaydin id,lat,lng,title bilgisi alinip ArrayListe atiliyor..
Double latDouble = Double.parseDouble(all.get(i+1));
Double lngDouble = Double.parseDouble(all.get(i+2));
map.addMarker(new MarkerOptions().position(new LatLng(latDouble, lngDouble)).snippet(all.get(i).toString()).title(all.get(i+3)));
You can use OnMarkerClickListener, but if you don't want a listener in each marker and the info is in the marker, you can use OnInfoWindowClickListener like this:
getMap().setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
public void onInfoWindowClick(Marker marker) {
// here you have the Marker to start the activity, for example:
String url = marker.getTitle();
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
Add "ID" to each marker added.
Then, implement OnMarkerClickListener. As given in documentaion:
"You can use an OnMarkerClickListener to listen for click events on
the marker. To set this listener on the map, call
GoogleMap.setOnMarkerClickListener(OnMarkerClickListener). When a user
clicks on a marker, onMarkerClick(Marker) will be called and the
marker will be passed through as an argument."
You will receive a "Marker" object in OnMarkerClickListener. Compare the "ID"s .
If you know order of your data, you can get the order of marker and match them.
I mean, your first line of database represents the marker which id is = 0.
You can get Id of a marker like this.
int order = Integer.parseInt(marker.getId().substring(1));
googleMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
int order = Integer.parseInt(marker.getId().substring(1));
}
});
Related
I want to create multiple markers on a map, each marker will have information, such as location, address..., and when I click the marker, another activity will show up and show the info of the marker I clicked, and I wrote some codes, the result turns out to be that, for different markers, the info transferred from the markers to the activity is the same, the code is like following:
for (final MyMarkerData object: aaa) {
m = googleMap.addMarker(new MarkerOptions()
.position(object.getLatLng())
.title(object.getTitle())
.snippet(object.getSnippet()));
googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker m) {
Intent intent = new Intent(getActivity(), EventInfo.class);
intent.putExtra("title", object.getTitle());
startActivity(intent);
return false;
}
});
}
the code in the activity is like:
Intent intent = getIntent();
String a = intent.getStringExtra("title");
TextView textview = findViewById(R.id.eventInfo);
textview.setText(a);
when I click three different markers, the TextView showed the same info(actually is different), which is the last info in ArrayList aaa, so what's wrong with this?
googleMap use only 1 OnMarkerClickListener for every Marker clicked event so you set it multiple times in a for loop is useless, the last one will override the previous listener. That's why only the last listener is registered.
Two things to refactor here:
- Call setOnMarkerClickListener only 1 time is enough
- Define a HashMap<Marker, object> to get the right object from the clicked marker
HashMap<Object, Marker> markerMap = new HashMap<Object, Marker>();
googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker m) {
Object obj = markerMap.get(m);
// Use obj for correct data object
}
});
for (final MyMarkerData object: aaa) {
marker = googleMap.addMarker(new MarkerOptions()
.position(object.getLatLng())
.title(object.getTitle())
.snippet(object.getSnippet()));
markerMap.put(aaa, marker)
}
I have an activity with a Google Map and some markers placed on the map. Additionaly, i have some TextViews that shows info from the first selected marker on the map.
I want to be able to access information from any selected marker from my map. I want my information from my Textviews to change when I click another marker. Can you tell me what method should I call or what should I do to be able to do that?
Thank you.
Check the docs
and to get the marker title and marker snippet check these links
getTitle ()
getSnippet ()
Add the marker as follows
myMarker = getMap().addMarker(new MarkerOptions()
.position(latLng)
.title("My Spot")
.snippet("This is my spot!"));
then, set the TextView using getTitle() or getSnippet() as follows
tv.setText(myMarker.getTitle());
or
tv.setText(myMarker.getSnippet());
and
to change the text of the TextView each time you click the marker detect the clicks with an onClickListener(). May be like this..
map.setOnMarkerClickListener(new OnMarkerClickListener()
{
#Override
public boolean onMarkerClick(Marker arg0) {
if(marker.isInfoWindowShown()) {
marker.hideInfoWindow();
} else {
marker.showInfoWindow();
}
tv.setText(myMarker.getTitle()); //Change TextView text here like this
return true;
}
});
UPDATE: I have solved the performance issue by adding a previousMarker object. So only the previous clicked marker will be remove and replaced with the default icon. However the info window is still not shown when I click on the marker.
I have a map view and set some markers on it. What I want is when I clicked on a marker, it changes its icon to be a different icon, and when I click on another marker, the previous marker's icon should change to its original one.
What I've done is something like this but it just simply changes the marker icon whenever I click on the marker.
#Override
public boolean onMarkerClick(Marker marker) { //Called when a marker has been clicked or tapped.
LatLng markerPos=marker.getPosition();
String markerLocationName=marker.getTitle();
String markerSubCategoryName=marker.getSnippet();
marker.remove();
MarkerOptions markerOptions =
new MarkerOptions().position(markerPos)
.title(markerLocationName)
.snippet(markerSubCategoryName)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.new_icon));// Changing marker icon
mMap.addMarker(markerOptions);
Log.d("marker","change marker icon"); // can open a dialog window here
return false;
}
So if I click 2 markers, I will get 2 new icons appears, meanwhile what I want is only the current clicked marker changes its icon.
So I've also done something like this by adding 2 more lines of code. It succeeds doing what I want but it has some drawback (see below).
#Override
public boolean onMarkerClick(Marker marker) { //Called when a marker has been clicked or tapped.
mMap.clear();
populateAllMarkersOnMap();//repopulate markers on map
LatLng markerPos=marker.getPosition();
String markerLocationName=marker.getTitle();
String markerSubCategoryName=marker.getSnippet();
marker.remove(); //remove the current clicked marker
MarkerOptions markerOptions =
new MarkerOptions().position(markerPos)
.title(markerLocationName)
.snippet(markerSubCategoryName)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.new_icon));// Changing marker icon
mMap.addMarker(markerOptions); //add marker with new icon into map
return false;
}
The drawback is 1/ it "disable" the info window (the same thing also happen in the first way). 2/ it clear all the markers on map and set all the markers again. Imagine I have 100 markers, should that be a performance problem on every click I do?
The populateAllMarkersOnMap() can be something as simple like this at the moment:
private void populateAllMarkersOnMap(){
setMarker(latA1, lonA1, "A1","A1.1");
setMarker(latA2, lonA2, "A2","A2.1");
// ... (100 times or populated via a loop)
};
So is there a way to get previous clicked marker to change its icon back to default when I click a new marker? Apologise for my English, if you think I should put another title for my question, please help.
Finally I found the best and most simple way. I made a previousMarker object and store the current clicked marker:
#Override
public boolean onMarkerClick(Marker marker) { //Called when a marker has been clicked or tapped.
if(previousMarker!=null){
previousMarker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.dot_icon));
}
marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.ct_icon));
previousMarker=marker; //Now the clicked marker becomes previousMarker
return false;
}
You might be looking for this method probably
Called when the marker's info window is closed.
optional public func mapView(mapView: GMSMapView, didCloseInfoWindowOfMarker marker: GMSMarker)
I found the best and most simple way. I made another marker object and store the current clicked marker enter code here
#Override
public boolean onMarkerClick(Marker marker) { //Called when a marker has been clicked or tapped.
if(previousMarker!=null){
marker2.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.dot_icon));
}
marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.ct_icon));
marker2=marker; //Now the clicked marker becomes previousMarker
return false;
}
this code is remove with click infoWindow
// Setting click event handler for InfoWIndow
googleMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
// Remove the marker
marker.remove();
}
});
but, How remove a single marker by Id without click infoWindow ? i will remove by Button View
Whenever you add the marker to map don't forget to keep its record, like adding it to Map or ArrayList.
your_google_map_obj.addMarker(new MarkerOptions()) //this adds Marker on Google Map, you
should know it always returns Marker object so that you can use it later especially for
removal
so Marker marker=your_google_map_obj.addMarker(new MarkerOptions()) add this marker object to list or map markerArraylist.add(marker); then easily you can extract marker from list by Marker marker=markerArraylist.get(index); and then call marker.remove();
Other way to do it
After adding the marker it is possible to obtain its reference:
Marker marker = map.addMarker(..);
Marker class has remove method, check this documentation
I've been searching for help on implementing OnMarkerClickListener but nothing I've found has worked. This is my marker below and when clicked it only changes colour(light blue). I'm looking for it to open a bigger window so I can put in more info. Is this possible?
googlemap.addMarker(new MarkerOptions()
.position(new LatLng(49.378,-0.3904))
.title("Hello World")
.snippet("This is my test app")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)));
The marker works fine above on my Map but now I would like to click on the marker and for it to open a new activity/page or a bigger window, what ever is easier to work with. As I am a real novice at making apps, If anyone who has successfully got a working example please could you put up a link or some code.
Thanks in advance!
Edit:
From the tutorial that was suggested I have changed some of the MainActivity.java.
I've added in OnMarkerClickListener and have chosen to add unimplemented methods to the Public Class
public class MainActivity extends Activity implements LocationListener, OnMarkerClickListener {
Underneath private void setUpMap() I have added to my code: private Marker myMarker, the setonMarkerclick listener and myMarker =, :
private Marker myMarker;
{
googlemap.setOnMarkerClickListener(this);
myMarker = googlemap.addMarker(new MarkerOptions()
.position(new LatLng(LatLng))
.title("Hello World")
.snippet("My First App")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)));
}
In the unimplemented method at the bottom I have:
#Override
public boolean onMarkerClick(Marker arg0) {
// TODO Auto-generated method stub
return false;
What do I need to change in the public Boolean OnMarkerClick part?
I'm not getting any errors but its just not working. What else do I have to add in or change?
Any help is appreciated!
Marker click events
Don't snap to marker after click in android map v2
Quoting from the above post
You can use an OnMarkerClickListener to listen for click events on the marker. To set this listener on the map, call GoogleMap.setOnMarkerClickListener(OnMarkerClickListener). When a user clicks on a marker, onMarkerClick(Marker) will be called and the marker will be passed through as an argument. This method returns a boolean that indicates whether you have consumed the event (i.e., you want to suppress the default behavior). If it returns false, then the default behavior will occur in addition to your custom behavior. The default behavior for a marker click event is to show its info window (if available) and move the camera such that the marker is centered on the map.
https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/GoogleMap.OnMarkerClickListener.
Use OnMarkerClickListener on your marker.
Check the link for code snippets
Google Maps API v2: How to make markers clickable?
Example: Works on my phone
Marker source, destination;
GoogleMap mMap;
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
source = mMap.addMarker(new MarkerOptions()
.position(sc)
.title("MyHome")
.snippet("Bangalore")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.pin)));
destination = mMap.addMarker(new MarkerOptions()
.position(lng)
.title("MapleBear Head Office")
.snippet("Jayanager")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.pin)));
mMap.setOnMarkerClickListener(marker -> {
if (marker.getTitle().equals("MyHome")) // if marker source is clicked
Toast.makeText(MainActivity.this, marker.getTitle(), Toast.LENGTH_SHORT).show();// display toast
return true;
});
This code handles the maker click event and loads a new layout (XML) with some information:
/**
* adding individual markers, displaying text on on marker click on a
* bubble, action of on marker bubble click
*/
private final void addLocationsToMap() {
int i = 0;
for (Stores store : storeList) {
LatLng l = new LatLng(store.getLatitude(), store.getLongtitude());
MarkerOptions marker = new MarkerOptions()
.position(l)
.title(store.getStoreName())
.snippet("" + i)
.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
googleMap.addMarker(marker);
++i;
}
googleMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
try {
popUpWindow.setVisibility(View.VISIBLE);
Stores store = storeList.get(Integer.parseInt(marker
.getSnippet()));
// set details
email.setText(store.getEmail());
phoneNo.setText(store.getPhone());
address.setText(store.getAddress());
// setting test value to phone number
tempString = store.getPhone();
SpannableString spanString = new SpannableString(tempString);
spanString.setSpan(new UnderlineSpan(), 0,
spanString.length(), 0);
phoneNo.setText(spanString);
// setting test value to email
tempStringemail = store.getEmail();
SpannableString spanString1 = new SpannableString(tempStringemail);
spanString1.setSpan(new UnderlineSpan(), 0, spanString1.length(), 0);
email.setText(spanString1);
storeLat = store.getLatitude();
storelng = store.getLongtitude();
} catch (ArrayIndexOutOfBoundsException e) {
Log.e("ArrayIndexOutOfBoundsException", " Occured");
}
}
});
}
If you need the event Click in a market,this code it's the solution.
private GoogleMap mGoogleMap;
mGoogleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener()
{
#Override
public boolean onMarkerClick(Marker arg0) {
if(arg0 != null && arg0.getTitle().equals(markerOptions2.getTitle().toString())); // if marker source is clicked
Toast.makeText(menu.this, arg0.getTitle(), Toast.LENGTH_SHORT).show();// display toast
return true;
}
});
Good Luck
I suggest use of OnInfoWindowClickListener, it will trigger when you click on marker and then the snippet.
Use setTag to attach any object with the marker.
Marker marker = mMap.addMarker(markerOptions);
marker.setTag(myObject);
and the listener
mMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker arg0) {
MyObject mo = (MyObject )arg0.getTag();
}
});
Below code to used for Kotlin when user click on marker to perform any action
googleMap!!.setOnMarkerClickListener { marker ->
if (marker.title == "Marker1")
Log.d(TAG, "Clicked on Marker1")
true
}