I'm looking to create a method that runs when a marker's info box/snippet is clicked. As yet, I can't find anything about this, only the marker. Any help or links as to where I could find some more info?
To create a method that runs when you click the info window you need to setOnInfoWindowClickListener:
map.setInfoWindowAdapter(new InfoWindowAdapter() {
// Use default InfoWindow frame
#Override
public View getInfoWindow(Marker args) {
return null;
}
// Defines the contents of the InfoWindow
#Override
public View getInfoContents(Marker args) {
// Getting view from the layout file info_window_layout
View 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(new OnInfoWindowClickListener() {
public void onInfoWindowClick(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 = new Intent(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();
}
}
});
Just set OnInfoWindowClickListener for your Map.
map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
//YOUR CODE
}
});
Related
I have this onClick method inside an adapter
#Override
public void onClick(final View view) {
// Clicked postion
AsyncTask.execute(new Runnable() {
#Override
public void run() {
int position = getAdapterPosition();
PlaceSaved place = items.get(position);
// Here is the data Do your stuff
Snackbar.make(view, "Position is:"+place.getTitle()+","+place.getLongi(), Snackbar.LENGTH_LONG).show();
latlang.Lat = Double.parseDouble(place.getTitle());
latlang.Lang = Double.parseDouble(place.getLongi());
lStatus.LOCATION_STATUS_UPDATE = 1;
Intent i = new Intent(view.getContext(), MainActivity.class);
view.getContext().startActivity(i);
SecondFragment sf = new SecondFragment();
sf.showMap();
}
});
}
The showMap method is defined as:
public void showMap(){
Log.e("ShowMap", "is called");
if (marker != null) {
marker.remove();
mMap.clear();
}
if (mMap != null) {
updateLocationUI();
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(latlang.Lat, latlang.Lang), DEFAULT_ZOOM));
marker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(latlang.Lat, latlang.Lang))
.anchor(0.5f, 0.5f)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.location_icon))
);
}
new SunFragment();
}
with onclick, showMap is called properly and also the sunFragment, method.
But this has no effect on mMap related methods e.g. moveCamera and marker.
They remain in the same position.
How I can move the camera? And more than that, why its not moving at all?
I'm using google's android Map Utils and below is the code I use to try to generate an info window.
/*
* Set the Google Marker Cluster Manager.
*/
mClusterManager = new ClusterManager<>(this, mGoogleMap);
mGoogleMap.setOnCameraIdleListener(mClusterManager);
mGoogleMap.setInfoWindowAdapter(mClusterManager.getMarkerManager());
mGoogleMap.setOnMarkerClickListener(mClusterManager.getMarkerManager());
mGoogleMap.setOnInfoWindowClickListener(mClusterManager);
mGoogleMap.setInfoWindowAdapter(mPlaceInfoWindowAdapter);
mGoogleMap.setOnMarkerClickListener(mClusterManager);
GoogleMap.InfoWindowAdapter infoWindowAdapter = new GoogleMap.InfoWindowAdapter()
{
#Override
public View getInfoWindow(Marker marker)
{
Toast.makeText(MainActivity.this, "getInfoWIndow", Toast.LENGTH_SHORT).show();
return null;
}
#Override
public View getInfoContents(Marker marker)
{
View v = LayoutInflater.from(MainActivity.this).inflate(R.layout.map_infowindow_layout, null);
Toast.makeText(MainActivity.this, "getInfoContents", Toast.LENGTH_SHORT).show();
return v;
}
};
mClusterManager.getMarkerCollection().setOnInfoWindowAdapter(infoWindowAdapter);
mClusterManager.getClusterMarkerCollection().setOnInfoWindowAdapter(infoWindowAdapter);
mClusterManager.setOnClusterClickListener(new ClusterManager.OnClusterClickListener<MapMarker>()
{
#Override
public boolean onClusterClick(Cluster<MapMarker> cluster)
{
Toast.makeText(MainActivity.this, "cluster click", Toast.LENGTH_SHORT).show();
return false;
}
});
mClusterManager.setOnClusterItemClickListener(new ClusterManager.OnClusterItemClickListener<MapMarker>()
{
#Override
public boolean onClusterItemClick(MapMarker mapMarker)
{
Toast.makeText(MainActivity.this, "cluster item click", Toast.LENGTH_SHORT).show();
return false;
}
});
Below is the image of the result when clicking on a marker on the map.
The methods of the InfoWindowAdapter are never called.
I've also tried to set the mGoogleMap.setOnMarkerClickListener(mClusterManager) but that doesn't work either. Am I doing something wrong or is this a bug with google's code?
I fixed this by returning false from the onClusterItemClick method. If set to true, the info window doesn't show.
I try to make a if statement for a GoogleMap OnInfoWindowClickListener.
But everytime I come to the else.
And hint?
Marker s780 = googleMap.addMarker(new MarkerOptions()
.position(new LatLng(52.16033, 7.87964))
.title("780")
.icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_780)));
Marker s149 = googleMap.addMarker(new MarkerOptions()
.position(new LatLng(52.60861, 7.99206))
.title("149")
.icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_149)));
googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
if (marker.equals("s780"))
Toast.makeText(getActivity(), "Marker 727", Toast.LENGTH_SHORT).show();
if (marker.equals("s149"))
Toast.makeText(getActivity(), "Marker 725", Toast.LENGTH_SHORT).show();
else
Toast.makeText(getActivity(), "Argument:"+marker, Toast.LENGTH_SHORT).show();
In your code: if (marker.equals("s780")), marker is of type Marker, while "s780" is of type String. Of course equals returns false. You should change it to something like:
final Marker s780 = ...
googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
if (marker == s780) {
...
}
}
}
You should work with the getId() function of the Marker class.
For example inside the :
public void onInfoWindowClick(Marker marker) {
StringBuilder builder = new StringBuilder(marker.getId());
String str = builder.deleteCharAt(0).toString() ;
int id = Integer.parseInt(str) ;
if (id == 0)
Toast.makeText(getActivity(), "Marker 780 was pressed", Toast.LENGTH_LONG).show();
}
Then you can do a simple comparison with ints which is much easier.
The ids of the markers are in order of when they were added to the map, starting by 0. So your marker s780 has the id 0 and the marker s149 has the id 1.
I hope that helped
You just missed an else, so even if the first if (s780) was true, you would make a Toast with
Marker 727
then check again for if (s149), but eventually go inside the else and make a Toast with
Argument:s780
googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
if (marker.equals("s780"))
Toast.makeText(getActivity(), "Marker 727", Toast.LENGTH_SHORT).show();
else //You missed this else
if (marker.equals("s149"))
Toast.makeText(getActivity(), "Marker 725", Toast.LENGTH_SHORT).show();
else
Toast.makeText(getActivity(), "Argument:"+marker, Toast.LENGTH_SHORT).show();
I have found a way for me:
if (marker.getTitle().equals("555 Test"))
{ Toast.makeText(getActivity(), "Marker 5", Toast.LENGTH_SHORT).show();}
Thank you for your hinds.
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 want to call a new activity when clicked on the marker's title and not on marker itself.
map.setOnMarkerClickListner
is called only on the click of the marker.
But I dont want to do that. I want the marker to show the title and snippet on the click of the marker but I want to call new activity on the click of the title.
Any idea how we do that?
Thanks
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(new InfoWindowAdapter() {
// Use default InfoWindow frame
#Override
public View getInfoWindow(Marker args) {
return null;
}
// Defines the contents of the InfoWindow
#Override
public View getInfoContents(Marker args) {
// Getting view from the layout file info_window_layout
View 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(new OnInfoWindowClickListener() {
public void onInfoWindowClick(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 = new Intent(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 contents
return v;
}
});
To set a title on a marker:
marker.showInfoWindow();
To set a click listener on title:
googleMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker arg0) {
// TODO Auto-generated method stub
}
});
GoogleMap mGoogleMap;
mGoogleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker arg0) {
Intent intent = new Intent(getBaseContext(), Activity.class);
String reference = mMarkerPlaceLink.get(arg0.getId());
intent.putExtra("reference", reference);
// Starting the Activity
startActivity(intent);
Log.d("mGoogleMap1", "Activity_Calling");
}
});
/**
* 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");
}
}
});
}
So, I have set infowindowadapter for my Google map, but the problem is that when I click on the first marker, it shows the information correctly, but when I hit the second marker it shows information from the first one, so infowindowadapter doesn't refresh.
Can someone tell me why that it and how to fix it?
I was following this post to set infowindowadapter:
custom info window adapter with custom data in map v2
EDIT:
new getMarkers().execute();
mMap.setOnMarkerClickListener(new OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
marker.showInfoWindow();
return false;
}
});
mMap.setInfoWindowAdapter(new InfoWindowAdapter() {
#Override
public View getInfoWindow(Marker marker) {
// TODO Auto-generated method stub
return null;
}
#Override
public View getInfoContents(Marker marker) {
View view = getLayoutInflater().inflate(R.layout.post_details_on_map,null);
date = (TextView)view.findViewById(R.id.txtMarkerDate);
comment = (TextView)view.findViewById(R.id.txtMarkerComment);
image = (ImageView)view.findViewById(R.id.ivMarkerPicture);
MyMarkerInfo mmi = markerMap.get(marker.getId());
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id",mmi.getId()));
MarkerDetails mDetails = new JSONAdapter().getMarkerDetails(params);
date.setText(mDetails.getDate());
comment.setText(mDetails.getComment());
new getPicture().execute(mDetails.getImageUrl());
return view;
}
});
}
Here is my code for exactly the same operation:
// Setting a custom info window adapter for the google map
map.setInfoWindowAdapter(new InfoWindowAdapter() {
// Use default InfoWindow frame
#Override
public View getInfoWindow(Marker args) {
return null;
}
// Defines the contents of the InfoWindow
#Override
public View getInfoContents(Marker args) {
// Getting view from the layout file info_window_layout
View 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(new OnInfoWindowClickListener() {
public void onInfoWindowClick(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;
}
}
/*
findDirections(SGTasksListAppObj.getInstance().currentUserLocation.getLatitude(),
SGTasksListAppObj.getInstance().currentUserLocation.getLongitude(),
clickMarkerLatLng.latitude, clickMarkerLatLng.longitude, GMapV2Direction.MODE_DRIVING );
*/
Intent intent = new Intent(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 contents
return v;
}
});
My guess would be that your are getting other markers information the wrong way. try to compare between my method and yours, and try to log the process to see that you are passing the right Id and getting the right info.