看起来我遇到了一个问题,MKMapView如果我在更改地图的可见区域时添加注释,我可以可靠地导致崩溃。我已经将导致它的代码简化为一个非常简单的实现,并且我在这里重现它:
NSMutableArray *pointAnnotationArray = [[NSMutableArray alloc] init];\nMKCoordinateRegion coordRegion = [mapViewOutlet region];\nfloat randMax = 0.1;\n\nfor (int i = 0; i < 100; i++){\n float randomDeviation1 = (((float) (arc4random() % ((unsigned)RAND_MAX + 1)) / RAND_MAX) * randMax) - (randMax / 2);\n float randomDeviation2 = (((float) (arc4random() % ((unsigned)RAND_MAX + 1)) / RAND_MAX) * randMax) - (randMax / 2);\n\n MKPointAnnotation *point = [[MKPointAnnotation alloc] init]; \n CLLocationCoordinate2D pointLocation = CLLocationCoordinate2DMake(coordRegion.center.latitude + randomDeviation1, coordRegion.center.longitude + randomDeviation2);\n [point setCoordinate:pointLocation]; \n [pointAnnotationArray addObject: …Run Code Online (Sandbox Code Playgroud) 我创建了一个 Mapkit 应用程序,在其中显示我的位置并每次更新此位置并将此位置设置在视图中心。我更新我的位置并使用布尔变量设置在中心。我想在地图上检测滚动或缩放触摸手指,当我这样做时,我的布尔变量发生了变化,并且没有将我的位置设置在中心,但我不知道如何处理 Mapkit 上的触摸事件。
请指导我,并告诉我如何在触摸和滚动地图时更改变量。谢谢。这是我的代码:
@implementation ViewController
{
CLLocationManager *locationManager;
double latitudes;
double longitudes;
BOOL updateLocation;
}
@synthesize mapView;
- (void)viewDidLoad
{
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
updateLocation = YES;
mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
mapView.mapType = MKMapTypeStandard;
mapView.zoomEnabled = YES;
mapView.scrollEnabled = YES;
mapView.showsUserLocation = YES;
[mapView.userLocation setTitle:@"I'm Here"];
[self.view addSubview:mapView];
mapView.delegate = self;
[self GetMyLocation];
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
NSLog(@"didFailWithError: %@", error);
UIAlertView *errorAlert = [[UIAlertView alloc] …Run Code Online (Sandbox Code Playgroud) 我有下面的快速代码,它绘制一个多边形并在 MKMapView 上添加注释。我想弄清楚如何识别注释的坐标是否在多边形内?
import UIKit
import MapKit
class ViewController: UIViewController {
@IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
let initialLocation = CLLocation(latitude: 49.140838, longitude: -123.127886)
centerMapOnLocation(initialLocation)
addBoundry()
var annotation = MKPointAnnotation()
annotation.coordinate = point1
annotation.title = "Roatan"
annotation.subtitle = "Honduras"
mapView.addAnnotation(annotation)
}
var points = [CLLocationCoordinate2DMake(49.142677, -123.135139),
CLLocationCoordinate2DMake(49.142730, -123.125794),
CLLocationCoordinate2DMake(49.140874, -123.125805),
CLLocationCoordinate2DMake(49.140885, -123.135214)]
var point1 = CLLocationCoordinate2DMake(49.141821, -123.131577)
func addBoundry() {
let polygon = MKPolygon(coordinates: &points, count: points.count)
mapView.addOverlay(polygon)
}
let regionRadius: CLLocationDistance = 1000
func centerMapOnLocation(location: …Run Code Online (Sandbox Code Playgroud) 我对 swift 很陌生(来自 python),我正在努力创建 MKAnnotationView。我遵循了 ray wenderlich 的教程,但代码似乎已经过时了。该函数调用似乎不起作用,也无法产生引脚注释中应有的“i”。这是我当前使用的代码:
import MapKit
extension ViewController: MKMapViewDelegate {
// 1
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
if let annotation = annotation as? Artwork {
let identifier = "pin"
var view: MKPinAnnotationView
if let dequeuedView = MapView.dequeueReusableAnnotationView(withIdentifier: identifier)
as? MKPinAnnotationView { // 2
dequeuedView.annotation = annotation
view = dequeuedView
} else {
// 3
view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
view.canShowCallout = true
view.calloutOffset = CGPoint(x: -5, y: 5)
let button = …Run Code Online (Sandbox Code Playgroud) 我正在尝试在完整的地图空间上添加 ovarlay。我尝试使用
@available(iOS 4.0, *)public let MKMapRectWorld: MKMapRect
但找不到正确的方法。
我正在寻找一个简单的导航应用程序,它总是根据用户的方向旋转。我已经看到了一些 Android 的答案,但还没有找到任何适用于 Swift 的东西。
我尝试了以下方法,但它对我不起作用,并且不确定这是否让我更接近我正在寻找的东西。
let newCam = MGLMapCamera()
newCam.heading = 90
mapView.setCamera(newCam, animated: true)
Run Code Online (Sandbox Code Playgroud)
我对 Xcode 和 Swift 还很陌生,理解起来有困难,所以请放轻松!
我有一个数组[CLLocationCoordinate2D],我想在MKMapView所有这些坐标周围画一个圆圈。
我已经成功地围绕一个圆圈画了一个圆,CLLocationCoordinate2D如下所示:
let coordinate = CLLocationCoordinate2D(latitude: 53, longitude: 27)
self.mapView.add(MKCircle(center: coordinate, radius: 100))
extension MapViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
guard overlay is MKCircle else { return MKOverlayRenderer() }
let circle = MKCircleRenderer(overlay: overlay)
circle.strokeColor = UIColor.red
circle.fillColor = UIColor(red: 255, green: 0, blue: 0, alpha: 0.1)
circle.lineWidth = 1
return circle
}
}
Run Code Online (Sandbox Code Playgroud)
如何绘制一个包围/包含所有坐标的圆?,如下所示:
我正在尝试集成需要 URL 中的签名查询参数的 Apple Map Web Snapshot。我能够在 NPM 的 JWA 包中成功生成和验证 ES256 签名,但不能在 Java 中生成和验证。请帮助我找到等效的库以生成有效的签名,我在 Java 中尝试了几个 JWA 库。
// Required modules.
const { readFileSync } = require("fs");
const { sign } = require("jwa")("ES256");
/* Read your private key from the file system. (Never add your private key
* in code or in source control. Always keep it secure.)
*/
const privateKey = readFileSync("[file_system_path]");
// Replace the team ID and key ID values with your actual values.
const teamId = …Run Code Online (Sandbox Code Playgroud) 我正在使用 Xcode 12。
我能够显示带有区域的地图,但带有硬编码值。
相反,我想根据用户当前位置设置地图区域。
我有一个 LocationManager 类,它获取用户的位置并发布它。
我有一个 ShowMapView SwiftUI 视图,它观察基于 LocationManager 类的对象以获取用户的位置。但是,我不知道如何使用 locationManager 对象中的数据来设置地图使用的区域。
这是 LocationManager 类,它获取用户的位置并发布它。
import Foundation
import MapKit
final class LocationManager: NSObject, ObservableObject {
@Published var location: CLLocation?
private let locationManager = CLLocationManager()
override init() {
super.init()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.distanceFilter = kCLDistanceFilterNone
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
}
}
extension LocationManager: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.last {
self.location = location
}
}
}
Run Code Online (Sandbox Code Playgroud)
这里是 …