我试图在地图上显示三件事情:
GPS(当前位置),Marker 1,Marker 2,但我不知道我做得对不对!这是我的代码:
GPS(当前位置),Marker 1,Marker 2,但我不知道我做得对不对!这是我的代码:
self.startMarker.position = CLLocationCoordinate2D(latitude: self.startLatitude,longitude: self.startLongitude) self.startMarker.icon = #imageLiteral(resourceName: "Pin Start") self.startMarker.map = self.mapView self.endMarker.position = CLLocationCoordinate2D(latitude: self.endLatitude,longitude: self.endLongitude) self.endMarker.icon = #imageLiteral(resourceName: "Pin End") self.endMarker.map = self.mapView let southWest = CLLocationCoordinate2DMake(self.startLatitude,self.startLongitude) let northEast = CLLocationCoordinate2DMake(self.endLatitude,self.endLongitude) let bounds = GMSCoordinateBounds(coordinate: northEast,coordinate: southWest) let camera = self.mapView.camera(for: bounds,insets:.zero) self.mapView.camera = camera!
更多代码:
// MARK: - Google Map private func locationManager(manager: CLLocationManager,didChangeAuthorizationStatus status: CLAuthorizationStatus) { if status == CLAuthorizationStatus.authorizedWhenInUse { mapView.isMyLocationEnabled = true } } func locationManager(_ manager: CLLocationManager,didUpdateLocations locations: [CLLocation]) { let newLocation = locations.last mapView.camera = GMSCameraPosition.camera(withTarget: newLocation!.coordinate,zoom: 14) mapView.isMyLocationEnabled = true }
结果是这样的:
我需要的 :
EDITED
if let myLocation = self.mapView.myLocation { let path = GMSMutablePath() path.add(myLocation.coordinate) path.add(self.startMarker.position) path.add(self.endMarker.position) let bounds = GMSCoordinateBounds(path: path) self.mapView.animate(with: GMSCameraUpdate.fit(bounds,withPadding: 40))
}
解决方法
显示屏幕界限的所有标记:
在这里我试图给你一个简单的例子,希望这将有助于你.
使用此功能,您可以在屏幕上显示任意数量的标记.
例:
let path = GMSMutablePath() for var i in 0 ..< array.count { let lat = Double(array.objectAtIndex(i).valueForKey(lattitude) as! String) let long = Double(arrayr.objectAtIndex(i).valueForKey(longitude) as! String) marker.position = CLLocationCoordinate2DMake(lat!,long!) path.addCoordinate(marker.position) marker.map = self.mapView } let bounds = GMSCoordinateBounds(path: path) self.mapView!.animateWithCameraUpdate(GMSCameraUpdate.fitBounds(bounds,withPadding: 30.0))