Calling different dialog activity to different infoWindow click event - android

I'm currently making a Campus Map using Google Maps API v2.
I have currently 10 map markers. Each of them has its info Window.
What I want to happen is that when the user clicks on the info Window, it shows a Listview Custom Dialog. I wanna use the if else statement but I don't know how to construct since I haven't found any examples on the Internet.
This is my Dialog Activity
public class AdminDialog extends DialogFragment{
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.layers)
.setItems(R.array.layer_options, new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
// The 'which' argument contains the index position
// of the selected item
}
});
return builder.create();
}
}
And I have some sample markers...
public void addMarkersToMap() {
Marker cmumarker = map.addMarker(new MarkerOptions()
.position(cmu)
.title("Central Mindanao University")
.snippet("Population: 6,143"));
cmumarker.showInfoWindow();
Marker adminmarker = map.addMarker(new MarkerOptions()
.position(admin)
.title("Central Mindanao University Administration Building")
.snippet("Population: 1,234")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
Marker casmarker = map.addMarker(new MarkerOptions()
.position(cas)
.title("College of Arts and Sciences")
.snippet("Population: 1,234")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
}
My concern is how to construct an if else statement or a switch case statement using this code.. this is where the Custom Dialog is called..
adminDialog = new AdminDialog();
adminDialog.show(getFragmentManager(), "custom-tag-goes-here");

declare your markers as fields, instead of local varaibles
Marker cmumarker , adminmarker, casmarker;
// You can also keep the ids
String cmumarkerId , adminmarkerId, casmarkerID;
public void addMarkersToMap() {
cmumarker = map.addMarker(new MarkerOptions()
.position(cmu)
.title("Central Mindanao University")
.snippet("Population: 6,143"));
cmumarker.showInfoWindow();
cmumarkerId=cmumarker.getID();
adminmarker = map.addMarker(new MarkerOptions()
.position(admin)
.title("Central Mindanao University Administration Building")
.snippet("Population: 1,234")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
adminmarkerId=adminmarker.getID();
casmarker = map.addMarker(new MarkerOptions()
.position(cas)
.title("College of Arts and Sciences")
.snippet("Population: 1,234")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
casmarkerId=casmarker.getID();
}
set the click listener to the markers, for example after you create the map, or at the end of addMarkersToMap()
map.setOnMarkerClickListener(this);
override the onMarkerClick(Marker marker) function, and make your decisions
#Override
public boolean onMarkerClick(Marker marker) {
if (marker.getId().equals(cmumarkerId)) {
//do whatever you want
return true;
}
if (marker.getId().equals(adminmarkerId)) {
//do whatever you want
return true;
}
if (marker.getId().equals(casmarkerId)) {
//do whatever you want
return true;
}
return false;
}
Also, if you want the info window to be clicable, instead of the marker, you
overrride the public void onInfoWindowClick(Marker marker) instead of the onMarkerClick
asign the listener by means of map.setOnInfoWindowClickListener(this); instead of map.setOnMarkerClickListener(this);
UPDATE:
If you have many markers, you can look for a better way of keeping the ids. Some people uses to compare directly the marker, by reference(ie. if (marker==adminmarker{...}) but sometimes it happens to be diferent objects with the same values, so the id is the most secure way of comparing them.

UPDATE for the answers.. Credits to Carlos Robles..
Marker cmumarker, adminmarker, casmarker;
String PREVIOUS_ID;
public void addMarkersToMap() {
cmumarker = map.addMarker(new MarkerOptions()
.position(cmu)
.title("Central Mindanao University")
.snippet("Population: 6,143"));
cmumarker.showInfoWindow();
adminmarker = map.addMarker(new MarkerOptions()
.position(admin)
.title("Central Mindanao University Administration Building")
.snippet("Population: 1,234")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
casmarker = map.addMarker(new MarkerOptions()
.position(cas)
.title("College of Arts and Sciences")
.snippet("Population: 1,234")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
PREVIOUS_ID=adminmarker.getId();
map.setOnInfoWindowClickListener(this);
}
And this is a sample where the Custom Dialog can only be called by adminmarker..
#Override
public void onInfoWindowClick(Marker marker) {
AdminDialog adminDialog;
if (marker.getId().equals(PREVIOUS_ID)) {
adminDialog = new AdminDialog();
adminDialog.show(getFragmentManager(), "custom-tag-goes-here");
}
}

Related

How am I suppose to use OnLongClickListener on specific Marker inside google map

First of all I create Arraylist where I put all my markers.
markerArrayList = new ArrayList<Marker>();
marker1 = mMap.addMarker(new MarkerOptions()
.position(new LatLng(51.1117744, 17.0353596))
.title("Giselle French Bakery Cafe")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
marker1.setDraggable(false);
marker1.setTag(0);
marker2 = mMap.addMarker(new MarkerOptions()
.position(new LatLng(51.110969, 17.031510))
.title("Second Bakery Cafe")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
marker2.setDraggable(false);
marker2.setTag(1);
markerArrayList.add(marker1);
markerArrayList.add(marker2);
Next I have used a LongClickListener on map like here, and try to check which one is clicked:
mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
#Override
public void onMapLongClick(LatLng latLng) {
for (Marker marker : markerArrayList) {
if (Math.abs(marker.getPosition().latitude - latLng.latitude) < 0.005
&& Math.abs(marker.getPosition().longitude - latLng.longitude) < 0.005) {
onMarkerLongClick(marker);
break;
}
}
}
});
but the problem is, that no matter where I use long click on map, it always choose the first one in a row from arraylit.
Your markers are placed closer than the difference you check in onMapLongClick(...), which leaves you with a very small difference window. Try decreasing from 0.005 to 0.00005 or moving first marker further away from the second one (try checking these coordinates: LatLng(51.15, 17.00)).
After check some solution I have found the best answer if the markers are set really close one by one.
Here is the fragment of code which should work properly
mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
#Override
public void onMapLongClick(LatLng latLng) {
for (Marker marker : markerArrayList) {
if (Math.abs(marker.getPosition().latitude - latLng.latitude) < 0.001
&& Math.abs(marker.getPosition().longitude - latLng.longitude) < 0.001) {
onMarkerLongClick(marker);
break;
}
}
}
});

Marker on map after a button click in android

I want to make a marker on my map application after a click event on my activity, I mean when I click on button "accident" a red marker is added to the map With current location coordinates
Thanks
You can do this in your onClickListener:
GoogleMap map = ... // get a map.
Marker marker = map.addMarker(new MarkerOptions()
.position(new LatLng(37.7750, 122.4183))
.title("San Francisco")
.snippet("Population: 776733"));
For further reference, see this answer.
Inside:
#Override
public void onMapReady(GoogleMap arg0) {
....
}
Create a setOnMapClickListener that sets a callback that's invoked when the map is tapped:
mGoogleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener(){
#Override
public void onMapClick(LatLng destination) {
MarkerOptions options = new MarkerOptions();
options.position(destination);
options.title("Lat=" + destination.latitude + ", Long=" + destination.longitude);
Marker marker = mGoogleMap.addMarker(options);
mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(17));
}
});

Google maps marker how saved data

When I paint I need to bind some data marker when the marker, and I need to click on the marker to get data. How do I bind data and received data? i need help! thanks
You can bind your data to a Marker using a HashMap. The key will be the Marker itself and the value will be the data that you need to relate to your Marker (just a String in my example):
private Map<Marker, String> markers = new HashMap<>(); // Map to bind a Strings to Markers
// ...
Marker marker1 = map.addMarker(new MarkerOptions().position(new LatLng(52, 5)));
markers.put(marker1, "One");
Marker marker2 = map.addMarker(new MarkerOptions().position(new LatLng(52.1, 5.1)));
markers.put(marker2, "Two");
map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(final Marker marker) {
Log.i("Marker", markers.get(marker)); // Query the map to get the String related to the clicked Marker
return false;
}
});

how to focus on google marker after clicking item from listview?

This is my marker which is set in - MainAcitivity
googleMap.addMarker(new MarkerOptions()
.position(new LatLng(26.89209, 75.82759))
.title("Store"));
And What i am Trying to do is that in listview when user will click on txtplace field so google marker will focus on that marker which i set
holder.txtPlace.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (holder.txtStore.getText().toString().equals("Store") ) {
//mMap.animateCamera(CameraUpdateFactory.newCameraPosition( what to do here ??? ));
}
}
});
Try using this function for example:
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(26.89209, 75.82759)), 14));

Android Google Map - Clicked marker opens new activity or bigger window

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
}

Categories

Resources