MKMapView可见区域填充

问题描述 投票:7回答:5

德拉同事,

我很难用原生MKMapView实现可见区域。我需要有一个与Google Map SDK Visible Region完全相同的功能。

基本上它意味着在底部有一种填充到下面的代码:

let span = MKCoordinateSpan(latitudeDelta: 0.8, longitudeDelta: 0.8)
let reg = MKCoordinateRegion(center: someMyCoordinate, span: span)
mapView.setRegion(reg, animated: true)

它必须是这样的:一旦我在UIView坐标中添加了一些填充,它应该移动地图中心并调整缩放以使坐标区域完全可见,同时考虑填充。

可能我的描述有点乱,但如果你看看this video它就变得非常清楚了。

先感谢您!

ios swift mkmapview google-maps-sdk-ios
5个回答
6
投票

如果您打印可见区域,您将看到跨度将被填充,因为它需要适合视图。中心坐标仍将位于视图的中心。我不确定你想做什么,但我想它可以存档

setVisibleMapRect:edgePadding:animated:

您需要将区域转换为MKMapRect。请参阅Convert MKCoordinateRegion to MKMapRect了解如何做到这一点


10
投票

由于上面建议的方法,完整的解决方案如下。

import Foundation
import MapKit
extension MKCoordinateRegion{
    var mapRect:MKMapRect {
        get{
            let a = MKMapPointForCoordinate(CLLocationCoordinate2DMake(
                   self.center.latitude + self.span.latitudeDelta / 2,
                   self.center.longitude - self.span.longitudeDelta / 2))

            let b = MKMapPointForCoordinate(CLLocationCoordinate2DMake(
                    self.center.latitude - self.span.latitudeDelta / 2,
                    self.center.longitude + self.span.longitudeDelta / 2))

            return MKMapRectMake(min(a.x,b.x), min(a.y,b.y), abs(a.x-b.x), abs(a.y-b.y))
        }
    }
}

extension MKMapView {
    func setVisibleRegion(mapRegion: MKCoordinateRegion, edgePadding insets: UIEdgeInsets, animated animate: Bool) {
        self.setVisibleMapRect(mapRegion.mapRect, edgePadding: insets , animated: animate)
    }
}

现在你可以使用setVisibleRegion func。


3
投票

如果你需要在swift中从100的底部填充,你可以简单地写:

mapView.layoutMargins = UIEdgeInsets(top: 10, right: 10, bottom: 100, left: 10)

然后你可以检查这个地图中心的一些注释,如下所示:

mapView.showAnnotations(mapView.annotations, animated: true)

0
投票

谷歌填充背后的原因是让googles按钮和版权不受影响,因此您可以在地图边缘提供自己的用户界面。 (如Google地图视频中所示)

从iOS 11开始,Apple解决了同样的问题,不是填充,而是让您控制将控件放在新类的位置。那些是

MKScaleView
MKCompassButton
MKUserTrackingButton

将它们放在地图上的任何位置,甚至放在地图之外。它们与MKMapView完全整合。

Apple忘记的唯一控件是左下角的法律链接(至少对于ltr语言)。所以这个链接总是妨碍我们的设计,至少在iOS 11中。

也许你真的想填充,然后与其他答案一起去。但是如果您想要控制放置Apples控件并且能够支持不支持iOS 11之前的iOS版本,那么请使用新类。


-1
投票

您需要一些逻辑坐标设置如下并设置区域,然后您可以设置缩放级别与谷歌地图相同。

MKCoordinateRegion地区;

CLLocationDegrees maxLat = -90;
CLLocationDegrees maxLon = -180;
CLLocationDegrees minLat = 90;
CLLocationDegrees minLon = 180;

float currentLatitude;

float currentLongitude;

if(currentLatitude> maxLat)

maxLat = currentLatitude;

if(currentLatitude <minLat)

minLat = currentLatitude;

if(当前经度> maxoLon)

maxLon = currentLongitude;

if(currentLongitude <minLon)

minLon = currentLongitude;

region.center.latitude =(maxLat + minLat)/ 2;

region.center.longitude =(maxLon + minLon)/ 2;

region.span.latitudeDelta = maxLat - minLat;

region.span.longitudeDelta = maxLon - minLon;

[myMapview setRegion:region animated:YES];

[myMapview setCenterCoordinate:theCoordinate zoomLevel:13 animated:YES];

© www.soinside.com 2019 - 2024. All rights reserved.