ios - 使用 Google Maps SDK 的哪个方法,例如 MapKit 的 struct MKCooperativeRegion

标签 ios swift google-maps mapkit

我返工this tutorial适用于 iOS 版 Google map SDK。我想通过提供中心点和定义水平和垂直范围的跨度来定义它。 我读过this tutorial of Google Maps SDK for iOS我认为应该使用 zoomAtCooperative:forMeters:perPoints: 在我看来,这不太正确

    func mapRegion() -> MKCoordinateRegion {
        var region: MKCoordinateRegion = MKCoordinateRegion(center: CLLocationCoordinate2DMake(0, 0), span: MKCoordinateSpanMake(0, 0))
        var initialLoc: Location = self.trek.locations.firstObject as! Location
var minLat = (initialLoc as Location).latitude.doubleValue
        var minLng = (initialLoc as Location).longitude.doubleValue
        var maxLat = (initialLoc as Location).latitude.doubleValue
        var maxLng = (initialLoc as Location).longitude.doubleValue

        for location in self.trek.locations.array {
            if (location as! Location).latitude.doubleValue < minLat {
                minLat = (location as! Location).latitude.doubleValue;
            }
            if (location as! Location).longitude.doubleValue < minLng {
                minLng = (location as! Location).longitude.doubleValue;
            }
            if (location as! Location).latitude.doubleValue > maxLat {
                maxLat = (location as! Location).latitude.doubleValue;
            }
            if (location as! Location).longitude.doubleValue > maxLng {
                maxLng = (location as! Location).longitude.doubleValue;
            }
        }

        region.center.latitude = (minLat + maxLat) / 2.0;
        region.center.longitude = (minLng + maxLng) / 2.0;

        region.span.latitudeDelta = (maxLat - minLat) * 1.1; // 10% padding
        region.span.longitudeDelta = (maxLng - minLng) * 1.1; // 10% padding

        return region;

    }

最佳答案

您必须使用GMSColinedBounds

func mapRegion() -> GMSCoordinateBounds {
        var path = GMSMutablePath()
        var bounds = GMSCoordinateBounds(path: path)
        var initialLoc: Location = self.trek.locations.firstObject as! Location    
        var minLat = (initialLoc as Location).latitude.doubleValue
        var minLng = (initialLoc as Location).longitude.doubleValue
        var maxLat = (initialLoc as Location).latitude.doubleValue
        var maxLng = (initialLoc as Location).longitude.doubleValue      
        for location in self.trek.locations.array {
            if (location as! Location).latitude.doubleValue < minLat {
                minLat = (location as! Location).latitude.doubleValue;
            }
            if (location as! Location).longitude.doubleValue < minLng {
                minLng = (location as! Location).longitude.doubleValue;
            }
            if (location as! Location).latitude.doubleValue > maxLat {
                maxLat = (location as! Location).latitude.doubleValue;
            }
            if (location as! Location).longitude.doubleValue > maxLng {
                maxLng = (location as! Location).longitude.doubleValue;
            }
        }       
        path.addCoordinate(CLLocationCoordinate2DMake(minLat, minLng))
        path.addCoordinate(CLLocationCoordinate2DMake(maxLat, maxLng)) 
        return bounds;
    }   
self.viewMap?.animateWithCameraUpdate(GMSCameraUpdate.fitBounds(mapRegion(), withPadding: 15.0)) 

关于ios - 使用 Google Maps SDK 的哪个方法,例如 MapKit 的 struct MKCooperativeRegion,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31631623/

相关文章:

android - BottomSheet - 为什么 FAB 按钮在 View 上?当我再次关注 map 时如何隐藏 BottoSheet?

ios - assetExport完成后检索路径

iphone - 为什么是 "present modal view controller"?

ios - +entityForName : nil happens intermittently

ios - iAd 契约(Contract)到期?不是这样,iTunesConnect 说。 iAd Module 不这么认为

SwiftUI:如何使用 .presentation 修饰符进行警报?出现编译器类型检查错误

ios - 具有Alamofire下载功能的JWT认证

javascript - 单击覆盖 geojson 图层时,Google Maps API map 单击事件不起作用

Swift:当计时器触发时,如何阻止平移手势取消?

javascript - Google map 全屏选项被禁用?