In Flutter Google Maps, how can I check if location is within bounds of the current maps view? Other libs provide such functions, like leaflet Checking if marker coordinates are in bounds
You can use getVisibleRegion() of the GoogleMapController to get the LatLngBounds and then use the method contains of that LatLngBounds.
Future<bool> _checkIfWithinBounds() async {
GoogleMapController mapController;
var mapBounds = await mapController.getVisibleRegion();
return mapBounds.contains(LatLng(yourLocation.lat, yourLocation.lng ),);
}
Related
Objective:
To be able to show custom markers from dev only and disable google maps' default markers.
Description:
I am trying to put markers in GoogleMap from google_maps_flutter plugin but Google already has its own markers so it is getting in the way of the markers that I am trying to add. Is there any way to just show the map and add user-defined markers only? If not is it possible to minimize the number of markers shown by default map?
Just looked around and found some possible fix.
Seems like we can generate map style from this website:
Styling Wizard.
From there I toned down landmarks and then I was able to remove markers using this:
final String mapStyle =
await rootBundle.loadString('assets/map/map_style.txt');
//Set it on mapcontroller after map is created.
onMapCreated: (GoogleMapController controller) {
if (_controller.isCompleted) {
return;
}
controller.setMapStyle(mapStyle);
_controller.complete(controller);
},
// create a function to create custom marker
Future<BitmapDescriptor> createCustomMarkerBitmap() async {
Uint8List? data = await getBytesFromAsset("assets/icons/map_marker.png", 100);
return BitmapDescriptor.fromBytes(data!);
}
// then call the function to create a custom marker.
BitmapDescriptor? _marker = await createCustomMarkerBitmap();
final Marker marker = Marker(
markerId: _markerId,
position: _position,
icon: _marker, // use the marker
infoWindow: _infoWindow,
onTap: () {},
);
I am developing a flutter app, trying to add Google Maps using the official plugin, however, there are tons of system markers that I don't know how to remove.
The markers are from Google's own database, but we really don't need that extra information, they are causing issues with our users because users thought the markers are from our own app! Big user experience issue.
I couldn't find a switch or something like that from the class GoogleMap.
Any ideas? thanks!
This can be achieved by applying custom google map styles to your google map.
To create custom google map styling. Use this tool to generate a map_style.json and save it in your assets folder. (Make sure it is referenced in pubspec.yaml aswell).
//this is the function to load custom map style json
void changeMapMode(GoogleMapController mapController) {
getJsonFile("assets/map_style.json")
.then((value) => setMapStyle(value, mapController));
}
//helper function
void setMapStyle(String mapStyle, GoogleMapController mapController) {
mapController.setMapStyle(mapStyle);
}
//helper function
Future<String> getJsonFile(String path) async {
ByteData byte = await rootBundle.load(path);
var list = byte.buffer.asUint8List(byte.offsetInBytes,byte.lengthInBytes);
return utf8.decode(list);
}
Implement it like this in your google map widget:
GoogleMap(
...
onMapCreated: (GoogleMapController c) {
yourMapController = c;
changeMapMode(yourMapController);
},
),
How to add a marker in flutter (as far as I understand, this is placemark, but after studying the documentation. I haven't found how to add it. marker on the map) on the map? I use Yandex Mapkit
Try like this
final List<MapObject> mapObjects = [];
final placemarks = [
Placemark(
mapId: MapObjectId('placemark_3'),
point const Point(latitude: 55.69494398296744, longitude: 37.653375915527334),
....
),
...
];
setState(() {
mapObjects.addAll(placemarks);
});
I am using google_maps_flutter to show the maps of user location, I come up with an idea where user can click anything in maps and show the the coordinates they clicked. What I mean with the coordinates here is Lat and Lng position that the click from the maps. Is that possible to do that ? and are there some articles as guide for me to do that ?
use onTap callback of GoogleMap widget like below to get coordinates from map where use clicks.
GoogleMap(
onTap: (LatLng latLng) {
final lat = latLng.latitude;
final long = latLng.longitude;
},
);
Why the build method is showing 'NULL' at Restart, but update the Latitude value on hot-reload?
Is it possible to load the Text('Lat: $latitude') as initState() itself?
class _LoadingScreenState extends State<LoadingScreen> {
double latitude;
double longitude;
#override
void initState() {
super.initState();
getLocation();
}
void getLocation() async {
Location location = Location();
await location.getCurrentLocation();
latitude = location.latitude;
longitude = location.longitude;
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(child: Text('Lat: $latitude')),
);
}
}
That's because you haven't called the setState method when you changed your data, so the widget did not rebuild itself.
It should be like this:
void getLocation() async {
Location location = Location();
await location.getCurrentLocation();
setState(() {
latitude = location.latitude;
longitude = location.longitude;
});
}
latitude havn't had time to be assigned with a value when the build method is constructing the widget.
Wrap the assignment of latitude and longitude with the setState method to notify the framework that a new build should take place. That way the latitude value will be updated as soon as it is available.
setState(() {
latitude = location.latitude;
longitude = location.longitude;
});
A tip is to display something else instead of the latitude value while waiting for it to be assigned. This could e.g. be a CircularProgressIndicator.
as with the answer above, the getLocation is async meaning it will finish in future, on restart you get the current latitude which is null then as it didn't get the value yet, when hot reloading you get to show the value then which is already finished,
you can use setState in stateful class as above,
or you can use futurebuilder to show the value when available.