【问题标题】:Google Maps zoom out to GPS and markers谷歌地图缩小到 GPS 和标记
【发布时间】:2017-02-14 08:04:30
【问题描述】:

我试图在地图上显示三件事: 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
        }

结果是这样的:

我需要什么:

已编辑

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))

}

【问题讨论】:

  • 我已经添加了一个答案,检查一下,确保调用了 didUpdateLocations()

标签: ios iphone swift xcode google-maps


【解决方案1】:

显示所有带有屏幕绑定的标记:

在这里,我试图为您提供一个简单的示例,希望对您有所帮助。 使用它,您可以在屏幕上显示任意数量的标记。

示例:

let path = GMSMutablePath()
for var i in 0 ..< array.count //Here take your "array" which contains lat and long.
{
    let marker = GMSMarker()
    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))

【讨论】:

  • 您能告诉我应该如何填写array.count 吗?
  • @Mc.Lover 看,如果你有不同的纬度和经度,如果你从后端获取它,那么你可以直接使用那个,或者通过添加你的经度和使用那个数组来创建你自己的数组.
  • 谢谢!但还是不行!你能看看我编辑的代码吗?
【解决方案2】:

你可以试试下面的代码,这是从ObjC代码转换而来的,这里是includingCoordinate的文档

let bounds = GMSCoordinateBounds()
bounds.includingCoordinate(self.startMarker.position)
bounds.includingCoordinate(self.endMarker.position)
bounds.includingCoordinate(yourCurrentLocationPosition)
self.mapView.animateWithCameraUpdate(GMSCameraUpdate(fitBounds:bounds, padding:20.0f))

【讨论】:

    【解决方案3】:
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        showMarkers(locations)
        //stop updating location when you find suitable
    }
    
    func showMarkers(userLocation: [CLLocation]){
    
        let location1: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: self.startLatitude, longitude: self.startLongitude)
        let location2: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: self.endLatitude, longitude: self.endLongitude)
        let location3: CLLocationCoordinate2D = userLocation[0].coordinate
    
        var locationArray = []
        locationArray.append(location1)
        locationArray.append(location2)
        locationArray.append(location3)
    
        var bounds = GMSCoordinateBounds()
        for location in locationArray
        {
            let latitude = location.valueForKey("latitude")
            let longitude = location.valueForKey("longitude")
    
            let marker = GMSMarker()
            marker.position = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
            marker.map = self.mapView
            bounds = bounds.includingCoordinate(marker.position)
        }
        let update = GMSCameraUpdate.fitBounds(bounds, withPadding: 100)
        mapView.animateWithCameraUpdate(update)
    }
    

    注意:

    locationArray 包含三个位置,即。 标记 1标记 2用户位置

    【讨论】:

      【解决方案4】:

      对于正在寻找 Objective-C 解决方案的人来说,这可能会有所帮助

      //Locations
      CLLocationCoordinate2D source = CLLocationCoordinate2DMake(19.2880, 72.1587);
      CLLocationCoordinate2D userLocation = CLLocationCoordinate2DMake(19.1780, 72.9577);
      CLLocationCoordinate2D destination = CLLocationCoordinate2DMake(19.0760, 72.8777);
      
      //Create a GMSMutablePath
      GMSMutablePath *path = [GMSMutablePath new];
      [path addCoordinate:source];
      [path addCoordinate:userLocation];
      [path addCoordinate:destination];
      
      //Create bounds
      GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc]initWithPath:path];
      
      //Update the camera position
      [mapview animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds]];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-07-10
        • 1970-01-01
        • 2018-11-25
        • 1970-01-01
        • 2015-07-12
        • 1970-01-01
        • 1970-01-01
        • 2013-07-20
        相关资源
        最近更新 更多