I want to know how i can set "OnClick" for a marker on google maps in android.
Example: User touch the marker and My app detect this.
My marker
#Override
public void onMapReady(GoogleMap map) {
map.moveCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(-18.142, 178.431), 2));
map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
map.setMyLocationEnabled(true);
mUiSettings = map.getUiSettings();
mUiSettings.setZoomControlsEnabled(true);
LatLng sydney = new LatLng(-33.867, 151.206);
map.setMyLocationEnabled(true);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));
map.addMarker(new MarkerOptions()
.title("Sydney")
.snippet("The most populous city in Australia.")
.position(sydney));
}
Try with OnMarkerClickListener, like following:
map.setOnMarkerClickListener(new OnMarkerClickListener(){
#Override
public boolean onMarkerClick(Marker arg0) {
// do your stuff
return true;
}});
implement OnMarkerClickListener
public class MarkerDemoActivity extends android.support.v4.app.FragmentActivity
implements OnMarkerClickListener
{
private Marker myMarker;
myMarker = googleMap.addMarker();
}
#Override
public boolean onMarkerClick(final Marker marker) {
if (marker.equals(myMarker))
{
//handle click here
}
}
}
Example
this.googleMap.setOnMarkerClickListener(new OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker arg0) {
Log.d(TAG, arg0.getTitle() + " | " + arg0.getPosition().toString());
return false;
}
});
Related
I'm having trouble putting my current/updated location on the map which has other data. please see the image below. I need it because I will do a distance between the vehicle location and my current location, anyways how to do it? this activity is extended by fragment. I really need your help.
This is my OnMapReady:
public void onMapReady(GoogleMap googleMap) {
mMapProximity = googleMap;
mMapProximity.setBuildingsEnabled(true);
mMapProximity.getUiSettings().setRotateGesturesEnabled(false);
setUpMap();
}
private void setUpMap() {
mMapProximity.clear();
mMapProximity.setMapType(GoogleMap.MAP_TYPE_NORMAL);
mMapProximity.setIndoorEnabled(true);
mMapProximity.setBuildingsEnabled(true);
mMapProximity.getUiSettings().setZoomControlsEnabled(false);
final Double lati = Double.parseDouble(latitudeProxListMap);
final Double longi = Double.parseDouble(longitudeProxListMap);
m = mMapProximity.addMarker(new MarkerOptions()
.position(new LatLng(lati, longi))
.anchor(0.5f, 0.5f)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.pin_logo))
.visible(true));
mMapProximity.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
marker.showInfoWindow();
}
});
mMapProximity.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lati, longi), 17.0f));
mMapProximity.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(final Marker marker) {
if (marker.isInfoWindowShown()) {
marker.hideInfoWindow();
} else {
marker.showInfoWindow();
}
return true;
}
});
mMapProximity.setInfoWindowAdapter(new MarkerInfoWindowAdapter());
m.showInfoWindow();
}
class MarkerInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {
#Override
public View getInfoWindow(Marker marker) {
return null;
}
#Override
public View getInfoContents(Marker marker) {
View v = getActivity().getLayoutInflater().inflate(R.layout.info_vehicle_list_map, null);
TextView title =v.findViewById(R.id.title);
TextView snippet =v.findViewById(R.id.snippet);
snippet.setVisibility(View.GONE);
title.setText("Location: " + location);
return v;
}
}
Implement LocationListener in your activity and than in onLocationChange method update the marker with the current location values. That will put a marker on the current location.
How to implement adding a marker on long press in Google Maps API v3 for android? There are answers about this in Stack Overflow itself but they are for Google Maps API v2.
public class advertiserMap extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_advertiser_map);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
//for searching for a location
public void onMapSearch(View view) {
EditText locationSearch = (EditText) findViewById(R.id.editText);
String location = locationSearch.getText().toString();
List<Address> addressList = null;
if (location != null || !location.equals("")) {
Geocoder geocoder = new Geocoder(this);
try {
addressList = geocoder.getFromLocationName(location, 1);
} catch (IOException e) {
e.printStackTrace();
}
Address address = addressList.get(0);
LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
}
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
As #tyczj pointed out, there's no v3 for Android Google Maps API, so you are probably using v2.
That said, to accomplish what you want, call setOnMapLongClickListener in your mMap object, and add the marker as you want inside onMapLongClick. You should do this in the onMapReady method:
#Override
public void onMapReady(GoogleMap googleMap) {
...
mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
#Override
public void onMapLongClick(LatLng latLng) {
googleMap.addMarker(new MarkerOptions()
.position(latLng)
.title("Your marker title")
.snippet("Your marker snippet"));
}
});
}
// EDIT:
If you want to keep only one marker present at a time, you should declare your marker in the global activity's scope, and then, in onMapLongClick, if marker already exists, instead of creating a new marker, just update it's position:
public class advertiserMap extends FragmentActivity implements OnMapReadyCallback {
// Declare marker globally
Marker myMarker;
...
#Override
public void onMapReady(GoogleMap googleMap) {
...
mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
#Override
public void onMapLongClick(LatLng latLng) {
// First check if myMarker is null
if (myMarker == null) {
// Marker was not set yet. Add marker:
myMarker = googleMap.addMarker(new MarkerOptions()
.position(latLng)
.title("Your marker title")
.snippet("Your marker snippet"));
} else {
// Marker already exists, just update it's position
myMarker.setPosition(latLng);
}
}
});
}
}
If done like this, remember to always check if your marker is not NULL before manipulating it in your code.
Hey guys if you all are using mMap.clear() then remove it . Mostly it work just fine.
I'm try to use startActivity, but it doesn't work.
This is my code:
setOnMarkerClickListener:
try like this...
public class MarkerDemoActivity extends Activity or FragmentActivity
implements OnMarkerClickListener
{
private Marker myMarker;
..............
private void setUpMap()
{
.......
googleMap.setOnMarkerClickListener(this);
myMarker = googleMap.addMarker(new MarkerOptions()
.position(latLng)
.title("My Spot")
.snippet("This is my spot!")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
......
}
#Override
public boolean onMarkerClick(final Marker marker) {
if (marker.equals(myMarker))
{
Intent intent=new Intent(MarkerDemoActivity.this,AnotherActivity.class);
startActivity();
}
}
}
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng chennai = new LatLng(12.9671, 80.2593);
mMap.addMarker(new MarkerOptions().position(chennai).title("Chennai"));
LatLng perungudi = new LatLng(12.97, 80.25);
mMap.addMarker(new MarkerOptions().position(perungudi).title("Perungudi"));
LatLng pallikarnai = new LatLng(12.9377, 80.2154);
mMap.addMarker(new MarkerOptions().position(pallikarnai).title("Pallikarnai"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(chennai,12));
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
if (marker.getTitle().equals("Chennai")){
Intent intent = new Intent(MapsActivity.this, LoginActivity.class);
startActivity(intent);
return false;
}
});
}
I am creating an app where I am clustering multiple request on maps.
I am able to cluster request on map but I want to get click event on cluster. I tried using setOnClusterItemClickListener but I am unable to capture click event in that method.I am also having OnCameraChangeListener in map which runs when I tap on cluster.
Is this causing issue because of both listeners or I am doing something wrong.
Here's is my listener's code
map.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
#Override
public void onCameraChange(CameraPosition cameraPosition) {
cameraChangeHandler.removeCallbacks(throttledRunnable);
cameraChangeHandler.postDelayed(throttledRunnable, 300);
if (marker != null) {
marker.remove();
mClusterManager.clearItems();
}
LatLng latLng = cameraPosition.target;
appSharedPreference.setLatitude(String.valueOf(latLng.latitude));
appSharedPreference.setLongitude(String.valueOf(latLng.longitude));
marker = map.addMarker(new MarkerOptions().position(latLng));
submitData();
}
});
map.setOnMarkerClickListener(mClusterManager);
mClusterManager.setOnClusterItemClickListener(new ClusterManager.OnClusterItemClickListener<MyItem>() {
#Override
public boolean onClusterItemClick(MyItem myItem) {
Toast.makeText(getActivity(),"Hello",Toast.LENGTH_LONG).show();
return false;
}
});
Using the link provided I was able to get this working using following code:
map.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
#Override
public void onCameraChange(CameraPosition cameraPosition) {
if (marker != null) {
marker.remove();
mClusterManager.clearItems();
}
LatLng latLng = cameraPosition.target;
appSharedPreference.setLatitude(String.valueOf(latLng.latitude));
appSharedPreference.setLongitude(String.valueOf(latLng.longitude));
// marker = map.addMarker(new MarkerOptions().position(latLng));
submitData();
pDialouge.hide();
}
});
mClusterManager.setRenderer(new MyClusterRenderer(getActivity(), map, mClusterManager));
map.setOnMarkerClickListener(mClusterManager);
mClusterManager
.setOnClusterClickListener(new ClusterManager.OnClusterClickListener<MyItem>() {
#Override
public boolean onClusterClick(Cluster<MyItem> cluster) {
clickedCluster = cluster;
return false;
}
});
mClusterManager
.setOnClusterItemClickListener(new ClusterManager.OnClusterItemClickListener<MyItem>() {
#Override
public boolean onClusterItemClick(MyItem item) {
clickedClusterItem = item;
return false;
}
});
How can I add a Tab/Touch/Click listener on the Markers I have on my Google Map in android program. For example in the below image I have one marker on google maps and when clicked, I want to bring up a toast saying this has been clicked
http://i.stack.imgur.com/Bc7Lp.png
I have tried onMapClickListener but that does not work.
map.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick(LatLng point) {
}
}
All markers in Google Android Maps Api v2 are clickable. You don't need to set any additional properties to your marker. What you need to do - is to register marker click callback to your googleMap and handle click within callback:
public class MarkerDemoActivity extends android.support.v4.app.FragmentActivity
implements OnMarkerClickListener{
private Marker myMarker;
private void setUpMap()
{
.......
googleMap.setOnMarkerClickListener(this);
myMarker = googleMap.addMarker(new MarkerOptions()
.position(latLng)
.title("My Spot")
.snippet("This is my spot!")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
......
}
#Override
public boolean onMarkerClick(final Marker marker) {
if (marker.equals(myMarker))
{
//handle click here
}
}
}
Use this
map.setOnMarkerClickListener(new OnMarkerClickListener()
{
#Override
public boolean onMarkerClick(Marker arg0)
Toast.makeText(MainActivity.this, arg0.getTitle(),1000).show();// display toast
return true;
}
});
This will help to you :)