我有一个具有 SwiftUI 生命周期的 SwiftUI 应用程序,我正在尝试让点击手势在 MKMapView 中工作。我似乎无法正确获取地图的参考。在本小节中我想做的就是点击地图并添加注释。然后我也会使用注释的坐标。
这是代码。
struct MapView: UIViewRepresentable {
typealias UIViewType = MKMapView
@State private var myMapView: MKMapView?
class Coordinator: NSObject, MKMapViewDelegate {
var control: MapView
let parent = MapView()
let sfCoord = CLLocationCoordinate2D(latitude: 37.7749, longitude: -122.4194)
init(_ control: MapView) {
self.control = control
}
func mapView(_ mapView: MKMapView, didAdd views: [MKAnnotationView]) {
if let annotationView = views.first {
if let annotation = annotationView.annotation {
if annotation is MKUserLocation {
let region = MKCoordinateRegion(center: annotation.coordinate, latitudinalMeters: 2000, …Run Code Online (Sandbox Code Playgroud) 我在使用MKMapView/annotations/UINavigationController时遇到了一些问题.基本上,我使用UINavigationController管理我的视图; 我的一个视图包含一个MKMapView,我使用addAnnotations方法在其上添加注释(10到200).
一切都工作正常,除了一件事:如果我在我的UINavigationController上"太快"导航,iphone模拟器崩溃,接收"EXC BAD ACCESS"信号.例如,如果我加载包含MKMapView的视图,并立即按下UINavigationController导航栏中的"返回"按钮,我会收到信号.我认为问题出在addAnnotations方法上:加载我的MKMapView时,我会向它添加注释,但看起来所有内容都是异步完成的.如果我在按下"返回"按钮之前等待一秒钟,我没有错误,但如果我太快,它会崩溃.如果我删除addAnnotations行,我根本没有错误.我想这是因为我的视图是由UINavigationController在addAnnotations方法完成工作之前发布的.
对此有什么好的解决方案?我不希望用户等待(例如显示加载视图); 我想解决方案可能是一个更好的内存管理,但我不知道我怎么能做到这一点.
if(DEBUG_MODE) { NSLog(@"Creating array of placemarks : begin"); }
self.placemarkCache = [[NSMutableArray alloc] init];
// Loading placemarks for a placemark dictionary
NSArray *sortedKeys = [[self.placemarkDictionary allKeys] sortedArrayUsingSelector:@selector(compare:)];
for (id key in sortedKeys) {
MyPlacemark *currentPlacemark = [self.placemarkDictionary objectForKey:key];
[self.placemarkCache addObject:currentPlacemark];
[currentPlacemark release];
}
if(DEBUG_MODE) { NSLog(@"Creating array of placemarks : done"); }
if(DEBUG_MODE) { NSLog(@"Adding placemarks : begin"); }
[self.mapView addAnnotations:self.placemarkCache];
if(DEBUG_MODE) { NSLog(@"Adding placemarks : done"); }
Run Code Online (Sandbox Code Playgroud)
在这个例子中,我在地图上显示任何内容之前得到"添加地标:完成"消息.
我认为使用我自己的自定义图钉进行注释会非常容易.
但我从来没有能够让它工作,我不明白为什么!
我只是使用:
Annotation *anno = [[[Annotation alloc] init] autorelease];
anno.coordinate = ridesMap.userLocation.location.coordinate;
anno.title = @"Current Location";
anno.subtitle = [NSString stringWithFormat:@"%f, %f", anno.coordinate.latitude, anno.coordinate.longitude];
static NSString *defaultPinID = @"LocationIdentifier";
MKPinAnnotationView *pinView = (MKPinAnnotationView *)[ridesMap dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if (pinView == nil){
pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:anno reuseIdentifier:defaultPinID] autorelease];
}
pinView.image = [UIImage imageNamed:@"custom_pin.png"];
pinView.opaque = NO;
pinView.canShowCallout = YES;
pinView.draggable = NO;
pinView.annotation = anno;
NSLog(@"Adding current location annotation");
return pinView;
Run Code Online (Sandbox Code Playgroud)
我认为这应该工作,因为UIImage是它想要的,我在我的项目中有custom_pin.png文件.
它从不使用我的图像,只是标准的红色图钉.我在这做错了什么?
想要解决我面临的mapkit问题.应该是一个愚蠢的问题,或者我在浏览mapkit框架时错过了一些东西.
这是Senario.当用户执行像披萨这样的搜索时,我在地图上放置了多个注释.添加了右侧注释视图的按钮,单击该按钮可打开下一个详细视图.问题是如何将一些信息发送到下一个视图,例如我在创建注释时添加索引,现在我想从注释中访问此信息,并通过按钮上的选择器将其传递给下一个视图.
我已经检查了所有mapkit精致,但没有找到一个我可以用下一个视图和注释映射此信息.
希望我没有在你的问题中困惑你们.请让我知道我会重新构建它.
提前解冻.
我对iphone开发很新,并且在工作中被问及应用程序的可行性,该应用程序涉及在Google地图上绘制自定义路线(我可以通过网页管理这个),并且应用程序本身会向用户显示他们可以使用GPS风格(转弯)导航屏幕导航的路线.
路线将具有触发特定信息的标记,例如地标等.再次,我可以通过网页上的Google地图配置这些标记.
问题是你将如何处理这种应用程序?从头开始或在任何现有平台上构建它?任何提示将不胜感激,因为我不知道从哪里开始.
我有一个只有MKAnnotation的mapView,它有一个服装图像.当用户更改mapType时,我需要更改该注释的图像.
我这样做的方法是从地图中删除注释,并插入另一个正确的图像,我不认为是最好的方法.显示新图像大约需要1 2秒.
如何在不删除注释的情况下执行此操作并删除另一个注释?
谢谢,
RL
我想让"使用当前位置"警报继续提示用户是否说"不".
目前在我正在开发的应用程序中,如果用户说"不",则提示永远不再显示.我想知道如果他们返回到视图控制器,如何保持提示/警报活着.
谢谢你的帮助
我在这里有一个地图的基本设置,我试图让引脚动画一滴......就像你在地图应用程序中按住并按住引脚一样.因此,当我进入视图时,所有引脚都会动画到它们的位置,我真的不知道该怎么做.如果有人可以引导我使用正确的代码,我需要添加,这将是伟大的!
这是我现有的代码:
class FirstViewController: UIViewController,MKMapViewDelegate, CLLocationManagerDelegate{
@IBOutlet weak var mapView: MKMapView!
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
self.mapView.showsUserLocation = true
let LitzmanLocation = CLLocationCoordinate2DMake(32.100668,34.775192)
// Drop a pin
let Litzman = MKPointAnnotation()
Litzman.coordinate = LitzmanLocation
Litzman.title = "Litzman Bar"
Litzman.subtitle = "Nemal Tel Aviv St'"
mapView.addAnnotation(Litzman)}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
//Dispose of resources that can be re created.
}
//Mark:
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
let …Run Code Online (Sandbox Code Playgroud) 我不希望用户缩小更多.有没有办法在Map kit使用swift时设置最大缩放级别?
mapkit ×10
iphone ×6
ios ×5
mkmapview ×4
objective-c ×3
annotations ×1
cocoa-touch ×1
gps ×1
ios9 ×1
mkannotation ×1
navigation ×1
swift ×1
swift3 ×1
swiftui ×1
uiimage ×1
xcode ×1