Skip to content

Commit fb48460

Browse files
committed
Implementation of most functionality
1 parent 20bf0a2 commit fb48460

File tree

11 files changed

+309
-107
lines changed

11 files changed

+309
-107
lines changed

README.md

Lines changed: 83 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,87 @@
1-
# flutter_platform_maps
1+
# apple_maps_flutter
22

3-
A new Flutter package project.
3+
A Flutter package that provides a native map to both Android and iOS devices.
44

5-
## Getting Started
5+
The plugin relies on Flutter's mechanism for embedding Android and iOS views. As that mechanism is currently in a developers preview, this plugin should also be considered a developers preview.
6+
7+
This package combines the [google_maps_flutter]("https://pub.dev/packages/google_maps_flutter") plugin with [apple_maps_flutter]("https://pub.dev/packages/apple_maps_flutter") to create a cross platform implementation of native maps for Android/iOS.
8+
9+
# Screenshots
10+
11+
| Example 1 | Example 2 |
12+
| :---------------------------------------------------------------------------------------------------: | :---------------------------------------------------------------------------------------------------: |
13+
| ![Example 1](https://github.com/LuisThein/apple_maps_flutter/blob/master/resources/example_img01.png) | ![Example 2](https://github.com/LuisThein/apple_maps_flutter/blob/master/resources/example_img02.png) |
14+
15+
# iOS
16+
17+
To use this plugin on iOS you need to opt-in for the embedded views preview by adding a boolean property to the app's Info.plist file, with the key `io.flutter.embedded_views_preview` and the value `YES`. You will also have to add the key `Privacy - Location When In Use Usage Description` with the value of your usage description.
18+
19+
# Android
20+
21+
Specify your API key in the application manifest android/app/src/main/AndroidManifest.xml:
22+
23+
```xml
24+
<manifest ...
25+
<application ...
26+
<meta-data android:name="com.google.android.geo.API_KEY"
27+
android:value="YOUR KEY HERE"/>
28+
```
29+
30+
## Sample Usage
31+
32+
```dart
33+
class HomePage extends StatelessWidget {
34+
@override
35+
Widget build(BuildContext context) {
36+
return Scaffold(
37+
body: PlatformMap(
38+
initialCameraPosition: CameraPosition(
39+
target: const LatLng(47.6, 8.8796),
40+
zoom: 16.0,
41+
),
42+
markers: Set<Marker>.of(
43+
[
44+
Marker(
45+
markerId: MarkerId('hi'),
46+
position: LatLng(47.6, 8.8796),
47+
consumeTapEvents: true,
48+
infoWindow: InfoWindow(
49+
title: 'PlatformMarker',
50+
snippet: 'Hi im a Platform Marker',
51+
),
52+
onTap: () {
53+
print("Marker tapped");
54+
},
55+
),
56+
],
57+
),
58+
mapType: MapType.satallite,
59+
myLocationEnabled: true,
60+
myLocationButtonEnabled: true,
61+
onTap: (location) => print('onTap: $location'),
62+
onCameraMove: (location) => print('cameraMove: $location'),
63+
compassEnabled: true,
64+
onMapCreated: (controller) {
65+
Future.delayed(Duration(seconds: 2)).then((_) {
66+
controller.animateCamera(
67+
CameraUpdate.newCameraPosition(
68+
const CameraPosition(
69+
bearing: 270.0,
70+
target: LatLng(51.5160895, -0.1294527),
71+
tilt: 30.0,
72+
zoom: 18,
73+
),
74+
),
75+
);
76+
},
77+
);
78+
},
79+
),
80+
);
81+
}
82+
}
83+
```
84+
85+
Suggestions and PR's to make this plugin better are always welcome. Please notice that the features provided by this package depend on the apple_maps_flutter plugin, which will improve in the future.
686

7-
This project is a starting point for a Dart
8-
[package](https://flutter.dev/developing-packages/),
9-
a library module containing code that can be shared easily across
10-
multiple Flutter or Dart projects.
1187

12-
For help getting started with Flutter, view our
13-
[online documentation](https://flutter.dev/docs), which offers tutorials,
14-
samples, guidance on mobile development, and a full API reference.

lib/platform_maps_flutter.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ part 'src/camera.dart';
1212
part 'src/platform_maps.dart';
1313
part 'src/location.dart';
1414
part 'src/marker.dart';
15-
part 'src/map_type.dart';
15+
part 'src/controller.dart';
16+
part 'src/ui.dart';

lib/src/camera.dart

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class CameraPosition {
4444
/// will be silently clamped to the supported range.
4545
final double zoom;
4646

47-
appleMaps.CameraPosition appleMapsCameraPosition() {
47+
appleMaps.CameraPosition get appleMapsCameraPosition {
4848
return appleMaps.CameraPosition(
4949
target: this.target.appleLatLng,
5050
heading: this.bearing,
@@ -53,7 +53,7 @@ class CameraPosition {
5353
);
5454
}
5555

56-
googleMaps.CameraPosition googleMapsCameraPosition() {
56+
googleMaps.CameraPosition get googleMapsCameraPosition {
5757
return googleMaps.CameraPosition(
5858
target: this.target.googleLatLng,
5959
bearing: this.bearing,
@@ -62,3 +62,63 @@ class CameraPosition {
6262
);
6363
}
6464
}
65+
66+
class CameraUpdate implements googleMaps.CameraUpdate, appleMaps.CameraUpdate {
67+
static newCameraPosition(CameraPosition cameraPosition) {
68+
if (Platform.isIOS) {
69+
return appleMaps.CameraUpdate.newCameraPosition(
70+
cameraPosition.appleMapsCameraPosition);
71+
} else if (Platform.isAndroid) {
72+
return googleMaps.CameraUpdate.newCameraPosition(
73+
cameraPosition.googleMapsCameraPosition);
74+
}
75+
}
76+
77+
static newLatLng(LatLng latLng) {
78+
if (Platform.isIOS) {
79+
return appleMaps.CameraUpdate.newLatLng(latLng.appleLatLng);
80+
} else if (Platform.isAndroid) {
81+
return googleMaps.CameraUpdate.newLatLng(latLng.googleLatLng);
82+
}
83+
}
84+
85+
static newLatLngZoom(LatLng latLng, double zoom) {
86+
if (Platform.isIOS) {
87+
return appleMaps.CameraUpdate.newLatLngZoom(latLng.appleLatLng, zoom);
88+
} else if (Platform.isAndroid) {
89+
return googleMaps.CameraUpdate.newLatLngZoom(latLng.googleLatLng, zoom);
90+
}
91+
}
92+
93+
static zoomBy(double amount) {
94+
if (Platform.isIOS) {
95+
return appleMaps.CameraUpdate.zoomBy(amount);
96+
} else if (Platform.isAndroid) {
97+
return googleMaps.CameraUpdate.zoomBy(amount);
98+
}
99+
}
100+
101+
static zoomIn() {
102+
if (Platform.isIOS) {
103+
return appleMaps.CameraUpdate.zoomIn();
104+
} else if (Platform.isAndroid) {
105+
return googleMaps.CameraUpdate.zoomIn();
106+
}
107+
}
108+
109+
static zoomOut() {
110+
if (Platform.isIOS) {
111+
return appleMaps.CameraUpdate.zoomOut();
112+
} else if (Platform.isAndroid) {
113+
return googleMaps.CameraUpdate.zoomOut();
114+
}
115+
}
116+
117+
static zoomTo(double zoom) {
118+
if (Platform.isIOS) {
119+
return appleMaps.CameraUpdate.zoomTo(zoom);
120+
} else if (Platform.isAndroid) {
121+
return googleMaps.CameraUpdate.zoomTo(zoom);
122+
}
123+
}
124+
}

lib/src/controller.dart

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
part of flutter_platform_maps;
2+
3+
class PlatformMapController {
4+
appleMaps.AppleMapController appleController;
5+
googleMaps.GoogleMapController googleController;
6+
7+
PlatformMapController(dynamic controller) {
8+
if (controller.runtimeType == googleMaps.GoogleMapController) {
9+
this.googleController = controller;
10+
} else if (controller.runtimeType == appleMaps.AppleMapController) {
11+
this.appleController = controller;
12+
}
13+
}
14+
15+
/// Starts an animated change of the map camera position.
16+
///
17+
/// The returned [Future] completes after the change has been started on the
18+
/// platform side.
19+
Future<void> animateCamera(cameraUpdate) async {
20+
if (Platform.isIOS) {
21+
return this.appleController.animateCamera(cameraUpdate);
22+
} else if (Platform.isAndroid) {
23+
return this.googleController.animateCamera(cameraUpdate);
24+
}
25+
}
26+
27+
/// Changes the map camera position.
28+
///
29+
/// The returned [Future] completes after the change has been made on the
30+
/// platform side.
31+
Future<void> moveCamera(CameraUpdate cameraUpdate) async {
32+
if (Platform.isIOS) {
33+
return this.appleController.moveCamera(cameraUpdate);
34+
} else if (Platform.isAndroid) {
35+
return this.googleController.moveCamera(cameraUpdate);
36+
}
37+
}
38+
}

lib/src/location.dart

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
// Copyright 2018 The Chromium Authors. All rights reserved.
2-
// Use of this source code is governed by a BSD-style license that can be
3-
// found in the LICENSE file.
4-
51
part of flutter_platform_maps;
62

73
/// A pair of latitude and longitude coordinates, stored as degrees.

lib/src/map_type.dart

Lines changed: 0 additions & 27 deletions
This file was deleted.

lib/src/marker.dart

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
1-
// Copyright 2018 The Chromium Authors. All rights reserved.
2-
// Use of this source code is governed by a BSD-style license that can be
3-
// found in the LICENSE file.
4-
51
part of flutter_platform_maps;
62

7-
dynamic _offsetToJson(Offset offset) {
8-
if (offset == null) {
9-
return null;
10-
}
11-
return <dynamic>[offset.dx, offset.dy];
12-
}
13-
143
/// Text labels for a [Marker] info window.
154
class InfoWindow {
165
const InfoWindow({
@@ -142,4 +131,52 @@ class Marker {
142131
onTap: this.onTap,
143132
position: this.position.appleLatLng,
144133
);
134+
135+
googleMaps.Marker get googleMapsMarker => googleMaps.Marker(
136+
markerId: this.markerId.googleMapsMarkerId,
137+
alpha: this.alpha,
138+
draggable: this.draggable,
139+
infoWindow: this.infoWindow.googleMapsInfoWindow,
140+
onTap: this.onTap,
141+
position: this.position.googleLatLng,
142+
);
143+
144+
static appleMaps.Annotation appleMapsAnnotationFromMarker(Marker marker) {
145+
return appleMaps.Annotation(
146+
annotationId: marker.markerId.appleMapsAnnoationId,
147+
alpha: marker.alpha,
148+
draggable: marker.draggable,
149+
infoWindow: marker.infoWindow.appleMapsInfoWindow,
150+
onTap: marker.onTap,
151+
position: marker.position.appleLatLng,
152+
);
153+
}
154+
155+
static googleMaps.Marker googleMapsMarkerFromMarker(Marker marker) {
156+
return googleMaps.Marker(
157+
markerId: marker.markerId.googleMapsMarkerId,
158+
alpha: marker.alpha,
159+
draggable: marker.draggable,
160+
infoWindow: marker.infoWindow.googleMapsInfoWindow,
161+
onTap: marker.onTap,
162+
position: marker.position.googleLatLng,
163+
);
164+
}
165+
166+
static Set<appleMaps.Annotation> toAppleMapsAnnotationSet(
167+
Set<Marker> markers) {
168+
Set<appleMaps.Annotation> _annotations = Set<appleMaps.Annotation>();
169+
markers.forEach((marker) {
170+
_annotations.add(appleMapsAnnotationFromMarker(marker));
171+
});
172+
return _annotations;
173+
}
174+
175+
static Set<googleMaps.Marker> toGoogleMapsMarkerSet(Set<Marker> markers) {
176+
Set<googleMaps.Marker> _markers = Set<googleMaps.Marker>();
177+
markers.forEach((marker) {
178+
_markers.add(googleMapsMarkerFromMarker(marker));
179+
});
180+
return _markers;
181+
}
145182
}

0 commit comments

Comments
 (0)