Android Map show marker Title and Snippet - java

I have a google map in my android app and want to show the marker title when it loads here is what I have
MarkerOptions markerOptions = new MarkerOptions().position(
new LatLng(lat, lng)).title(name);
mMap.addMarker(markerOptions);
But then how can I show the maker title?
Thanks
EDIT
Right now I have
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(lat, lng)).zoom(14).build();
mMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
marker.showInfoWindow();
But then the marker is cut off, how can I have it so the marker is also seen

To show the Marker title programmatically, you can just do this
myMarker=mMap.addMarker(markerOptions);
myMarker.showInfoWindow();
UPDATE
To make the infoWindow fit inside the view, try
int zoom = (int)mMap.getCameraPosition().zoom;
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng), zoom), 4000, null);
Please dont forget to add this before opening the info window.

Related

Marker OnClick; adding custom information into infoWindow by the user

I am creating an app in which a user can place a marker and put inside an infoWindow his own information and a picture from a gallery? Is this possible? Do I use another Activity, where do I store this information?
To set a custom marker image, you have to use the image in a bitmap format.
This is an example that add the marker, his custom image and a tittle description:
Mymarker = googleMap;
LatLng markerPosition = new LatLng(Lat, Long);
BitmapDrawable dr = (BitmapDrawable) getResources().getDrawable(R.drawable.my_image);
Bitmap bitmapD = Bitmap.createScaledBitmap(dr.getBitmap(), 200, 200, false);
Mymarker.addMarker(new MarkerOptions().snippet("My Snippet").icon(BitmapDescriptorFactory.fromBitmap(bitmapD)).position(markerPosition).title("Marker"));
Mymarker.moveCamera(CameraUpdateFactory.newLatLng(markerPosition));

How to update marker positions with data from Firebase Google Maps API Android

I'm creating an app that makes a real time tracking of users using Firebase Database. Every user is displayed in the map using markers.
When there is a new location update, the marker has to be updated. The problem is that with Firebase method onDataChange() or similars every time a new location update is retrieved, I can't access to the old marker to remove it and create a new one or just update the old marker, because the marker doesn't exist.
I tried it saving markers in SharedPreferences using Gson, but when I pass the marker to json, the app crashes.
Does anyone know how can I update the markers?
This is written inside the onDataChange():
for (User user: usersList) {
Gson gson = new Gson();
/*
String previousJson = preferences.getString(user.getId()+"-marker", "");
Marker previousMarker = gson.fromJson(previousJson, Marker.class);
if (previousMarker!=null) markerAnterior.remove();
*/
final LatLng latlng = new LatLng(user.getLastLocation().getLatitude(), user.getLastLocation().getLongitude());
MarkerOptions markerOptions = new MarkerOptions()
.position(latlng)
.title(user.getId());
Marker marker= mMap.addMarker(markerOptions);
// String newJson = gson.toJson(marker); //CRASH
// editor.putString(user.getId()+"-marker", newJson);
// editor.commit();
}
Thank you.
Create a HashMap instance variable that will map user IDs to Markers:
private Map<String, Marker> mMarkerMap = new HashMap<>();
Then, populate new Markers in the HashMap so you can retrieve them later.
If the Marker already exists, just update the location:
for (User user : usersList) {
final LatLng latlng = new LatLng(user.getLastLocation().getLatitude(), user.getLastLocation().getLongitude());
Marker previousMarker = mMarkerMap.get(user.getId());
if (previousMarker != null) {
//previous marker exists, update position:
previousMarker.setPosition(latlng);
} else {
//No previous marker, create a new one:
MarkerOptions markerOptions = new MarkerOptions()
.position(latlng)
.title(user.getId());
Marker marker = mMap.addMarker(markerOptions);
//put this new marker in the HashMap:
mMarkerMap.put(user.getId(), marker);
}
}

Android Google Maps - remove marker with icon

I want to remove a Marker on long click, and place it in another place without clearing the whole map.
Right now the circle radius works fine, but the custom pin icon is not removed.
using the GoogleMap.clear() method should not be used - there are some other objects on the map I don't want refreshed.
I have also tried setPosition method and had the very same result.
How to erase this icon?
Marker declaration:
marker = googleMap.addMarker(new MarkerOptions().position(marker.getPosition()).draggable(true).title("INFO")
.infoWindowAnchor(0.5f,0.5f).visible(false)
.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.pin_icon))));
this.googleMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
#Override
public void onMapLongClick(LatLng latLng) {
if (marker != null) {
marker.remove();
}
createMarker(latLng);
}
});
private void createMarker(LatLng latLng) {
marker = googleMap.addMarker(new MarkerOptions().position(latLng).draggable(true).title("INFO")
.infoWindowAnchor(0.5f,0.5f)
.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.ic_pin_gr))));
}
I used such approach for creating markers and simply setPosition(new Latlng(...)) working.
marker = mGoogleMap.addMarker(new MarkerOptions()
.anchor(0.5f, 0.5f)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_pin))
.position(new LatLng(cab.getLatitude(), cab.getLongitude())));
Did you try to replace marker.remove(); on setPosition() inside onLongClick() method ?

Android - Google Map make marker sticky

I have following marker code on my MapFragment. I want this marker stick to it's position. Means I want marker at center position in map and when I move/drag map then marker shouldn't get moved. How to do that with following marker?
PlaceMarker = PlaceMap.addMarker(
new MarkerOptions()
.position(new LatLng(0, 0))
.draggable(true)
.title("Drag Me"));
PlaceMarker.showInfoWindow();
Thanks
You can add a listener when the user pans the map and set the marker on the center.
mMap.setOnCameraChangeListener(new OnCameraChangeListener() {
public void onCameraChange(CameraPosition cameraPosition) {
mMap.clear();
mMap.addMarker(new MarkerOptions().position(cameraPosition.target));
}
});
Another way of doing this is by overlaying a marker icon (Not a real marker; defined in your XML layout) and get the location when the user sets that as a location.
You would want to set draggable to false
public void setDraggable (boolean draggable)
The sets the draggability of the marker.

How to remove map markers

I want to implement a map system where the user can select a point of origin and destination as two different markers.
When the user long clicks on the map they could select a button to use this location as a start or end point.
case (R.id.setStart):
markerStart = mMap.addMarker(new MarkerOptions()
.position(this.startPoint)
.title("Start")
.draggable(true)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
outputName = rGC.getStringFromCoordinates(this.startPoint.latitude, this.startPoint.longitude);
autocompleterStart.setText(outputName);
break;
case (R.id.setEnd):
markerEnd = mMap.addMarker(new MarkerOptions()
.position(this.endPoint)
.title("End")
.draggable(true)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
However, when the user repeats the same process (select a new location as start/end), I want to be able to remove the previous marker. So if they select the same START button, the previous marker for start is removed and a new one is created.
Is there a way to check if a particular marker exists on the map. I tried adding marker.remove(); but it gives me a nullpointerexception.
case (R.id.setEnd):
markerEnd.remove();
markerEnd = mMap.addMarker(new MarkerOptions()
.position(this.endPoint)
.title("End")
.draggable(true)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
Any ideas? Thanks in advanced!
you can use method which i have implemented in my app and working fine...for me
LatLng latpoint;
MAP.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
#Override
public void onMapLongClick(LatLng point) {
// TODO Auto-generated method stub
// MAP.clear();
if (marker != null) {
marker.remove();
}
latpoint = point;
marker = MAP.addMarker(new MarkerOptions()
.position(
new LatLng(latpoint.latitude,
latpoint.longitude))
.title(point.toString()).draggable(true)
.visible(true));
}
});

Categories

Resources