I am trying to center the marker's info window because it's fully visible. There are two images showing the problem. On the first the title of the info window is not visible and on the second image is how it should look like. Here is my code:
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(final Marker marker) {
LatLng markerLocation = marker.getPosition();
Point mappoint = mMap.getProjection().toScreenLocation(new LatLng(markerLocation.latitude, markerLocation.longitude));
mappoint.set(mappoint.x, mappoint.y - 100);
CameraUpdate cu = CameraUpdateFactory.newLatLng(mMap.getProjection().fromScreenLocation(mappoint));
mMap.animateCamera(cu, new GoogleMap.CancelableCallback() {
#Override
public void onFinish() {
if (marker != null)
marker.showInfoWindow();
}
#Override
public void onCancel() {
}
});
return false;
}
});
The problem was the line:
returning false;
Should be:
returning true;
Related
this is the layout code
LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
locationRecycler = findViewById(R.id.locations_area);
locationRecycler.setLayoutManager(layoutManager);
locationAreaAdapter = new LocationAreaAdapter(this, locations);
public void callMapPins(){
for ( markerCount=0 ; markerCount<lat.size();markerCount++){
LatLng sydney = new LatLng(Float.parseFloat(lat.get(markerCount)), Float.parseFloat(lng.get(markerCount)));
// mgoogleMap.addMarker(new MarkerOptions().position(sydney));
markerId.add( mgoogleMap.addMarker(new MarkerOptions().position(sydney)).getId()) ;
mgoogleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
if(markerId.contains(marker.getId())){
// Toast.makeText(insideHostelList.this, "", Toast.LENGTH_SHORT).show();
recyclerView.scrollToPosition(markerCount);
//this is not working
}
return false;
}
});
mgoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney,10f));
}
}
Im storing marker id in a array and then when clicking on a marker comparing the id's and then using the marker count to scroll to a particular postion but it doesn't seems to work can anyone help.
Take the OnMarkerClickListener out of the for loop.Set the markerClickListener after you created and added the markers to the map and stored the ids.
public void callMapPins(){
for (markerCount=0 ; markerCount<lat.size();markerCount++){
LatLng sydney = new LatLng(Float.parseFloat(lat.get(markerCount)), Float.parseFloat(lng.get(markerCount)));
markerId.add( mgoogleMap.addMarker(new MarkerOptions().position(sydney)).getId()) ;
}
mgoogleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
if(markerId.contains(marker.getId())){
// Toast.makeText(insideHostelList.this, "", Toast.LENGTH_SHORT).show();
int index = markerId.indexOf(marker.getId());
recyclerView.scrollToPosition(index);
//this is not working
mgoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(marker.getPosition(),10f));
}
return false;
}
});
}
i want to show the location indicator when the map is loaded and add a marker whenever the map is clicked but none of these seem to work !
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MapFragment mapFragment = (MapFragment) getSupportFragmentManager().findFragmentById(R.id.mapFragment);
assert mapFragment != null;
mapFragment.getMapAsync(new OnMapInitListener() {
#Override
public void onMapReady(MapView mapView) {
OnlineManager.getInstance().enableOnlineMapStreaming(true);
PositionManager.getInstance().startPositionUpdating();
PositionManager.getInstance().enableRemotePositioningService();
mpView=mapView;
mpView.addMapGestureListener(new MapGestureAdapter() {
#Override
public boolean onMapClicked(final MotionEvent e, final boolean isTwoFingers) {
MapMarker marker = new MapMarker(new GeoCoordinates(PositionManager.getInstance().getLastKnownPosition().getLongitudeAccuracy(),PositionManager.getInstance().getLastKnownPosition().getLatitudeAccuracy()));
mpView.addMapObject(marker);
return true;
}
});
}
#Override
public void onMapError(int error, String info) {}
});
}
You’re trying to create new Marker with getLongitudeAccuracy() and getLatitudeAccuracy(). You need to use geo coordinates!
If you want to add the marker to the position of last known gps signal you can use this code:
MapMarker marker = new MapMarker(PositionManager.getInstance().getLastKnownPosition().getCoordinates())
But as there can be no known position at that time it can result in no marker added. So be sure you have location turned on and strong signal. Based on your example it would make more sense to add marker to the position you clicked on. For that purpose use this code:
mpView.addMapGestureListener(new MapGestureAdapter() {
#Override
public boolean onMapClicked(final MotionEvent e, final boolean isTwoFingers) {
MapMarker marker = new MapMarker(mpView.geoCoordinatesFromPoint(e.getX(), e.getY()));
mpView.addMapObject(marker);
return true;
}
});
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;
}
});
}
Im trying to get the zoom out of any markers i make, for this i did a method where i create all the markers with their owns setOnMarkerClickListener , the thing is when i run the app it only does the zoom to miami but not to the others, i dont know what im missing
private float zoomLevel = 15;
public void Veterinarias (GoogleMap googleMap){
rMap = googleMap;
final LatLng bvillia = new LatLng(-31.4223465,-64.1829377);
final LatLng shangai = new LatLng(31.2071315,121.2287258);
final LatLng miami = new LatLng(25.7959734,-80.3209081);
rMap.addMarker(new MarkerOptions().position(bvillia).title("Bv arturo illia"));
rMap.addMarker(new MarkerOptions().position(shangai).title("Shangai"));
rMap.addMarker(new MarkerOptions().position(miami).title("Miami"));
rMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
String name= marker.getTitle();
if(name.equalsIgnoreCase("Bv arturo illia")){
rMap.moveCamera(CameraUpdateFactory.newLatLngZoom(bvillia, zoomLevel));
}
return false;
}
});
rMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
String name= marker.getTitle();
if(name.equalsIgnoreCase("Shangai")){
rMap.moveCamera(CameraUpdateFactory.newLatLngZoom(shangai, zoomLevel));
}
return false;
}
});
rMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
String name= marker.getTitle();
if(name.equalsIgnoreCase("Miami")){
rMap.moveCamera(CameraUpdateFactory.newLatLngZoom(miami, zoomLevel));
}
return false;
}
});
}
}
pd: is there a way to zoom in progressive and not in just one time ?
The Marker Listener gets set on the Map so you're just overriding the previous listeners with the last one you set. If you move your conditions all to one listener, it should work fine.
rMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
String name= marker.getTitle();
if(name.equalsIgnoreCase("Bv arturo illia")){
rMap.moveCamera(CameraUpdateFactory.newLatLngZoom(bvillia, zoomLevel));
}else if(name.equalsIgnoreCase("Shangai")){
rMap.moveCamera(CameraUpdateFactory.newLatLngZoom(shangai, zoomLevel));
}else if(name.equalsIgnoreCase("Miami")){
rMap.moveCamera(CameraUpdateFactory.newLatLngZoom(miami, zoomLevel));
}
return false;
}
});
I have application with google map v2 containing couple of MarkerOptions inside it. Is there a way to catch tap on info box that is opened after pin is tapped?
mMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
// do something
}
});
Probably duplicate question.
Try out as below:
GoogleMap mMap;
Marker myMarker= mMap.addMarker(new MarkerOptions()
.position(lng)
.title("Head Quarter Office")
.snippet("Delhi")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.icon)));
mMap.setOnMarkerClickListener(new OnMarkerClickListener()
{
#Override
public boolean onMarkerClick(Marker arg0) {
if(arg0.getTitle().equals("Marker")) // if marker source is clicked
Toast.makeText(MainActivity.this, arg0.getTitle(),1000).show();// display toast
return true;
}
});