Draw different polylines in Gmaps for Flutter - google-maps

since there is no plugins for open kml files in flutter, i have extracted all the coords and tried to plot them inside a google map using polylines.
They are 360 pairs of coordinates
i am using this code
List<LatLng> tempList = new List<LatLng>();
for (int i = 0; i <11; i+=2) {
tempList.clear();
tempList.add(listaCoords[i]);
tempList.add(listaCoords[i+1]);
_circuito1.add(Polyline(
polylineId: PolylineId(i.toString()),
points: tempList,
color: Colors.redAccent.shade700,
width: 10,
visible: true,
geodesic: true,
));
}
setState(() {
lineas.clear();
lineas.addAll(_circuito1);
});
//Más código....
return GoogleMap(
mapType: MapType.hybrid,
initialCameraPosition: _kGooglePlex,
onMapCreated: _onMapCreated,
markers: _markers,
polylines: lineas,
);
They don't show up inside the map, but if a decrease the number of points in the Set of polylines, they begin to show up
But if a increase these iterations like around 50 different polylines the plot dissapears
I hope you can help me :)

i have found the solution! i called native functions of android and imported the KML import utility natively from android to flutter!

Related

Show multiple markers in google maps

I'm trying to create multiple Markers for google maps. I started out with a Set<Marker> markers = Set();, then made a loop over a List and added Markers to markers
Marker resultMarker = Marker(
markerId: MarkerId(currentLocation.id),
infoWindow: InfoWindow(title: "${currentLocation.title}", snippet: "$snippet"),
position: LatLng(currentLocation.coordinates[1], currentLocation.coordinates[0]),
);
markers.add(resultMarker);
I then tried returning a widget of:
GoogleMap(
initialCameraPosition: CameraPosition(target: _cameraPosition, zoom: 11.0),
onMapCreated: (controller) => mapController = controller,
myLocationButtonEnabled: false,
markers: markers,
);
But I only get the first marker and not all of them. How can I get all markers?
This one worked for me:
List<Marker> markers = <Marker>[];
and inside your loop:
markers.add(
Marker(
markerId: MarkerId(currentLocation.id),
infoWindow: InfoWindow(title: "${currentLocation.title}", snippet: "$snippet"),
position: LatLng(currentLocation.coordinates[1], currentLocation.coordinates[0]),
)
);
make sure that markers data are correct since google map does not show markers with invalid LatLng ( try to test with dummy data)
and then
GoogleMap(
initialCameraPosition: CameraPosition(
target: LatLng(38.9647,35.2233),
zoom: 9.0,
),
mapType: MapType.normal,
markers: Set<Marker>.of(_markers),
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
},
)

Adding markers dynamically to flutter google map

