我正在处理一些 iPhone MapKit 代码,当地图边界发生变化时会触发一些事件(基本上是对外部服务器的一些查询)。有一个我似乎无法解决的边缘情况。当用户输入新地址时,我使用 setRegion 放大该位置,然后计算地图的边界并使用这些坐标查询我的外部服务器。我总是调用 setRegion 但是,当它不会触发时有一种边缘情况。也就是说,当用户神奇地碰巧选择了位于地图当前中心的完全相同的地址时,当该地图被缩放到完全相同的区域时。当然,这种可能性很少见,但使用以下代码很容易实现:
CLLocation *centerOfMap = [[CLLocation alloc] initWithLatitude:mapView.centerCoordinate.latitude longitude:mapView.centerCoordinate.longitude];
mySearchLocation.searchLocation = centerOfMap;
//Recenter and zoom map in on search location
MKCoordinateRegion region = {{0.0f, 0.0f}, {0.0f, 0.0f}};
region.center = mySearchLocation.searchLocation.coordinate;region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[self.mapView setRegion:region animated:YES];
Run Code Online (Sandbox Code Playgroud)
这相当于允许用户在当前地图的中心放置一个大头针,然后根据它向我的服务器发出查询。不幸的是,因为代码逻辑取决于区域的变化(我这样做是为了让用户可以轻松地在地图上平移而不必显式按下刷新按钮),我需要一个解决方法。我如何知道何时调用了 setRegion 但它不会触发 regionWillChange / regionDidChange。我可以以某种方式覆盖该功能吗?
编辑:当我评论第一个答案时,我尝试使用以下代码检测区域何时不会改变:
//Recenter and zoom map in on search location
MKCoordinateRegion region = {{0.0f, 0.0f}, {0.0f, 0.0f}};
region.center = mySearchLocation.searchLocation.coordinate;
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
region = [mapView regionThatFits:region]; //Not …Run Code Online (Sandbox Code Playgroud) 当地图视图处于 MKUserTrackingModeFollowWithHeading 模式时,我想缩小以包含最近的注释。
我试图像这样设置区域:
MKCoordinateRegion currentRegion = self.mapView.region;
currentRegion.span.latitudeDelta *= 4;
currentRegion.span.longitudeDelta *= 4;
[self.mapView setRegion:currentRegion];
Run Code Online (Sandbox Code Playgroud)
这会将地图缩小 4 倍,但在启用用户跟踪的情况下,iOS 会通过动画自动缩放回原始缩放级别。我无法让任何地区坚持下去。我假设 MapRect 会具有相同的行为,但我还没有尝试过。
有谁知道另一种方法来做到这一点?
根据有关 setUserTrackingMode 的文档:
如果地图被缩小,地图视图会自动放大用户的位置,从而有效地改变当前的可见区域。
所以我意识到这可能是不可能的。但是,用户可以通过捏来缩小,并且用户跟踪模式保持启用状态而无需放大。非常感谢任何创意。
我想访问MKMapItem对象中的数据。在搜索信息后,我向社区询问。根据Apple class reference,有这些属性:
如果我登录,MKMapItem我会得到显示为字典的 JSON 字典的内容,例如:
许多项目包含我想要的信息,这些信息不是MKMapItem. 有没有办法解析和建模这些项目?我试过了
NSDictionary *mapItemDictionary = (NSDictionary *)mapItem;
[mapItemDictionary valueForKey:@"key"];
Run Code Online (Sandbox Code Playgroud)
,这会导致崩溃:
[<MKMapItem 0xb02d830> valueForUndefinedKey:]:此类不符合键地址的键值编码。
我有一个简单的 Mac 应用程序,不打算用于任何类型的分发;只是个人使用。该应用程序是一个NSWindow包含MKMapView. 由于我没有 Mac 开发人员帐户,也不想要(请参阅“仅供个人使用”),因此我不会进行任何形式的代码签名或配置。
但是,在启动时,我得到一个空白地图和这个错误:
您的应用程序已尝试访问 Map Kit API。如果没有授权,您将无法访问此 API。您可能会从 Mac 开发人员计划获得一项权利,仅供您在 Mac App Store 应用程序中使用。有关 Apple Mac 开发人员计划的更多信息,请访问 developer.apple.com。
我真的需要购买 Mac 开发者帐户才能MKMapView在 OS X上使用吗?如果没有,我错过了什么?
我有两组坐标并试图在地图视图上绘制折线。我可以用一组坐标绘制多段线,但无法绘制两条多段线。
下面是代码...
func drawRouteOnMap()
{
var centerCoordinate : CLLocationCoordinate2D = CLLocationCoordinate2DMake(icRouteLat[0], icRouteLong[0])
let span = MKCoordinateSpanMake(0.001, 0.001)
var centerPosition = MKCoordinateRegionMake(centerCoordinate, span)
mapView.setRegion(centerPosition,animated:true)
self.mapView.mapType = MKMapType.Hybrid
routePointer = "one";
// first route line
//----------able to draw polyline with this set of coordinates
for i in 0..<routeLat.count-1
{
var fromCoordinate :CLLocation = CLLocation(latitude: routeLat[i], longitude: routeLong[i])
var toCoordinate :CLLocation = CLLocation(latitude: routeLat[i+1], longitude: routeLong[i+1])
var locations = [fromCoordinate, toCoordinate];
var coordinates = locations.map({(location: CLLocation) -> CLLocationCoordinate2D in return location.coordinate});
var polyLine …Run Code Online (Sandbox Code Playgroud) 我需要你的帮助,我正在开发一个应用程序,我有一些引脚(位置),我想要的是获得每个和我的位置之间的距离.我的代码如下
let annotation = MKPointAnnotation()
let annotationTwo = MKPointAnnotation()
let saintPaulHospitalBC = MKPointAnnotation()
override func viewDidLoad() {
super.viewDidLoad()
mapita.showsUserLocation = true // Mapita is the name of the MapView.
annotation.coordinate = CLLocationCoordinate2D(latitude: 25.647399800, longitude: -100.334304500)
mapita.addAnnotation(annotation)
annotationTwo.coordinate = CLLocationCoordinate2D(latitude: 25.589339000, longitude: -100.257724800)
mapita.addAnnotation(annotationTwo)
saintPaulHospitalBC.coordinate = CLLocationCoordinate2D(latitude: 49.280524700, longitude: -123.128232600)
mapita.addAnnotation(SaintPaulHospitalBC)
}
Run Code Online (Sandbox Code Playgroud)
当我运行代码时,地图显示了引脚,但我还能做些什么才能开始计算距离?谢谢!
我正在尝试实现一个 MKUserTrackingButton(这是 iOS 11 SDK 中的新功能)。这个“按钮”实际上是从 UIView 继承的,所以我只是在我的 Map 和 Identity Inspector 中放置了一个 UIView 实例,将它链接到 MKUserTrackingButton 类,在我的代码和 viewDidLoad() 中添加了一个出口。
我用以下方式初始化它:
self.centerMapButton = MKUserTrackingButton.init(mapView: self.mapView)
Run Code Online (Sandbox Code Playgroud)
但是,没有任何效果,我的地图上只有一个空白视图。
PS:这是关于这个新功能的 WWDC 2017 会议(在 1:25):https : //developer.apple.com/videos/play/wwdc2017/237/
这是我在 viewDidLoad() 中调用以实现按钮的方法:
func setupUserTrackingButtonAndScaleView() {
mapView.showsUserLocation = true
let button = MKUserTrackingButton(mapView: mapView)
button.layer.backgroundColor = UIColor(white: 1, alpha: 0.8).cgColor
button.layer.borderColor = UIColor.white.cgColor
button.layer.borderWidth = 1
button.layer.cornerRadius = 5
button.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(button)
let scale = MKScaleView(mapView: mapView)
scale.legendAlignment = .trailing
scale.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(scale)
NSLayoutConstraint.activate([button.bottomAnchor.constraint(equalTo: view.bottomAnchor, …Run Code Online (Sandbox Code Playgroud) 我正在尝试注册自定义注释视图(我将它们创建为 MKAnnotationView 的子类),但是我遇到了一些麻烦。最初,我尝试使用以下代码为单个注释标记仅注册一个自定义注释视图(RestaurantMarkerView 是我的自定义注释视图):
mapView.register(RestaurantMarkerView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)
Run Code Online (Sandbox Code Playgroud)
它工作正常(当我点击图钉时,它会显示标注)。现在我添加另一个注释标记并尝试为其注册另一个视图,据我所知,在这种情况下,我应该为视图使用自定义标识符,因此我通过以下代码执行此操作:
mapView.register(RestaurantMarkerView.self, forAnnotationViewWithReuseIdentifier: "a")
mapView.register(ChoosenRestaurantMarkerView.self, forAnnotationViewWithReuseIdentifier: "b")
Run Code Online (Sandbox Code Playgroud)
现在,当我点击标记时,它们不会显示标注。当我将其中一个标记的重用标识符更改为 时MKMapViewDefaultAnnotationViewReuseIdentifier,该标记会显示标注。那么,如何注册多个自定义注释视图?谢谢。
我正在尝试在地图中的图钉顶部显示自定义呼叫。我需要在图钉顶部显示带有一些文字的图像作为我的呼唤。我编写的代码产生以下输出。
但是,我想要的输出如下所示:
我怎样才能达到我想要的输出?请注意,气泡是必须使用的图像资产,我知道如何使用 CAShapeLayer 绘制气泡,但这并不能完成我想要的。这是到目前为止的代码。
// methods for Custom annotations
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let annotationView = MKAnnotationView(annotation: pin, reuseIdentifier: "UserLocation")
annotationView.image = UIImage(named: "marker")
annotationView.canShowCallout = true
configureDetailView(annotationView: annotationView)
return annotationView
}
func configureDetailView(annotationView: MKAnnotationView) {
let width = 300
let height = 200
let calloutView = UIView()
calloutView.translatesAutoresizingMaskIntoConstraints = false
let views = ["calloutView": calloutView]
calloutView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:[calloutView(300)]", options: [], metrics: nil, views: views))
calloutView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[calloutView(200)]", options: [], metrics: nil, views: views))
let …Run Code Online (Sandbox Code Playgroud) mapkit ×10
ios ×6
swift ×5
mkmapview ×3
iphone ×2
cocoa-touch ×1
geolocation ×1
ios11 ×1
ios7 ×1
json ×1
mkmapitem ×1
mkpolyline ×1
nsdictionary ×1
swift4 ×1