-
Notifications
You must be signed in to change notification settings - Fork 125
/
SwiftMapLauncherPlugin.swift
248 lines (211 loc) · 9.47 KB
/
SwiftMapLauncherPlugin.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import Flutter
import UIKit
import MapKit
private enum MapType: String {
case apple
case google
case amap
case baidu
case waze
case yandexNavi
case yandexMaps
case citymapper
case mapswithme
case osmand
case doubleGis
case tencent
case here
case tomtomgo
case tomtomgofleet
case copilot
case sygicTruck
case naver
case kakao
case tmap
case mapyCz
case mappls
func type() -> String {
return self.rawValue
}
}
private class Map {
let mapName: String;
let mapType: MapType;
let urlPrefix: String?;
init(mapName: String, mapType: MapType, urlPrefix: String?) {
self.mapName = mapName
self.mapType = mapType
self.urlPrefix = urlPrefix
}
func toMap() -> [String:String] {
return [
"mapName": mapName,
"mapType": mapType.type(),
]
}
}
private let maps: [Map] = [
Map(mapName: "Apple Maps", mapType: MapType.apple, urlPrefix: ""),
Map(mapName: "Google Maps", mapType: MapType.google, urlPrefix: "comgooglemaps://"),
Map(mapName: "Amap", mapType: MapType.amap, urlPrefix: "iosamap://"),
Map(mapName: "Baidu Maps", mapType: MapType.baidu, urlPrefix: "baidumap://"),
Map(mapName: "Waze", mapType: MapType.waze, urlPrefix: "waze://"),
Map(mapName: "Yandex Navigator", mapType: MapType.yandexNavi, urlPrefix: "yandexnavi://"),
Map(mapName: "Yandex Maps", mapType: MapType.yandexMaps, urlPrefix: "yandexmaps://"),
Map(mapName: "Citymapper", mapType: MapType.citymapper, urlPrefix: "citymapper://"),
Map(mapName: "MAPS.ME", mapType: MapType.mapswithme, urlPrefix: "mapswithme://"),
Map(mapName: "OsmAnd", mapType: MapType.osmand, urlPrefix: "osmandmaps://"),
Map(mapName: "2GIS", mapType: MapType.doubleGis, urlPrefix: "dgis://"),
Map(mapName: "Tencent (QQ Maps)", mapType: MapType.tencent, urlPrefix: "qqmap://"),
Map(mapName: "HERE WeGo", mapType: MapType.here, urlPrefix: "here-location://"),
Map(mapName: "TomTom Go", mapType: MapType.tomtomgo, urlPrefix: "tomtomgo://"),
Map(mapName: "TomTom Go Fleet", mapType: MapType.tomtomgofleet, urlPrefix: "tomtomgofleet://"),
Map(mapName: "Sygic Truck", mapType: MapType.sygicTruck, urlPrefix: "com.sygic.aura://"),
Map(mapName: "CoPilot", mapType: MapType.copilot, urlPrefix: "copilot://"),
Map(mapName: "Naver Map", mapType: MapType.naver, urlPrefix: "nmap://"),
Map(mapName: "Kakao Maps", mapType: MapType.kakao, urlPrefix: "kakaomap://"),
Map(mapName: "TMap", mapType: MapType.tmap, urlPrefix: "tmap://"),
Map(mapName: "Mapy CZ", mapType: MapType.mapyCz, urlPrefix: "szn-mapy://"),
Map(mapName: "Mappls MapmyIndia", mapType: MapType.mappls, urlPrefix: "mappls://")
]
private func getMapByRawMapType(type: String) -> Map? {
return maps.first(where: { $0.mapType.type() == type })
}
private func getMapItem(latitude: String, longitude: String, name: String?) -> MKMapItem {
let coordinate = CLLocationCoordinate2DMake(Double(latitude)!, Double(longitude)!)
let destinationPlacemark = MKPlacemark(coordinate: coordinate, addressDictionary: nil)
let mapItem = MKMapItem(placemark: destinationPlacemark)
mapItem.name = name
return mapItem
}
private func getDirectionsMode(directionsMode: String?) -> String {
switch directionsMode {
case "driving":
return MKLaunchOptionsDirectionsModeDriving
case "walking":
return MKLaunchOptionsDirectionsModeWalking
case "transit":
if #available(iOS 9.0, *) {
return MKLaunchOptionsDirectionsModeTransit
} else {
return MKLaunchOptionsDirectionsModeDriving
}
default:
if #available(iOS 10.0, *) {
return MKLaunchOptionsDirectionsModeDefault
} else {
return MKLaunchOptionsDirectionsModeDriving
}
}
}
private func showMarker(mapType: MapType, url: String, title: String, latitude: String, longitude: String) {
switch mapType {
case MapType.apple:
let coordinate = CLLocationCoordinate2DMake(Double(latitude)!, Double(longitude)!)
let region = MKCoordinateRegion(center: coordinate, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.02))
let placemark = MKPlacemark(coordinate: coordinate, addressDictionary: nil)
let mapItem = MKMapItem(placemark: placemark)
let options = [
MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: region.center),
MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: region.span)]
mapItem.name = title
mapItem.openInMaps(launchOptions: options)
default:
UIApplication.shared.open(URL(string:url)!, options: [:], completionHandler: nil)
}
}
private func showDirections(mapType: MapType, url: String, destinationTitle: String?, destinationLatitude: String, destinationLongitude: String, originTitle: String?, originLatitude: String?, originLongitude: String?, directionsMode: String?, waypoints: [[String: String?]]?) {
switch mapType {
case MapType.apple:
let destinationMapItem = getMapItem(latitude: destinationLatitude, longitude: destinationLongitude, name: destinationTitle ?? "Destination");
let hasOrigin = originLatitude != nil && originLatitude != nil
var originMapItem: MKMapItem {
if !hasOrigin {
return MKMapItem.forCurrentLocation()
}
let origin = getMapItem(latitude: originLatitude!, longitude: originLongitude!, name: originTitle ?? "Origin")
return origin
}
let waypointMapItems = waypoints == nil ? [MKMapItem]() : waypoints!.map { getMapItem(latitude: $0["latitude"]!!, longitude: $0["longitude"]!!, name: $0["title"] as? String) }
MKMapItem.openMaps(
with: [originMapItem] + waypointMapItems + [destinationMapItem],
launchOptions: [MKLaunchOptionsDirectionsModeKey: getDirectionsMode(directionsMode: directionsMode)]
)
default:
UIApplication.shared.open(URL(string:url)!, options: [:], completionHandler: nil)
}
}
private func isMapAvailable(map: Map?) -> Bool {
// maptype is not available on iOS
guard let map = map else {
return false
}
if map.mapType == MapType.apple {
return true
}
return UIApplication.shared.canOpenURL(URL(string:map.urlPrefix!)!)
}
public class SwiftMapLauncherPlugin: NSObject, FlutterPlugin {
public static func register(with registrar: FlutterPluginRegistrar) {
let channel = FlutterMethodChannel(name: "map_launcher", binaryMessenger: registrar.messenger())
let instance = SwiftMapLauncherPlugin()
registrar.addMethodCallDelegate(instance, channel: channel)
}
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
switch call.method {
case "getInstalledMaps":
result(maps.filter({ isMapAvailable(map: $0) }).map({ $0.toMap() }))
case "showMarker":
let args = call.arguments as! NSDictionary
let mapType = args["mapType"] as! String
let url = args["url"] as! String
let title = args["title"] as! String
let latitude = args["latitude"] as! String
let longitude = args["longitude"] as! String
let map = getMapByRawMapType(type: mapType)
if (!isMapAvailable(map: map)) {
result(FlutterError(code: "MAP_NOT_AVAILABLE", message: "Map is not installed on a device", details: nil))
return;
}
showMarker(mapType: MapType(rawValue: mapType)!, url: url, title: title, latitude: latitude, longitude: longitude)
result(nil)
case "showDirections":
let args = call.arguments as! NSDictionary
let mapType = args["mapType"] as! String
let url = args["url"] as! String
let destinationTitle = args["destinationTitle"] as? String
let destinationLatitude = args["destinationLatitude"] as! String
let destinationLongitude = args["destinationLongitude"] as! String
let originTitle = args["originTitle"] as? String
let originLatitude = args["originLatitude"] as? String
let originLongitude = args["originLongitude"] as? String
let directionsMode = args["directionsMode"] as? String
let waypoints = args["waypoints"] as? [[String: String?]]
let map = getMapByRawMapType(type: mapType)
if (!isMapAvailable(map: map)) {
result(FlutterError(code: "MAP_NOT_AVAILABLE", message: "Map is not installed on a device", details: nil))
return;
}
showDirections(
mapType: MapType(rawValue: mapType)!,
url: url,
destinationTitle: destinationTitle,
destinationLatitude: destinationLatitude,
destinationLongitude: destinationLongitude,
originTitle: originTitle,
originLatitude: originLatitude,
originLongitude: originLongitude,
directionsMode: directionsMode,
waypoints: waypoints
)
result(nil)
case "isMapAvailable":
let args = call.arguments as! NSDictionary
let mapType = args["mapType"] as! String
let map = getMapByRawMapType(type: mapType)
result(isMapAvailable(map: map))
default:
result(FlutterMethodNotImplemented)
}
}
}