I want to register a OnMarkerClickListener to a GoogleMap:
implement the GoogleMap.OnMarkerClickListener interface
register listener via GoogleMap.setOnMarkerClickListener(this)
but when I click on a marker the callback onMarkerClick() is never called. For test purposes I also implemented OnMapLongClickListener and it works perfectly well.
public class ShowCoords extends FragmentActivity implements
OnMapReadyCallback,
LocationListener,
GoogleMap.OnCameraMoveListener,
GoogleMap.OnCameraMoveStartedListener,
GoogleMap.OnMyLocationButtonClickListener,
GoogleMap.OnMyLocationClickListener,
GoogleMap.OnMarkerClickListener,
GoogleMap.OnMapLongClickListener {
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.show_coords);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.getUiSettings().setScrollGesturesEnabled(true);
mMap.getUiSettings().setCompassEnabled(true);
mMap.getUiSettings().setMapToolbarEnabled(false);
if (gpsPermission()) {
mMap.setMyLocationEnabled(true);
}
mMap.setOnMyLocationButtonClickListener(this);
mMap.setOnMyLocationClickListener(this);
mMap.setOnCameraMoveStartedListener(this);
mMap.setOnCameraMoveListener(this);
mMap.setOnMarkerClickListener(this);
mMap.setOnMapLongClickListener(this);
if (displayFormat.equals(getString(R.string.heatmapValue))) {
// display markers as heatmap
} else if (displayFormat.equals(getString(R.string.clusterValue))) {
// diplay markers as cluster
mClusterManager = new ClusterManager<>(this, mMap);
mMap.setOnCameraIdleListener(mClusterManager);
// this causes the problem
mMap.setOnMarkerClickListener(mClusterManager);
for (LatLng p : coords) {
mClusterManager.addItem(new MyClusterItem(p));
}
} else {
Log.w(TAG, "onMapReady: coords still null, nothing to display");
}
//just for testing
mMap.addMarker(new MarkerOptions().position(new LatLng(51.866404, 12.643411)));
}
#Override
public boolean onMarkerClick(Marker marker) {
Log.i(TAG, "onMarkerClick");
return true;
}
#Override
public void onMapLongClick(LatLng latLng) {
Log.i(TAG, "onMapLongClick");
}
// removed other callbacks
}
Any ideas, whats going wrong?
I got it. When I display the markers as a cluster (via Google Maps Android Marker Clustering Utility) the clustermanager registers as OnMarkerClickListener and the registration of my listener get lost. As a workaround, I omit the clustermanager registration and pass the click event on the clustermanager by myself.
Related
I have a google map in my app and I handle clicks on it. Everything is fine but if I click on a marker or really close to it onMarkerClick() is called, not onMapClick() and then I don't have the exact location of the place I tapped because marker.getPosition() returns the center point of the marker what's not the same.
Is it possible to disable onMarkerClick() and have onMapClick() called even if I click on a marker?
Return true for disabling marker click event
googleMap.setOnMarkerClickListener(new OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
return true;
}
});
For getting click on maps use this listener on onMapReady
#Override
public void onMapReady(GoogleMap googleMap) {
googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener()
{
#Override
public void onMapClick(LatLng arg0)
{
Log.v("onMapClick", "Yea worked!");
}
});
}
Complete code
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// 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);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
#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));
googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick(LatLng latLng) {
Toast.makeText(MapsActivity.this, "Map clicked", Toast.LENGTH_SHORT).show();
}
});
googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
Toast.makeText(MapsActivity.this, "Marker disabled", Toast.LENGTH_SHORT).show();
return true;
}
});
}
}
It is possible.Here is a simple solution.
First of all you should declare GoogleMap.OnMarkerClickListener and GoogleMap.OnMapClickListener in your activity or fragment like this.
public class MapActivity extends FragmentActivity implements
GoogleMap.OnMarkerClickListener,
GoogleMap.OnMapClickListener
{
//..
}
And then register that interfaces in your onCreate() method.
#Override
protected void onCreate(Bundle savedInstanceState)
{
//..
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
mMap.setOnMarkerClickListener(this);
mMap.setOnMapClickListener(this);
}
Finally you can implement two interfaces.You should call onMapClick() method in onMarkerClick() method to achieve that even if you click on a marker like that.Don't forget to return true in onMarkerClick() method.
#Override
public void onMapClick(LatLng latLng) {
Log.v("onMapClick", "Map Clicked..")
}
#Override
public boolean onMarkerClick(Marker marker) {
onMapClick(marker.getPosition());
return true;
}
I have this code, that allows a user to select a location (mechanic_location) and passes it to another activity(mechanic_login). The problem is, it does not zoom. Help me enable it to zoom to the current location the user is in but give the user the ability to pick another location different from the one that he/she is in. Once the location is picked, click a button to send the latitude and longitude to the view (mechanic_login). Here is my code
public class mechanic_location extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
Button mSendLocationBtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mechanic_location);
// 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);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick(LatLng latLng) {
Intent returnIntent = new Intent();
returnIntent.putExtra("picked_point",latLng);
setResult(Activity.RESULT_OK,returnIntent);
finish();
}
});
}
}
You can use the moveCamera() Method and defined the zoom level
float zoomLevel = 16.0f; //This goes up to 21
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, zoomLevel));
for mor information about using google map, you can check it here
source
I extend my app with google map activity. it created a mapclass and extends FragmentActivity implements OnMapReadyCallback.
the onMapReadyCallBack return GoogleMap object as below code.
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
public GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user);
// 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);
}
#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));
}
}
I want to access mMap on my MainActivity class to do some operation. i create a button with onclick attribute to gotolocation method. but it return error becuase the map object is null; which is the proper way to access the GoogleMap object in my activity class.
public void gotoLocation(View v){
if (mMap != null){
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Kabul"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}else{
Log.d("Map object", "It si null==============");
}
}
Keep in mind that only one Activity at a time can be shown to the user, so there is no need to access the mMap variable from MainActivity.
Assuming you want to open up MapsActivity when the gotoLocation() button is clicked, you can pass the data associated with the location from MainActivity over to MapsActivity.
First, prepare the LatLng and description in MainActivity in order to send it to MapsActivity when the button is clicked:
public void gotoLocation(View v){
LatLng goToLocation = new LatLng(34.5392354, 69.1378334);
Bundle args = new Bundle();
args.putParcelable("latLon", goToLocation);
args.put("desc", "Marker in Kabul");
Intent i = new Intent(this, MapsActivity.class);
i.putExtras(args);
startActivity(i);
}
Then, when MapsActivity is opened, it will retrieve the info, and modify the Marker accordingly:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
public GoogleMap mMap;
LatLng mLatLng;
String mDescription;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user);
//Get the data sent from MainActivity
Intent intent = getIntent();
mLatLng = intent.getParcelableExtra("latLon");
mDescription = intent.getStringExtra("desc");
// 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);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
//Use the data sent from MainActivity:
if (mLatLng != null) {
// Add a marker for location/description sent from MainActivity
mMap.addMarker(new MarkerOptions().position(mLatLng).title(mDescription));
mMap.moveCamera(CameraUpdateFactory.newLatLng(mLatLng));
}
}
}
In addition to first answer, the mMap object is null because it is yet to be instantiated and onMapReady callback has not yet been called.
take a look at the docs for OnMapReadyCallback interface https://developers.google.com/android/reference/com/google/android/gms/maps/OnMapReadyCallback
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 made a code.
In this code, when i click a map, there will be a marker on clicked point.
This is my Code
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(final GoogleMap googleMap) {
mMap = googleMap;
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
mMap.setMyLocationEnabled(true);
} else {
Toast.makeText(getApplicationContext(), "oh, no", Toast.LENGTH_LONG).show();
}
googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick(LatLng point) {
String lat = String.valueOf(point.latitude);
String lng = String.valueOf(point.longitude);
MarkerOptions marker = new MarkerOptions().position(
new LatLng(point.latitude, point.longitude)).title("ok");
mMap.addMarker(marker);
}
});
}
}
Question :
What i want is that when i click SetMylocationEnable button, there also added a new marker. And because i want marker is only one in whole map, another marker that has been in the map before is to be removed. How can i do it? Would you teach me?
You can see what button i saying is, in picture. (picture is from : Enable my location icon Googlemap v2)
mMap.setOnMyLocationButtonClickListener(new OnMyLocationButtonClickListener() {
#Override
public boolean onMyLocationButtonClick() {
Location location = getLocation();
MarkerOptions marker = new MarkerOptions().position(
new LatLng(location.getLatitude(), location.getLongitude())).title("ok");
mMap.addMarker(marker);
return true;
}
});
private Location getLocation() {
LocationManager locationManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
return locationManager.getLastKnownLocation(locationManager
.getBestProvider(criteria, false));
}
keep a reference to the marker, if the reference is null then create the marker as you have done, if it is not, then edit the marker and change its location