I'm using Flutter to make a mobile application that uses a map. We decided to use Google map and the plugin for flutter we use is:
google_maps_flutter: ^0.5.7
I understand that adding markers to the map works like this:
Map<MarkerId, Marker> markers = <MarkerId, Marker>{};
Marker _marker = new Marker(
icon: BitmapDescriptor.defaultMarker,
markerId: _markerId,
position: LatLng(34.024441,-5.0310968),
infoWindow: InfoWindow(title: "userMarker", snippet: '*'),
);
GoogleMap _map;
#override
void initState(
markers[_markerId] = _marker;
_map = new GoogleMap(
/* other properties irrelevant to this prob */
markers: Set<Marker>.of(_markers.values),
);
);
The above snippet does work, I get to see the marker on the map. But modifying the marker or trying to add another marker like in the snippet below does not work.
FloatingActionButton(
onPressed: () {
setState(() {
_marker = new Marker(
icon: BitmapDescriptor.defaultMarker,
markerId: _markerId,
position: LatLng(currentLocation.latitude, currentLocation.longitude),
infoWindow: InfoWindow(title: "userMarker", snippet: '*'),
onTap: () {
debugPrint("Marker Tapped");
},
);
markers[_markerId] = _marker; // What I do here is modify the only marker in the Map.
});
markers.forEach((id,marker) { // This is used to see if the marker properties did change, and they did.
debugPrint("MarkerId: $id");
debugPrint("Marker: [${marker.position.latitude},${marker.position.longitude}]");
});
});
)
My intention here is using another plugin (geoLocator) to get location data of the user and change the only marker I have so it can track his movements. The debugPrint shows that the data is indeed changing, but I see no change in the map (the initial marker that I change uses a different location than my own when I test).
If there's no specific reason for you to use Map data structure, here's what I've done in the past.
I have a Set of Marker in my State
Set<Marker> markers = Set();
Give it to the map widget. markers: markers
GoogleMap(
onMapCreated: _onMapCreated,
myLocationEnabled: true,
initialCameraPosition:
CameraPosition(target: LatLng(0.0, 0.0)),
markers: markers,
)
And then adding the Marker, which I'm building with search result and which you'll be building with your user's location, to Set of Marker in setState method.
// Create a new marker
Marker resultMarker = Marker(
markerId: MarkerId(responseResult.name),
infoWindow: InfoWindow(
title: "${responseResult.name}",
snippet: "${responseResult.types?.first}"),
position: LatLng(responseResult.geometry.location.lat,
responseResult.geometry.location.lng),
);
// Add it to Set
markers.add(resultMarker);
Edit: I've just noticed you're using GoogleMap widget in your initState, you need to move it to the build method if you want to rebuild it everytime with the new state values.

How to Show 2 LatLng as Initial Position in Flutter Google Map

i'm doing (OLA, UBER) like app in flutter.i wanna show route from source and destination and to set initial camera position by covering two lat-long.
GoogleMap(
onMapCreated: _onMapCreated,
mapType: MapType.normal,
compassEnabled: true,
myLocationEnabled: true,
initialCameraPosition: CameraPosition(target:
LatLng(0.0, 0.0)), // <---- instead of single
// latlong, i wanna show both source and
// destination
markers: _createMarker(),
polylines: Set<Polyline>.of(polyLines.values),
),
await mapController
.animateCamera(CameraUpdate.newLatLngBounds(
LatLngBounds(southwest: LatLongONE, northeast: LatLongTWO), 35))
those two southwest, northeast will be given by this
enter link description here
google Api

Flutter: How to add marker to Google Maps with new Marker API?

mapController.addMarker(
MarkerOptions(
position: LatLng(37.4219999, -122.0862462),
),
);
I've seen this code snippet in a blog post, and I'm trying to add markers to Google Maps.
The method 'addMarker' isn't defined for the class 'GoogleMapController'.
I think the library has changed and I want to know what's the new way doing this, I've looked up in the controller.dart and api reference but couldn't figure it out.
I would be happy to see some tutorials and blog posts about it, don't hesitate to share.
Yes, The google maps API has changed and the Marker API is widget based and not based in controller anymore.
By CHANGELOG.md
"Breaking change. Changed the Marker API to be widget based, it was controller based. Also changed the example app to account for the same."
I copy some pieces of code from github app example that I think is important to you
Map<MarkerId, Marker> markers = <MarkerId, Marker>{}; // CLASS MEMBER, MAP OF MARKS
void _add() {
var markerIdVal = MyWayToGenerateId();
final MarkerId markerId = MarkerId(markerIdVal);
// creating a new MARKER
final Marker marker = Marker(
markerId: markerId,
position: LatLng(
center.latitude + sin(_markerIdCounter * pi / 6.0) / 20.0,
center.longitude + cos(_markerIdCounter * pi / 6.0) / 20.0,
),
infoWindow: InfoWindow(title: markerIdVal, snippet: '*'),
onTap: () {
_onMarkerTapped(markerId);
},
);
setState(() {
// adding a new marker to map
markers[markerId] = marker;
});
}
GoogleMap(
onMapCreated: _onMapCreated,
initialCameraPosition: const CameraPosition(
target: LatLng(-33.852, 151.211),
zoom: 11.0,
),
// TODO(iskakaushik): Remove this when collection literals makes it to stable.
// https://github.com/flutter/flutter/issues/28312
// ignore: prefer_collection_literals
markers: Set<Marker>.of(markers.values), // YOUR MARKS IN MAP
)
I advise you take a look in example app here. There is updated to new API.
I did below example from google codelabs.
Map<MarkerId, Marker> markers = <MarkerId, Marker>{};
LatLng _center = LatLng(9.669111, 80.014007);
onMap created add single static marker
void _onMapCreated(GoogleMapController controller) {
mapController = controller;
final marker = Marker(
markerId: MarkerId('place_name'),
position: LatLng(9.669111, 80.014007),
// icon: BitmapDescriptor.,
infoWindow: InfoWindow(
title: 'title',
snippet: 'address',
),
);
setState(() {
markers[MarkerId('place_name')] = marker;
});
}
google map widget:
GoogleMap(
onMapCreated: _onMapCreated,
initialCameraPosition: CameraPosition(
target: _center,
zoom: 14.0,
),
markers: markers.values.toSet(),
),

