首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Google的方法如MapKit的struct MKCoordinateRegion

使用Google的方法如MapKit的struct MKCoordinateRegion
EN

Stack Overflow用户
提问于 2015-07-25 22:09:01
回答 1查看 624关注 0票数 0

我为Google为本教程重新设计了iOS。我想通过提供一个中心点和一个定义水平和垂直范围的跨度来定义它。我读过本教程介绍用于iOS的Google,我认为应该使用zoomAtCoordinate:forMeters:perPoints: --在我的观点中,这是不完全正确的

代码语言:javascript
复制
    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;

    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-26 14:44:46

您必须使用GMSCoordinateBounds

代码语言:javascript
复制
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)) 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31631623

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档