I have a google map with some markers and all of them have title and snippet. I store markers in a HashMap. I want to display title and snippet with showInfoWindow() function when a button clicked.
this is my code :
Marker m = getMarker(location); //this function return marker from HashMap with its location
if(m!=null)
m.showInfoWindow();
m is not null, but showInfoWindow() doesn't work
debug output:
m.getTitle() log :
Change your code to this
Marker m = getMarker(location);
if(m!= null){
m.showInfoWindow();
}
The Marker object will be null if the marker has not been rendered on the map yet so be sure to check that the marker object is not null before using it.
For showing infoWindow your marker must be have a title.
MarkerOptions marker = new MarkerOptions().position(location)
.icon(BitmapDescriptorFactory.fromBitmap(aBitmapFile))
.title("The Title"); //here is title
mapPoint p = new mapPoint(lat, lng);
googleMap.addMarker(marker);
then you can call: .showInfoWindow(); on your marker.
Related
I want to add just one marker when tap on map and return the value of position it stands on
how can I do this ?
NOTE : GoogleMap doesn't have onTap
You can try this library for google map https://pub.dev/packages/google_maps_flutter and also it has on tap on the marker onTap.
Marker(
markerId: MarkerId("id"), // a string for marker unique id
position: LatLng(lat, long), // lat and long doubles
onTap: () {
//this is what you're looking for!
}
)
I have multiple locations that I need to display on my map.
I want the info/label for each marker to display on the maps along with the pin icon whether the marker/pin is clicked or not by the user.
I have the following code to display the markers.
for (DepartmentLocation loc : departmentLocations) {
if (loc != null && loc.getLatitude() != null && loc.getLongitude() != null) {
LatLng latLng = new LatLng(loc.getLatitude(), loc.getLongitude());
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
MarkerOptions markerOptions = new MarkerOptions().position(latLng).title(loc.getName());
markerOptions.visible(true);
Marker marker = googleMap.addMarker(markerOptions);
marker.showInfoWindow();
marker.setVisible(true);
markers.add(marker);
}
}
But still, I see only the red pins and no labels beside the pin.
Here is how my maps look like. I can see only one info label, not the others.
I have tried using the IconGenerator from the map utils.
implementation 'com.google.maps.android:android-maps-utils:0.5'
for (DepartmentLocation loc : departmentLocations) {
if (loc != null && loc.getLatitude() != null && loc.getLongitude() != null) {
LatLng latLng = new LatLng(loc.getLatitude(), loc.getLongitude());
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
MarkerOptions markerOptions = new MarkerOptions()
.icon(BitmapDescriptorFactory.fromBitmap(iconFactory.makeIcon(loc.getName())))
.position(latLng)
.title(loc.getName());
markerOptions.visible(true);
Marker marker = googleMap.addMarker(markerOptions);
marker.showInfoWindow();
marker.setVisible(true);
markers.add(marker);
}
}
When I use iconfactory, I am unable to show both the pin and the info label.
Only one InfoWindow is displayed at a time on GoogleMap. You can't display multiple InfoWindow at a time.
You have to use IconGenerator for that. You can set a view to IconGenerator and make a Bitmap form that view. You can make a view that contains both Marker and your InfoWindow View. Use that view to crate your Desired Marker.
You can check this answer
From the documentation, there is a text saying that the info window is shown one at a time, so there is no way to do that:
"An info window allows you to display information to the user when they
tap on a marker. Only one info window is displayed at a time. If a
user clicks on a marker, the current info window will be closed and
the new info window will be displayed. Note that if the user clicks on
a marker that is currently showing an info window, that info window
closes and re-opens."
You can read more here: https://developers.google.com/maps/documentation/android-sdk/infowindows
I am getting the place name of google maps using
Place place = PlaceAutocomplete.getPlace(MainActivity.this, data);
Logger.e("The ", "Place: " + place.getName());
now i want to set the marker at this place name. How can I acheive this?
First try to get the latitude and longitude from place name using Geocoder after that try following code for showing the marker on the map.
GoogleMap map;
map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
map.addMarker(new MarkerOptions()
.position(new LatLng(placelatitude, placeLongitude))
.title("Place Name"));
Im getting list of planes from web api with their locations and infos. Im calling this web api every few seconds and drawing the map markers (planes) to map.
When I call my method addPlanesToMap(), to add/update markers on map, map freezes for short period of time. If im movin the map at the same time the markers get added/updated there is some lag. This happens in devices like Nexus 5. There is around 200 - 300 markers in map.
Is there something how I can improve my code, do something completely differently?
HashMap<String, MarkerContainer> mMarkerHashMap = new HashMap <String, MarkerContainer>();
public void addPlanesToMap(List <? extends RealtimePlane > planes) {
View planeMarkerView = getActivity().getLayoutInflater().inflate(R.layout.map_marker_plane, null);
TextView markerShortCodeText = (TextView) planeMarkerView.findViewById(R.id.MapMarkerText);
ImageView markerArrow = (ImageView) planeMarkerView.findViewById(R.id.MapMarkerArrow);
for (RealtimePlane plane: planes) {
MarkerContainer markerContainer = mMarkerHashMap.get(plane.getPlaneId());
// Do not process, if not updated
if (markerContainer != null && markerContainer.getPlane().getRecordedAtTime().equals(plane.getRecordedAtTime())) {
continue;
}
markerShortCodeText.setText(plane.getPlaneCode());
markerShortCodeText.setBackgroundResource(getMapMarkerDrawableBg(plane.getType()));
markerArrow.setRotation((float) plane.getBearing());
markerArrow.setVisibility(View.VISIBLE);
// Add new plane marker to map
if (markerContainer == null) {
Marker m = mMap.addMarker(new MarkerOptions()
.position(plane.getLocation())
.flat(true)
.anchor(0.5f, 0.5f)
.title(plane.getLineCode())
.icon(BitmapDescriptorFactory.fromBitmap(loadBitmapFromView(getActivity(), planeMarkerView))));
mMarkerHashMap.put(plane.getPlaneId(), new MarkerContainer(m, plane));
}
// Marker already set, update
else {
markerContainer.getMarker().setIcon(BitmapDescriptorFactory.fromBitmap(loadBitmapFromView(getActivity(), planeMarkerView)));
markerContainer.getMarker().setPosition(plane.getLocation());
markerContainer.setPlane(plane);
}
}
}
Is there something how I can improve my code, do something completely differently
yes, do not plot every single marker and only plot what is in the visible area. you can also do marker clustering.
Plotting 2-300 markers every few seconds is not really a good idea to begin with
I'm currently having a map, and each 10 meters I use LocationListener to refresh my location and get the new Latitude and Longitude. Now I wish that the route the user is taking will be displayed with a red line. So everytime the OnLocationChange() from LocationListener class is called, I want to update the map with a line between the last location and the new location.
So far I've added the following:
private void initializeDraw() {
lineOptions = new PolylineOptions().width(5).color(Color.RED);
lineRoute = workoutMap.addPolyline(lineOptions);
}
during the OnLocationChanged I call this:
drawTrail();
now what should I insert into this function so that each time it adds the newly attained location as a point and draws a line from the last to the new point.
Thanks
First translate Location into LatLng:
LatLng newPoint = new LatLng(location.getLatitude(), location.getLongitude());
Then add a point to existing list of points:
List<LatLng> points = lineRoute.getPoints();
points.add(newPoint);
lineRoute.setPoints(points);