How to create circle on current location in flutter

I am working on flutter project.On which I have to draw one circle on my current location on google map.Does any one have idea.
I want like this in flutter
Thanks in advance.
This functionality is now available in the google_maps_flutter package:
https://pub.dev/documentation/google_maps_flutter_platform_interface/latest/google_maps_flutter_platform_interface/Circle-class.html
Partial example:
Set<Circle> circles = Set.from([Circle(
circleId: CircleId(id),
center: LatLng(latitude, longitude),
radius: 4000,
)]);
GoogleMap(
mapType: MapType.normal,
myLocationEnabled: true,
myLocationButtonEnabled: true,
initialCameraPosition: initialMapLocation,
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
},
onCameraMove: null,
circles: circles,
);
If you use flutter_map plugin, then you can draw radius circle easily. Here is the code.
FlutterMap(
options: MapOptions(
center: LatLng(latitude, longitude),
zoom: 16.0
),
layers: [
TileLayerOptions(
urlTemplate: "map url",
additionalOptions: {
"accessToken" : "xxx",
"id" : "map id"
}
),
MarkerLayerOptions(
markers: Marker( //marker
width: 25.0,
height: 25.0,
point: LatLng(latitude, longitude),
builder: (context) {
return Container(
child: IconButton(
icon: Icon(Icons.my_location),
iconSize: 25.0,
color: Color(0xff9045f7),
onPressed: () {
print('marker tapped');
},
),
);
}
)
),
CircleLayerOptions(
circles: CircleMarker( //radius marker
point: LatLng(latitude, longitude),
color: Colors.blue.withOpacity(0.3),
borderStrokeWidth: 3.0,
borderColor: Colors.blue,
radius: 100 //radius
)
)
],
),)
You could use google_maps_flutter plugin, here is the code:
circles: Set.from([Circle( circleId: CircleId('currentCircle'),
center: LatLng(position.latitude,position.longitude),
radius: 4000,
fillColor: Colors.blue.shade100.withOpacity(0.5),
strokeColor: Colors.blue.shade100.withOpacity(0.1),
),],),
One way to do this is by changing the icon of the Marker
mapController.addMarker(MarkerOptions(
position: LatLng(37.4219999, -122.0862462),
icon: BitmapDescriptor.fromAsset('images/circle.png',),
),
);
After setting appropriate permission on android/ios, set trackCameraPosition: true and myLocationEnabled: true on google maps options, after that you can add your image in current location by following code
mapController.addMarker(MarkerOptions(
position: mapController.cameraPosition.target,
icon: BitmapDescriptor.fromAsset('images/circle.png',),
),
);
Here is an excellent article which explains all possible things you can do with Google Maps for Flutter (Developers Preview) plugin article
There is an opended pull request with this feature at https://github.com/flutter/plugins/pull/1136
But Google team has not accepted it because there is a issue with license with whom made the commits. I don't know if this will be merged. There are a lot of pull request accepted, but not integrated.
Please keep in mind the google maps widget is a developer preview, at version 0.2. Many things and abilities will likely change over the coming months.