How to add marker to google map in flutter - android

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!
}
)

Related

How to get marker position in google map

I have seen solutions including using onDragEnd or this method
void _updatePosition(CameraPosition _position) {
Position newMarkerPosition = Position(
latitude: _position.target.latitude,
longitude: _position.target.longitude);
Marker marker = markers["1"];
setState(() {
markers["1"] = marker.copyWith(
positionParam: LatLng(newMarkerPosition.latitude, newMarkerPosition.longitude));
});
}
it shows on class Position : Undefined class 'Position',
and onDragEnd not available
What is the reason and how can I get position from marker ?
If the IDE/Flutter says "Position" is "Undefined", Please check you have imported the "Geolocator" package. It's implemented in this package.
Add this to pubspec.yaml dependecies:
geolocator: ^6.1.7
and make sure it is imported in the .dart file you're working on. :)

showInfoWindow() function doesn't work in Google Maps

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.

Show marker labels along with default pin icon all the time on google maps for android platform

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

Mark in google maps a list of addresses - Android or PHP

I have a list of addresses and I want to mark this places all in google maps(nearly 1k), getting maybe its cordinates to save them.
Actually I am not programming an app, but I know both lenguages (Android and PHP) and I think it is possible to do this with both. Any idea ? Thank in advance!
Have you tried the google maps api?
https://developers.google.com/maps/documentation/geocoding/intro
https://developers.google.com/maps/documentation/geocoding/intro#ReverseGeocoding
This is what you are looking for I think.
EDIT: to mark stars on map
// In the following example, markers appear when the user clicks on the map.
// Each marker is labeled with a single alphabetical character.
var labels = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
var labelIndex = 0;
function initialize() {
var bangalore = { lat: 12.97, lng: 77.59 };
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 12,
center: bangalore
});
// This event listener calls addMarker() when the map is clicked.
google.maps.event.addListener(map, 'click', function(event) {
addMarker(event.latLng, map);
});
// Add a marker at the center of the map.
addMarker(bangalore, map);
}
// Adds a marker to the map.
function addMarker(location, map) {
// Add the marker at the clicked location, and add the next-available label
// from the array of alphabetical characters.
var marker = new google.maps.Marker({
position: location,
label: labels[labelIndex++ % labels.length],
map: map
});
}

Android Google Maps get Boundary Co-ordinates

I am using Google Maps v2 in my application. When the user pans or zooms on the screen I would like to get the area of the map based on which I want to fetch the POI only in the screen view part.
I went through the documentation but could not find any help.
You need to use Projection and VisibleRegion classes in order to get visible LatLng region.
So your code would look something like:
LatLngBounds curScreen = googleMap.getProjection()
.getVisibleRegion().latLngBounds;
In Android(Kotlin) you can find LatLng this way, in every time zoom or. gesture detect
private var mMap: GoogleMap? = null
..........
mMap?.setOnCameraMoveStartedListener { reasonCode ->
if (reasonCode == GoogleMap.OnCameraMoveStartedListener.REASON_GESTURE) {
val curScreen: LatLngBounds = mMap!!.getProjection().getVisibleRegion().latLngBounds
var northeast=curScreen.northeast
var southwest=curScreen.southwest
var center=curScreen.center
Log.v("northeast LatLng","-:"+northeast)
Log.v("southwest LatLng","-:"+southwest)
Log.v("center LatLng","-:"+center)
}
}

Categories

Resources