ios – Google地图缩小到GPS和标记

前端之家收集整理的这篇文章主要介绍了ios – Google地图缩小到GPS和标记前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图在地图上显示三件事情:
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))
原文链接:https://www.f2er.com/iOS/337245.html

猜你在找的iOS相关文章