现在我想在我的iOS应用程序中在地图上添加静态引脚注释.
但我只是想知道是否会调用委托方法mapView:viewForAnnotation :.
在Apple文档中,据说
When it needs an annotation view, the map view calls the mapView:viewForAnnotation: method of its delegate object.
Run Code Online (Sandbox Code Playgroud)
我从互联网和Apple的官方文档中读过几个教程.当这个方法被调用时我仍然不会.
我正试图在MKMapView上捕捉平移和"滚动结束".使用手势识别器可以轻松进行平移.但是,MKMapView似乎没有在iOS 6中实现UIScrollViewDelegate.这使得解决方案中有没有办法限制MKMapView的最大缩放级别?不行.
思考?理想情况下,我只是利用UIScrollViewDelegate:
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
if ([super respondsToSelector:@selector(scrollViewDidEndDecelerating:)]) {
[super scrollViewDidEndDecelerating:scrollView];
}
[self.myDelegate mapDidFinishPanning:self];
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate: (BOOL)decelerate {
if ([super respondsToSelector:@selector(scrollViewDidEndDragging:)]) {
[super scrollViewDidEndDragging:scrollView];
}
if(!decelerate) {
[self.myDelegate mapDidFinishPanning:self];
}
Run Code Online (Sandbox Code Playgroud)
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
if ([super respondsToSelector:@selector(scrollViewWillBeginDragging:)]) {
[super scrollViewWillBeginDragging:scrollView];
}
[self.myDelegate mapDidBeginPanning:self];
}
Run Code Online (Sandbox Code Playgroud)
在扩展MKMapView的类中
@interface MyMapView : MKMapView <UIScrollViewDelegate, UIGestureRecognizerDelegate>
Run Code Online (Sandbox Code Playgroud)
但这在iOS 6中不起作用.我在MKMapViewDelegate中看不到任何足够的东西.
我正在尝试将我的一个应用程序更新到iOS7.问题是,在我MKMapView,当我点击一个引脚时,它显示了Callout,但是当点击rightCalloutAccessoryView它时,它不再向委托发送任何回调.因此,我无法推动细节视图.
它在iOS6上运行良好,在iOS7上不再适用.
这是相关的代码:
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
if ([annotation isKindOfClass:[MKUserLocation class]]) {
return nil;
}
NSString * annotationIdentifier = nil;
if ([annotation isKindOfClass:VPStation.class]) {
annotationIdentifier = @"stationAnnotationIdentifier";
}
if (!annotation) {
return nil;
}
MKPinAnnotationView * annotationView = [(MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier] retain];
if(annotationView == nil) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationIdentifier];
annotationView.canShowCallout = YES;
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
annotationView.image = [UIImage imageNamed:@"MyAnnotationPin"];
annotationView.centerOffset = CGPointMake(-10.0f, 0.0f);
}
annotationView.annotation = …Run Code Online (Sandbox Code Playgroud) 我正在使用填充了自定义引脚的地图视图.当用户点击地图上的某个地方取消选择一个引脚时,我想实现地图,这样引脚就不会被取消选择(即用户无法在不选择其他引脚的情况下取消选择引脚,因此至少会选择一个引脚).这是我对didDeselectAnnotationView方法的实现:
-(void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
{
[mapView selectAnnotation:view.annotation animated:NO];
}
Run Code Online (Sandbox Code Playgroud)
基本上,我正在尝试重新选择注释.但是,经过一些调试和打印到控制台后,我意识到注释视图实际上并没有被取消选择,直到方法didDeselectAnnotationView:完成运行(即事件的顺序为:用户点击地图上的某个地方,didDeselectAnnotationView:被调用并完成执行,实际取消选择注释视图).有没有其他人遇到过这个问题,或者是否有人知道另一种强制执行地图行为的方法,这样用户无法在不选择其他引脚的情况下取消选择引脚,以便始终选择一个引脚?
谢谢您的帮助.
annotations mkmapview mkannotationview ios mkmapviewdelegate
幸运的是,一个标准的标注视图MKAnnotationView满足我们的需要- ,title,subtitle,leftCalloutAccessoryView和rightCalloutAccessoryView.
不幸的是,我们在应用程序中使用自定义字体,并希望将这些自定义字体扩展到这些标注.
MKAnnotationView没有提供完成此任务的标准方法.
如何在MKAnnotation标注视图中使用自定义字体?
objective-c mkmapview mkannotationview ios mkmapviewdelegate
在显示PINS的实施类中,我保留了两个变量(标题和子标题),在这个例子中,USA当我点击PIN时只显示单词(标题).
CLLocationCoordinate2D location2D = (CLLocationCoordinate2D){ .latitude = latitudeOfUserLocation, .longitude = longitudeOfUserLocation };
ManageAnnotations *annotation=[[ManageAnnotations alloc]initWithTitle:@"USA" adresseDuTheme:@"Colorado" coordinate:location2D];//only USA is displayed
annotation.pinColor = MKPinAnnotationColorRed; //or red or whatever
[self->mapView addAnnotation:annotation];
MKCoordinateSpan span={.latitudeDelta=1,.longitudeDelta=0.5};
MKCoordinateRegion region={location2D,span};
[mapView setRegion:region];
Run Code Online (Sandbox Code Playgroud)
虽然,在ManageAnnotations类中,我为标题和副标题保留了两个变量.
@interface ManageAnnotations : NSObject<MKAnnotation>{
NSString *_libelle;
NSString *_adresse;
CLLocationCoordinate2D _coordinate;
}
//
@property(nonatomic,assign)MKPinAnnotationColor pinColor;
@property(copy)NSString *libelle;
@property(copy)NSString *adresse;
@property(nonatomic,readonly)CLLocationCoordinate2D coordinate;
-(id)initWithTitle:(NSString*)libelle adresseDuTheme:(NSString*)adresse coordinate:(CLLocationCoordinate2D)coordinate;
@end
#import "ManageAnnotations.h"
@implementation ManageAnnotations
@synthesize pinColor;
@synthesize libelle=_libelle;
@synthesize adresse=_adresse;
@synthesize coordinate=_coordinate;
-(id)initWithTitle:(NSString*)libelle adresseDuTheme:(NSString*)adresse coordinate:(CLLocationCoordinate2D)coordinate{
if((self=[super init])){
_libelle=[libelle …Run Code Online (Sandbox Code Playgroud) 我有一个使用 MapKit 的应用程序。
我在调整地图大小后计算当前缩放。
通过定义(来自MKGeometry.h文件)
MKZoomScale provides a conversion factor between MKMapPoints and screen points.
When MKZoomScale = 1, 1 screen point = 1 MKMapPoint. When MKZoomScale is
0.5, 1 screen point = 2 MKMapPoints.
Run Code Online (Sandbox Code Playgroud)
所以,我是这样计算的:
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
CGSize screenSize = mapView.bounds.size;
MKMapRect mapRect = mapView.visibleMapRect;
MKZoomScale zoomScale = screenSize.width * screenSize.height / (mapRect.size.width * mapRect.size.height);
}
Run Code Online (Sandbox Code Playgroud)
计算结果zoomScale符合定义(我已经检查过)。
我还在方法中的 mapView 上绘制了一个叠加层
-(void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context
Run Code Online (Sandbox Code Playgroud)
问题是,我的计算结果zoomScale不等于系统传递给该方法的结果。我希望它们相等,因为drawMapRect:在调整大小后立即调用(实际上,调整大小会导致调用此方法)。
这里有什么问题吗?
我也尝试使用
currentZoomScale = …Run Code Online (Sandbox Code Playgroud) 为了实现 mapView 我尝试添加 <#import 和 MKMapViewDelegate 并尝试
这是我的代码:
- (void)viewDidLoad
{
mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height - 90)];
mapView.showsUserLocation = YES;
mapView.mapType = MKMapTypeHybrid;
mapView.delegate = self;
[self.view addSubview:mapView1];
[super viewDidLoad];
}
Run Code Online (Sandbox Code Playgroud)
体系结构 i386 的未定义符号:“_OBJC_CLASS_$_MKMapView”,引用自:maptryViewController.o ld 中的 objc-class-ref:未找到体系结构 i386 clang 的符号:错误:链接器命令失败,退出代码为 1(使用 -v查看调用)
我犯了什么错误>?
当我单击PinAnnotation时,我想打开一个新的ViewController。但是,当我单击时,ViewForannotation不会调用,但是,当我单击“固定”时,它将显示弹出消息。不知道发生了什么。这是我的代码:
import UIKit
import MapKit
class Pin: NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
var key: String
var title: String?
var age: String?
var category: String?
var color: MKPinAnnotationColor = MKPinAnnotationColor.purple
var subtitle: String?
init(key: String, name: String , age: String , category: String , color: MKPinAnnotationColor) {
self.coordinate = CLLocationCoordinate2D(latitude: 0, longitude: 0)
self.key = key
self.title = name
self.color = color
self.subtitle = age
self.category = category
//print("keyy: ", key)
//print("title: ", name)
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的MapViewControllerClass:
import UIKit
import …Run Code Online (Sandbox Code Playgroud) 我有麻烦在MapView上选择自定义注释.我尝试了几种方法,没有成功:
您可以在下面的代码中找到所有这些方法:
override func viewDidLoad() {
super.viewDidLoad()
// some more initialization code
for item in items {
let annotation = IdentifiedMKPointAnnotation() // has only one additional property named "id"
annotation.coordinate = CLLocationCoordinate2D(latitude: item.spots[0].latitude!, longitude: item.spots[0].longitude!)
annotation.id = i
self.mapView.addAnnotation(annotation)
i += 1
}
}
// MARK: - MapViewDelegate
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let item = self.items[(annotation as! IdentifiedMKPointAnnotation).id!]
let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "")
//annotationView.canShowCallout = false
//annotationView.isEnabled = false …Run Code Online (Sandbox Code Playgroud) 如何改变MKMarkerAnnotationView尺寸?
我尝试设置annotationView.bounds.size = CGSize(width: 50, height: 50),但看起来大小没有改变。我还尝试打印视图的大小,看起来默认为 28,28
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard annotation is MKPointAnnotation else { return nil }
let annotationView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: Constant.Indentifier.mapPoint)
annotationView.canShowCallout = true
annotationView.animatesWhenAdded = true
annotationView.glyphImage = UIImage(systemName: "house.fill")
annotationView.glyphTintColor = .systemBlue
annotationView.markerTintColor = .white
print(annotationView.bounds.size) // defaulted to 28,28
annotationView.bounds.size = CGSize(width: 50, height: 50) // Does not change bubble size
return annotationView
}
Run Code Online (Sandbox Code Playgroud) 我的地图上有一个图钉的坐标。我尝试将地图置于该图钉的中心,但我不想将图钉直接放在地图的中间,而是放在屏幕的 1/3 处。我尝试通过以下解决方法来做到这一点:
let coordinateRegion = MKCoordinateRegionMakeWithDistance(coordinatesOfThePin, 500, 500)
mapView.setRegion(coordinateRegion, animated: true)
let frame = mapView.frame
let myPoint = CGPointMake(frame.midX, frame.midY/3)
let myCoordinate = mapView.convertPoint(myPoint, toCoordinateFromView: mapView)
let coordinateRegion2 = MKCoordinateRegionMakeWithDistance(myCoordinate, 500, 500)
mapView.setRegion(coordinateRegion2, animated: true)
Run Code Online (Sandbox Code Playgroud)
但它根据地图的第一个位置显示随机位置。你能帮我重写我的代码,这样我就可以放置一个引脚位置并得到如下结果:
-------------------
| |
| |
| X | <-- my pin in the 1/3 of the visible map area.
| |
| |
| |
| |
| |
| |
| |
| |
-------------------
Run Code Online (Sandbox Code Playgroud) mkmapview ×9
ios ×8
mapkit ×6
swift ×4
annotations ×2
iphone ×2
objective-c ×2
cocoa-touch ×1
ios6 ×1
ios7 ×1
mkannotation ×1
mkoverlay ×1
uiscrollview ×1