在"地图"应用程序中的iPhone 3GS上,您可以单击通常显示您的位置两次的图标,蓝点获得看起来像前照灯的光束,基本上显示您在地图上面向的方向并旋转图像因此.
使用MapKit MapView可以使用此选项吗?
我知道我可以用类似的东西来获得我的标题
- (void)locationManager:(CLLocationManager *) manager didUpdateHeading:(CLHeading *) newHeading {
Run Code Online (Sandbox Code Playgroud)
//如果准确性有效,请处理事件.
if (newHeading.headingAccuracy > 0) {
CLLocationDirection theHeading = newHeading.magneticHeading;
...
}
Run Code Online (Sandbox Code Playgroud)
}
但我不知道如何在Mapkit中获得那么好的前照灯效果,似乎没有任何文档.
有任何想法吗?
常规注释引脚的原点位于底部中间,因此引脚始终指向同一位置.
但是当我添加自定义图像时,它的原点是图像的中心,所以每次放大或缩小,我的图像底部指向不同的位置.

在这里我的别针应该指向巴黎的中心但是

但是当我放大时,我的图钉的底部并没有指向巴黎的中心.
我正在尝试CGRect.origin但没有得到任何有用的东西.
这是我的代码:
- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
MKAnnotationView * customPinView = [[MKAnnotationView alloc] init];
UIImage * img = [UIImage imageNamed:@"waterPin.png"] ;
CGRect resizeRect;
resizeRect.size.height = 40;
resizeRect.size.width = 40;
resizeRect.origin = (CGPoint){0.0f, 0.0f};
UIGraphicsBeginImageContext(resizeRect.size);
[img drawInRect:resizeRect];
UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
customPinView.image = resizedImage;
return customPinView;
}
Run Code Online (Sandbox Code Playgroud) 我想通过添加另一个String属性来子类化MKCircle(例如MyCircle),让我们称之为" code".这个属性不是可选的和常量的,所以我必须从初始化器设置它,对吧?当然MyCircle还应该得到中心坐标和半径.这两个属性是只读的,所以我还需要通过初始化程序设置它们.
最后,我需要一个初始化的是需要3个参数:coordinate,radius和code.听起来很简单但Swifts指定和便利的initalizers及其规则让我很难在这里.
问题是MKCircle:
class MKCircle : MKShape, MKOverlay, MKAnnotation, NSObjectProtocol {
convenience init(centerCoordinate coord: CLLocationCoordinate2D, radius: CLLocationDistance)
convenience init(mapRect: MKMapRect) // radius will be determined from MAX(width, height)
var coordinate: CLLocationCoordinate2D { get }
var radius: CLLocationDistance { get }
var boundingMapRect: MKMapRect { get }
}
Run Code Online (Sandbox Code Playgroud)
正如你所看到的初始化的MKCircle那个需要coordinate和radius是一个方便initalizer,因此没有从我的子类的初始化调用.此属性也是只读的,因此我无法从子类的初始化程序或外部设置它们.
我尝试了很多变化,但似乎唯一可行的方法是使我的code属性成为可选,使用继承的便利初始化器来设置坐标和半径,然后设置代码属性,如下所示:
class MyCircle: MKCircle {
var code: String? …Run Code Online (Sandbox Code Playgroud) 我想知道是否有人可以告诉我如何触摸map到形式的引脚MKPointAnnotations.
我想点击pin上,map然后通过带回我预设variables的pin那个去另一个视图.
任何人都能解释我这件事Swift吗?
谢谢
使用代码编辑:
class ViewController: UIViewController, MKMapViewDelegate {
@IBOutlet weak var mappa: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
var location : CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 44.648590, longitude: 10.918794)
let pinAnnotation = PinAnnotation()
pinAnnotation.setCoordinate(location)
self.mappa.addAnnotation(pinAnnotation)
}
class PinAnnotation : NSObject, MKAnnotation {
private var coord: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 0, longitude: 0)
var coordinate: CLLocationCoordinate2D {
get {
return coord
}
}
var title: String = "test" …Run Code Online (Sandbox Code Playgroud) 我想显示用户位置和周围区域,但我也希望允许用户在该区域内平移.现在,如果我尝试在地图上的其他位置滚动,它会自动将我带回到基本区域,用户位于中心.我怎么阻止这个?我想在中心显示用户的初始视图,但我希望能够滚动.在此先感谢您们的帮助!
导入UIKit导入MapKit导入CoreLocation
class ViewControllerMain:UIViewController,MKMapViewDelegate,CLLocationManagerDelegate {
@IBOutlet weak var mapView: MKMapView!
var locationManager:CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
locationManager.requestWhenInUseAuthorization()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.delegate = self
locationManager.startUpdatingLocation()
mapView.showsUserLocation = true
mapView.delegate = self
let longPress = UILongPressGestureRecognizer(target: self, action: "action:")
longPress.minimumPressDuration = 1.0
mapView.addGestureRecognizer(longPress)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
let regionToZoom = MKCoordinateRegionMake(manager.location.coordinate, MKCoordinateSpanMake(0.01, 0.01))
mapView.setRegion(regionToZoom, animated: true)
}
Run Code Online (Sandbox Code Playgroud) 我在Xcode 7 beta 4和iOS 9模拟器上看到了一个奇怪的问题.尝试加载包含地图的视图时,任何使用地图的应用都会挂起.它没有抛出异常.如果我删除地图,它的工作原理.即使在一个非常简单的项目中,这似乎也有100%的重复.
当存在地图时,我看到以下日志输出:
/BuildRoot/Library/Caches/com.apple.xbs/Sources/VectorKit_Sim/VectorKit-1134.11/GeoCSS/GeoCSS/StyleSheet.cpp:122 STYL Parse:这里解码错误/BuildRoot/Library/Caches/com.apple.xbs/Sources /VectorKit_Sim/VectorKit-1134.11/GeoCSS/GeoCSS/StyleSheet.cpp:99 STYL Parse:此处解码错误
具有相同设置的同事对同一项目没有任何问题.
我在我的 iOS 项目中使用 MapKit 有一个自定义的 MKTileOverlay,它大部分时间都可以正常工作。但是,在放大/缩小几次并平移地图后,一些图块没有被绘制。
起初我认为这是一个没有加载瓷砖的简单情况,所以我对 MKTileOverlay 进行了子类化并在控制台中添加了日志记录。它表明所有瓷砖都被完美地加载并交付到结果块。
由于我的想法不多了,我创建了一个本地图块生成器,它只返回带有 x/y/z 路径和绘制的框架的图像,以查看缺少哪些图块。
不幸的是,即使使用本地生成的图块,问题仍然存在,因此它与 Internet 连接无关。另一个奇怪的行为是,如果我有两个自定义叠加层,它们将是完全相同的图块,但不会在两个叠加层上呈现。
我现在能想到的唯一解决方案是将 tile 渲染器子类化并确保显示所有内容,因为无法知道 tile 未呈现。然而,这听起来像是很多工作和“重新发明轮子”的任务......
我正在尝试加载地图并在用户位置上显示初始位置,并使用 SwiftUI 在地图上显示用户的位置。我不知道如何在 SwiftUI 中做到这一点。
我试图将 'view.showsUserLocation = true' 放在 updateUIView 函数中,但它不起作用。
import SwiftUI
import MapKit
struct MapView : UIViewRepresentable {
func makeUIView(context: Context) -> MKMapView {
MKMapView(frame: .zero)
}
func updateUIView(_ view: MKMapView, context: Context) {
view.showsUserLocation = true
let coordinate = CLLocationCoordinate2D(
latitude: 34.011286, longitude: -116.166868)
let span = MKCoordinateSpan(latitudeDelta: 2.0, longitudeDelta: 2.0)
let region = MKCoordinateRegion(center: view.userLocation.location?.coordinate ?? coordinate, span: span)
view.setRegion(region, animated: true)
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试打印它时,该位置为零。
我MapKit用来显示两个注释和它们之间的距离。一切正常,但我一直在控制台上收到此错误。
MobileGestalt.c:1647:无法检索区域信息
有人知道是什么原因造成的吗?可能是.plist文件中的某些权限
这是完整的控制台输出:
2019-11-10 00:21:41.419108+0100 Post DACH[20420:264466] Metal API Validation Enabled
2019-11-10 00:21:41.567416+0100 Post DACH[20420:264466] libMobileGestalt MobileGestalt.c:1647: Could not retrieve region info
我的代码:
struct MapView: UIViewRepresentable {
var requestLocation: CLLocationCoordinate2D
var destinationLocation: CLLocationCoordinate2D
private let mapView = WrappableMapView()
func makeUIView(context: UIViewRepresentableContext<MapView>) -> MKMapView {
mapView.delegate = mapView
return mapView
}
func updateUIView(_ uiView: MKMapView, context: UIViewRepresentableContext<MapView>) {
let requestAnnotation = MKPointAnnotation()
requestAnnotation.coordinate = requestLocation
requestAnnotation.title = "Package"
uiView.addAnnotation(requestAnnotation)
let destinationAnnotation = MKPointAnnotation()
destinationAnnotation.coordinate = destinationLocation …Run Code Online (Sandbox Code Playgroud) 在 iOS 模拟器中,当运行我的 mapKit 视图时,调试器会因一长串更新而发疯,地图永远不会加载,但 GPS 会加载。
这似乎是在最近使用 Firebase 和 Facebook 安装可可豆荚后开始的。
最初,模拟器中根本没有加载地图。我终于OS_ACTIVITY_MODE = disable按照这个解决方案/sf/answers/2762287951/ 进行了设置,但现在我有了这个不断更新请求的当前错误日志。
我升级到 Xcode 11.2 版,但问题仍然存在。
这是什么问题,我该如何解决?
我的代码
ViewController.swift
import UIKit
import MapKit
class ViewController: UIViewController {
private let locationManager = CLLocationManager()
private var currentCoordinate: CLLocationCoordinate2D?
@IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
configureLocationServices()
// Do any additional setup after loading the view.
}
private func configureLocationServices() {
locationManager.delegate = self
let status = CLLocationManager.authorizationStatus()
if …Run Code Online (Sandbox Code Playgroud) mapkit ×10
ios ×5
swift ×5
swiftui ×2
inheritance ×1
initializer ×1
ios13.2 ×1
iphone ×1
view ×1
xcode ×1
xcode11.2 ×1
xcode7 ×1
xcode7-beta4 ×1