Skip to content

Added Id Parameter on the addMarkerRaw Method so a custom id can be u… #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class _MyHomePageState extends State<MyHomePage> {
const SizedBox(width: 16),
FloatingActionButton(
child: Icon(Icons.pin_drop),
onPressed: () {
onPressed: () async{
GoogleMap.of(_key).addMarkerRaw(
GeoCoord(33.875513, -117.550257),
info: 'test info',
Expand All @@ -132,8 +132,9 @@ class _MyHomePageState extends State<MyHomePage> {
);
},
);

GoogleMap.of(_key).addMarkerRaw(
GeoCoord(33.775513, -117.450257),
await GoogleMap.of(_key).center,
icon: 'assets/images/map-marker-warehouse.png',
info: contentString,
);
Expand Down
3 changes: 2 additions & 1 deletion example/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
<title>flutter_google_maps_example</title>
</head>
<body>
<script src="https://maps.googleapis.com/maps/api/js?key=API_KEY"></script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyA0YuTQJqKDW-rX6xuwp87Hvbj51QEhQwk"></script>

<script src="main.dart.js" type="application/javascript"></script>
</body>
</html>
6 changes: 6 additions & 0 deletions lib/src/core/google_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:flutter/foundation.dart';

import 'package:google_directions_api/google_directions_api.dart'
show GeoCoord, DirectionsService;
import 'package:google_maps_flutter/google_maps_flutter.dart' as gmap;

import 'map_items.dart';
import 'map_operations.dart';
Expand All @@ -27,6 +28,8 @@ class GoogleMap extends StatefulWidget {
this.markers,
this.onTap,
this.onLongPress,
this.onMapIdle,
this.onMapMove,
this.interactive = true,
this.initialZoom = _zoom,
this.mapType = MapType.roadmap,
Expand Down Expand Up @@ -85,6 +88,9 @@ class GoogleMap extends StatefulWidget {
/// For `web` this will be called when `right mouse clicked`.
final ValueChanged<GeoCoord> onLongPress;

final void Function() onMapIdle;
final ValueChanged<gmap.CameraPosition> onMapMove;

/// Set of mobile map preferences.
final MobileMapPreferences mobilePreferences;

Expand Down
10 changes: 10 additions & 0 deletions lib/src/core/google_map.state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import 'package:google_directions_api/google_directions_api.dart'

import 'map_items.dart';
import 'google_map.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart' show PatternItem;

class GoogleMapState extends GoogleMapStateBase {

@override
void moveCameraBounds(
GeoCoordBounds newBounds, {
Expand Down Expand Up @@ -42,6 +44,10 @@ class GoogleMapState extends GoogleMapStateBase {
@override
FutureOr<GeoCoord> get center => throw UnimplementedError();

FutureOr<double> get zoom => throw UnimplementedError();

FutureOr<GeoCoordBounds> get bounds => throw UnimplementedError();

@override
void changeMapStyle(
String mapStyle, {
Expand All @@ -59,13 +65,17 @@ class GoogleMapState extends GoogleMapStateBase {
String endLabel,
String endIcon,
String endInfo,
List<PatternItem> patterns,
int width,
Color color,
}) =>
throw UnimplementedError();

@override
void addMarkerRaw(
GeoCoord position, {
String label,
String id,
String icon,
String info,
String infoSnippet,
Expand Down
11 changes: 11 additions & 0 deletions lib/src/core/map_operations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:flutter/foundation.dart' show ValueChanged;
import 'package:google_directions_api/google_directions_api.dart'
show GeoCoord, GeoCoordBounds;

import 'package:google_maps_flutter/google_maps_flutter.dart' show PatternItem;
import 'map_items.dart';

/// Interface of setting up map operations including:
Expand Down Expand Up @@ -69,6 +70,12 @@ abstract class MapOperations

/// Gets center coordinates of the map.
FutureOr<GeoCoord> get center;

/// Gets zoom coordinates of the map.
FutureOr<double> get zoom;

/// Gets zoom coordinates of the map.
FutureOr<GeoCoordBounds> get bounds;

/// Sets the styling of the base map.
///
Expand Down Expand Up @@ -121,6 +128,7 @@ abstract class MapMarkers {
/// If marker with same [position] have been already added, addition of a new marker will be ignored.
void addMarkerRaw(
GeoCoord position, {
String id,
String label,
String icon,
String info,
Expand Down Expand Up @@ -161,6 +169,9 @@ abstract class MapDirections {
String endLabel,
String endIcon,
String endInfo,
List<PatternItem> patterns,
int width,
Color color,
});

/// Removes a direction from the map by given [origin] and [destination] coordinates.
Expand Down
22 changes: 17 additions & 5 deletions lib/src/mobile/google_map.state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import 'utils.dart';
import '../core/utils.dart' as utils;
import '../core/google_map.dart' as gmap;
import '../core/map_items.dart' as items;
import 'package:google_maps_flutter/google_maps_flutter.dart' show PatternItem;

class GoogleMapState extends gmap.GoogleMapStateBase {
final directionsService = DirectionsService();
Expand Down Expand Up @@ -141,6 +142,11 @@ class GoogleMapState extends gmap.GoogleMapStateBase {
FutureOr<GeoCoord> get center async =>
(await _controller?.getVisibleRegion())?.toGeoCoordBounds()?.center;

FutureOr<double> get zoom async => (await _controller?.getZoomLevel());

FutureOr<GeoCoordBounds> get bounds async =>
(await _controller?.getVisibleRegion()).toGeoCoordBounds();

@override
void changeMapStyle(
String mapStyle, {
Expand All @@ -160,6 +166,7 @@ class GoogleMapState extends gmap.GoogleMapStateBase {
void addMarkerRaw(
GeoCoord position, {
String label,
String id,
String icon,
String info,
String infoSnippet,
Expand All @@ -178,9 +185,8 @@ class GoogleMapState extends gmap.GoogleMapStateBase {
return true;
}());

final key = position.toString();

if (_markers.containsKey(key)) return;
final key = id ?? position.toString();
if (id == null && _markers.containsKey(key)) return;

final markerId = MarkerId(key);
final marker = Marker(
Expand Down Expand Up @@ -248,6 +254,9 @@ class GoogleMapState extends gmap.GoogleMapStateBase {
String endLabel,
String endIcon,
String endInfo,
List<PatternItem> patterns,
int width,
Color color = const Color(0xcc2196F3),
}) {
assert(() {
if (origin == null) {
Expand Down Expand Up @@ -328,10 +337,11 @@ class GoogleMapState extends gmap.GoogleMapStateBase {
points: response?.routes?.firstOrNull?.overviewPath
?.mapList((_) => _.toLatLng()) ??
[startLatLng?.toLatLng(), endLatLng?.toLatLng()],
color: const Color(0xcc2196F3),
color: color,
startCap: Cap.roundCap,
endCap: Cap.roundCap,
width: 8,
width: width,
patterns: patterns ?? [],
);

_setState(() => _polylines[key] = polyline);
Expand Down Expand Up @@ -594,6 +604,8 @@ class GoogleMapState extends gmap.GoogleMapStateBase {
onTap: (coords) => widget.onTap?.call(coords?.toGeoCoord()),
onLongPress: (coords) =>
widget.onLongPress?.call(coords?.toGeoCoord()),
onCameraMove: (position) => widget.onMapMove?.call(position),
onCameraIdle: () => widget.onMapIdle?.call(),
onMapCreated: (GoogleMapController controller) {
_controller = controller;
_controller.setMapStyle(widget.mapStyle);
Expand Down
34 changes: 27 additions & 7 deletions lib/src/web/google_map.state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'dart:ui' as ui;

import 'package:flutter/widgets.dart';
import 'package:flutter/scheduler.dart' show SchedulerBinding;
import 'package:google_maps_flutter/google_maps_flutter.dart' as gmaps;

import 'package:uuid/uuid.dart';
import 'package:flinq/flinq.dart';
Expand All @@ -19,6 +20,7 @@ import 'utils.dart';
import '../core/google_map.dart';
import '../core/utils.dart' as utils;
import '../core/map_items.dart' as items;
import 'package:google_maps_flutter/google_maps_flutter.dart' show PatternItem;

class GoogleMapState extends GoogleMapStateBase {
final htmlId = Uuid().v1();
Expand Down Expand Up @@ -63,13 +65,13 @@ class GoogleMapState extends GoogleMapStateBase {

_map.center = newBounds.center.toLatLng();

final zoom = _map.zoom;
// final zoom = _map.zoom;
if (animated == true) {
_map.panToBounds(newBounds.toLatLngBounds());
} else {
_map.fitBounds(newBounds.toLatLngBounds());
}
_map.zoom = zoom;
// _map.zoom = zoom;
}

@override
Expand Down Expand Up @@ -116,6 +118,10 @@ class GoogleMapState extends GoogleMapStateBase {
@override
FutureOr<GeoCoord> get center => _map.center?.toGeoCoord();

FutureOr<double> get zoom => _map.zoom.toDouble();

FutureOr<GeoCoordBounds> get bounds => _map.bounds.toGeoCoordBounds();

@override
void changeMapStyle(
String mapStyle, {
Expand All @@ -133,6 +139,7 @@ class GoogleMapState extends GoogleMapStateBase {
void addMarkerRaw(
GeoCoord position, {
String label,
String id,
String icon,
String info,
String infoSnippet,
Expand All @@ -151,9 +158,8 @@ class GoogleMapState extends GoogleMapStateBase {
return true;
}());

final key = position.toString();

if (_markers.containsKey(key)) return;
final key = id ?? position.toString();
if (id == null && _markers.containsKey(key)) return;

final marker = Marker()
..map = _map
Expand Down Expand Up @@ -274,6 +280,9 @@ class GoogleMapState extends GoogleMapStateBase {
String endLabel,
String endIcon,
String endInfo,
List<PatternItem> patterns,
int width,
Color color,
}) {
assert(() {
if (origin == null) {
Expand Down Expand Up @@ -646,6 +655,7 @@ class GoogleMapState extends GoogleMapStateBase {
_createMapOptions();

if (_map == null) {
// ignore: undefined_prefixed_name
ui.platformViewRegistry.registerViewFactory(htmlId, (int viewId) {
final elem = DivElement()
..id = htmlId
Expand All @@ -654,7 +664,18 @@ class GoogleMapState extends GoogleMapStateBase {
..style.border = 'none';

_map = GMap(elem, _mapOptions);

_subscriptions.add(_map.onIdle.listen((event) {
widget.onMapIdle?.call();
}));
_subscriptions.add(_map.onCenterChanged.listen(
(event) {
gmaps.CameraPosition pos = gmaps.CameraPosition(
target: gmaps.LatLng( _map.center.lat, _map.center.lng),
zoom: _map.zoom.toDouble(),
tilt: _map.tilt,
);
widget.onMapMove?.call(pos);
}));
_subscriptions.add(_map.onClick.listen(
(event) => widget.onTap?.call(event?.latLng?.toGeoCoord())));
_subscriptions.add(_map.onRightclick.listen(
Expand Down Expand Up @@ -686,7 +707,6 @@ class GoogleMapState extends GoogleMapStateBase {
_infos.clear();
_markers.clear();
_polygons.clear();
_circles.clear();
_infoState.clear();
_directions.clear();
_subscriptions.clear();
Expand Down
14 changes: 7 additions & 7 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: flutter_google_maps
version: 4.0.0
version: 4.1.0
homepage: https://github.com/marchdev-tk/flutter_google_maps
description: A Flutter plugin for integrating Google Maps in iOS, Android and Web applications. It is a wrapper of google_maps_flutter for Mobile and google_maps for Web.

Expand All @@ -11,18 +11,18 @@ dependencies:
sdk: flutter

google_polyline_algorithm: ^2.0.0
google_maps_flutter: ^0.5.28+1
google_maps_flutter: ^0.5.30
google_directions_api: ^0.5.0
google_maps: ^3.4.3
http: ^0.12.1
flinq: ^1.1.0
uuid: ^2.1.0
google_maps: ^3.4.4
http: ^0.12.2
flinq: ^1.1.1
uuid: ^2.2.0

dev_dependencies:
flutter_test:
sdk: flutter

pedantic: ^1.8.0
pedantic: ^1.9.2

flutter:
uses-material-design: true
Expand Down