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.
Related
I want to set OnMarkerClickListener of different Markers. Here I want to print i variable value of loop whenever respective marker will get clicked. So I did by following way .. but it is not working , It display same last value 170 of loop on the Snackbar in every different marker click.. But I suppose to get 0,10,20,30....170 respectively in snackbar on different marker click.
Please help...
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// SETTING MARKER
for(int i=0;i<180;i=i+10) {
LatLng sydney = new LatLng(i, i);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Position"+i));
//ON MARKER CLICK
final int finalI = i;
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
Snackbar.make((View) findViewById(R.id.map),""+finalI,Snackbar.LENGTH_LONG).show();
return true;
}
});
}
}
Here is the marker which was created by loop
but I am getting same value to 170
To Resolve your problem you should have a marker array.
Try this:
First make your app to implement GoogleMap.OnMarkerClickListener
Then create a Marker array :
Marker[] marker = new Marker[20]; //change length of array according to you
then inside
onMapReady(){
mMap.setOnMarkerClickListener(this);
for(int i=0;i<180;i=i+10) {
LatLng sydney = new LatLng(i, i);
marker[i] = mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Position"+i));
}
}
then finally
#Override
public boolean onMarkerClick(Marker marker) {
//you can get assests of the clicked marker
return false;
}
I found one way...
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// SETTING MARKER
for(int i=0;i<180;i=i+10) {
LatLng sydney = new LatLng(i, i);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Position"+i));
}
//ON MARKER CLICK
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
for(int i=0;i<180;i=i+10) {
if (marker.getTitle().equals("Marker in Position" + i))
Snackbar.make((View) findViewById(R.id.map), "" + i, Snackbar.LENGTH_LONG).show();
}return true;
}
});
}
I have a map that will only allow 1 marker at a time. But in order to stop that marker being accidentally replaced with another, it needs to be set so that the marker is cleared before another marker can be added.
I am setting the marker via onMapClick and clearing them via onMapLongClick. As it stands at the moment, the clearing and adding of markers works fine, but I cannot seem to set up the map so that you need to clear the map first.
I have tried the solution from check for existing markers but it hasn't worked.
Here is my code for the setup, which currently works by clearing existing marker and adding another without the need to clear the original marker first:
#Override
public void onMapLongClick(LatLng position) {
mMap.clear();
Toast.makeText(this, "Position Cleared", Toast.LENGTH_SHORT).show();
position = null;
}
#Override
public void onMapClick(LatLng position){
if (position != null){
mMap.clear();
mMap.addMarker(new MarkerOptions()
.position(position)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher_new)));
}
else {
mMap.addMarker(new MarkerOptions()
.position(position)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher_new)));
}
}
But I thought it should be something like:
#Override
public void onMapLongClick(LatLng position) {
mMap.clear();
Toast.makeText(this, "Position Cleared", Toast.LENGTH_SHORT).show();
position = null;
}
#Override
public void onMapClick(LatLng position){
if (position == null){
//mMap.clear();
mMap.addMarker(new MarkerOptions()
.position(position)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher_new)));
}
else {
Toast.makeText(this, "Clear first", Toast.LENGTH_SHORT).show();
}
}
But all that does is give me the Toast message and not be able to add a marker at all even on first load of the map.
Any help would be great
Create a reference to the added marker, and remove it before adding it again on a new LatLng position.
private Marker marker;
/**
**
some code here
**
**/
#Override
public void onMapClick(LatLng position) {
if(marker != null)
marker.remove();
marker = mMap.addMarker(new MarkerOptions().position(position).icon(
BitmapDescriptorFactory
.fromResource(R.drawable.ic_launcher_new)));
}
I realised I was using code incorrectly as such I have fixed the issue as thus:
public void onMapClick(LatLng position){
if(marker == null) {
marker = mMap.addMarker(new MarkerOptions()
.position(position)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher_new)));
} else {
Toast.makeText(this, "Please clear previous position before adding another", Toast.LENGTH_SHORT).show();
}
The app now does as intended. Thank you for your help
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");
}
}
});
}
I want to add a marker on map with long press.
Toast in onMapClick() was display with normal tap. But long press is not working. Toast in onMapLongClick() is not displayed with long press. Also marker is not displayed on map.
I'm using SupportMapFragment because I want to use my application on Android 2.x devices. I have tested my app on Nexus One which has Android version 2.3.7.
This is my code.
public class MainActivity extends FragmentActivity implements
OnMapClickListener, OnMapLongClickListener {
final int RQS_GooglePlayServices = 1;
private GoogleMap myMap;
Location myLocation;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager myFragmentManager = getSupportFragmentManager();
SupportMapFragment mySupportMapFragment = (SupportMapFragment) myFragmentManager
.findFragmentById(R.id.map);
myMap = mySupportMapFragment.getMap();
myMap.setMyLocationEnabled(true);
myMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
// myMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
// myMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
// myMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
myMap.setOnMapClickListener(this);
myMap.setOnMapLongClickListener(this);
}
#Override
protected void onResume() {
super.onResume();
int resultCode = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(getApplicationContext());
if (resultCode == ConnectionResult.SUCCESS) {
Toast.makeText(getApplicationContext(),
"isGooglePlayServicesAvailable SUCCESS", Toast.LENGTH_LONG)
.show();
} else {
GooglePlayServicesUtil.getErrorDialog(resultCode, this,
RQS_GooglePlayServices);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public void onMapClick(LatLng point) {
myMap.animateCamera(CameraUpdateFactory.newLatLng(point));
Toast.makeText(getApplicationContext(), point.toString(),
Toast.LENGTH_LONG).show();
}
#Override
public void onMapLongClick(LatLng point) {
myMap.addMarker(new MarkerOptions().position(point).title(
point.toString()));
Toast.makeText(getApplicationContext(),
"New marker added#" + point.toString(), Toast.LENGTH_LONG)
.show();
}
}
How can I solve this ?
I used this. It worked.
GoogleMap gm;
gm.setOnMapLongClickListener(this);
#Override
public void onMapLongClick(LatLng point) {
gm.addMarker(new MarkerOptions()
.position(point)
.title("You are here")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
}
You have no icon setted.
Try:
#Override
public void onMapLongClick(LatLng point) {
myMap.addMarker(new MarkerOptions()
.position(point)
.title(point.toString())
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
Toast.makeText(getApplicationContext(),
"New marker added#" + point.toString(), Toast.LENGTH_LONG)
.show();
}
googleMap = supportMapFragment.getMap();
// Setting a click event handler for the map
googleMap.setOnMapClickListener(new OnMapClickListener() {
#Override
public void onMapClick(LatLng latLng) {
// Creating a marker
MarkerOptions markerOptions = new MarkerOptions();
// Setting the position for the marker
markerOptions.position(latLng);
// Setting the title for the marker.
// This will be displayed on taping the marker
markerOptions.title(latLng.latitude + " : " + latLng.longitude);
// Clears the previously touched position
googleMap.clear();
// Animating to the touched position
googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
// Placing a marker on the touched position
googleMap.addMarker(markerOptions);
}
});
OR
mMap.setOnMapLongClickListener(new
GoogleMap.OnMapLongClickListener() {
#Override
public void onMapLongClick (LatLng latLng){
Geocoder geocoder =
new Geocoder(MapMarkerActivity.this);
List<Address> list;
try {
list = geocoder.getFromLocation(latLng.latitude,
latLng.longitude, 1);
} catch (IOException e) {
return;
}
Address address = list.get(0);
if (marker != null) {
marker.remove();
}
MarkerOptions options = new MarkerOptions()
.title(address.getLocality())
.position(new LatLng(latLng.latitude,
latLng.longitude));
marker = mMap.addMarker(options);
}
});
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
}
});