How To Get Click Event Of The Marker Text
I am displaying google map api v2 in my app. I have set some markers in the map. I have also set title and snippet on the markers which are shown when you click the marker. Now I w
Solution 1:
To achieve this you need to implement setOnInfoWindowClickListener
in your getInfoContents
method so that a click on your infoContents
window will wake the listener to do what you want, you do it like so:
map.setInfoWindowAdapter(newInfoWindowAdapter() {
// Use default InfoWindow frame@OverridepublicViewgetInfoWindow(Marker args) {
returnnull;
}
// Defines the contents of the InfoWindow@OverridepublicViewgetInfoContents(Marker args) {
// Getting view from the layout file info_window_layoutView v = getLayoutInflater().inflate(R.layout.info_window_layout, null);
// Getting the position from the marker
clickMarkerLatLng = args.getPosition();
TextView title = (TextView) v.findViewById(R.id.tvTitle);
title.setText(args.getTitle());
map.setOnInfoWindowClickListener(newOnInfoWindowClickListener() {
publicvoidonInfoWindowClick(Marker marker)
{
if (SGTasksListAppObj.getInstance().currentUserLocation!=null)
{
if (String.valueOf(SGTasksListAppObj.getInstance().currentUserLocation.getLatitude()).substring(0, 8).contains(String.valueOf(clickMarkerLatLng.latitude).substring(0, 8)) &&
String.valueOf(SGTasksListAppObj.getInstance().currentUserLocation.getLongitude()).substring(0, 8).contains(String.valueOf(clickMarkerLatLng.longitude).substring(0, 8)))
{
Toast.makeText(getApplicationContext(), "This your current location, navigation is not needed.", Toast.LENGTH_SHORT).show();
}
else
{
FlurryAgent.onEvent("Start navigation window was clicked from daily map");
tasksRepository = SGTasksListAppObj.getInstance().tasksRepository.getTasksRepository();
for (Task tmptask : tasksRepository)
{
String tempTaskLat = String.valueOf(tmptask.getLatitude());
String tempTaskLng = String.valueOf(tmptask.getLongtitude());
Log.d(TAG, String.valueOf(tmptask.getLatitude())+","+String.valueOf(clickMarkerLatLng.latitude).substring(0, 8));
if (tempTaskLat.contains(String.valueOf(clickMarkerLatLng.latitude).substring(0, 8)) && tempTaskLng.contains(String.valueOf(clickMarkerLatLng.longitude).substring(0, 8)))
{
task = tmptask;
break;
}
}
Intent intent = newIntent(getApplicationContext() ,RoadDirectionsActivity.class);
intent.putExtra(TasksListActivity.KEY_ID, task.getId());
startActivity(intent);
}
}
else
{
Toast.makeText(getApplicationContext(), "Your current location could not be found,\nNavigation is not possible.", Toast.LENGTH_SHORT).show();
}
}
});
// Returning the view containing InfoWindow contentsreturn v;
}
});
Solution 2:
To set a title on a marker:
marker.showInfoWindow();
To set a click listener on title:
googleMap.setOnInfoWindowClickListener(newOnInfoWindowClickListener() {
@OverridepublicvoidonInfoWindowClick(Marker arg0) {
// TODO Auto-generated method stub
}
});
Solution 3:
GoogleMap mGoogleMap;
mGoogleMap.setOnInfoWindowClickListener(newGoogleMap.OnInfoWindowClickListener() {
@OverridepublicvoidonInfoWindowClick(Marker arg0) {
Intentintent=newIntent(getBaseContext(), Activity.class);
Stringreference= mMarkerPlaceLink.get(arg0.getId());
intent.putExtra("reference", reference);
// Starting the Activity
startActivity(intent);
Log.d("mGoogleMap1", "Activity_Calling");
}
});
Solution 4:
/**
* adding individual markers, displaying text on on marker click on a
* bubble, action of on marker bubble click
*/privatefinalvoidaddLocationsToMap() {
inti=0;
for (Stores store : storeList) {
LatLngl=newLatLng(store.getLatitude(), store.getLongtitude());
MarkerOptionsmarker=newMarkerOptions()
.position(l)
.title(store.getStoreName())
.snippet("" + i)
.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
googleMap.addMarker(marker);
++i;
}
googleMap.setOnInfoWindowClickListener(newOnInfoWindowClickListener() {
@OverridepublicvoidonInfoWindowClick(Marker marker) {
try {
popUpWindow.setVisibility(View.VISIBLE);
Storesstore= 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();
SpannableStringspanString=newSpannableString(tempString);
spanString.setSpan(newUnderlineSpan(), 0,
spanString.length(), 0);
phoneNo.setText(spanString);
// setting test value to email
tempStringemail = store.getEmail();
SpannableStringspanString1=newSpannableString(tempStringemail);
spanString1.setSpan(newUnderlineSpan(), 0, spanString1.length(), 0);
email.setText(spanString1);
storeLat = store.getLatitude();
storelng = store.getLongtitude();
} catch (ArrayIndexOutOfBoundsException e) {
Log.e("ArrayIndexOutOfBoundsException", " Occured");
}
}
});
}
Post a Comment for "How To Get Click Event Of The Marker Text"