如何将自定义地图图钉添加到 Apple Watch 的地图视图中?

How to add Custom Map Pins to the Map View on the Apple Watch?

我开始为 Apple Watch 编写代码,我想知道我该怎么做:

谢谢!

首先你需要给你的控制器添加一个地图视图(例如InterceController.swift

然后,您可以在 InterfaceController.swiftawakeWithContext()willActivate()

中调用这些行
 let location = CLLocationCoordinate2D(
            latitude: 51.530777,
            longitude: -0.139364
        )

 let span = MKCoordinateSpan(
            latitudeDelta: 0.005,
            longitudeDelta: 0.005
       )

 let region = MKCoordinateRegion(center: location, span: span)

 map.setRegion(region)

 map.addAnnotation(location, withImageNamed: "bee.png", centerOffset: CGPoint(x: 0, y: 0))

关键是调用addAnnotation(location: CLLocationCoordinate2D, withImageNamed name: String!, centerOffset offset: CGPoint)

等效Objective-C代码:

  CLLocationCoordinate2D location = CLLocationCoordinate2DMake(51.530777, -0.139364);

    MKCoordinateSpan span = MKCoordinateSpanMake(0.005, 0.005);

    MKCoordinateRegion region = MKCoordinateRegionMake(location, span);

    [self.map setRegion:region];

    [self.map addAnnotation:location withImageNamed:@"bee.png" centerOffset:CGPointMake(0, 0)];