我已经实现了一个从MKAnnotation派生的自定义注释,称为ContainerAnnotation,以及一个从MKAnnotationView派生的自定义注释视图和一个名为ContainerAnnotationView的drawRect:方法.由于某种原因,drawRect:方法没有被调用,我无法弄清楚为什么.
这是我的注释视图的源代码.
ContainerAnnotationView.h:
@interface ContainerAnnotationView : MKAnnotationView
{
}
@end
Run Code Online (Sandbox Code Playgroud)
ContainerAnnotationView.m:
@implementation ContainerAnnotationView
- (void) drawRect: (CGRect) rect
{
// Draw the background image.
UIImage * backgroundImage = [UIImage imageNamed: @"container_flag_large.png"];
CGRect annotationRectangle = CGRectMake(0.0f, 0.0f, backgroundImage.size.width, backgroundImage.size.height);
[backgroundImage drawInRect: annotationRectangle];
// Draw the number of annotations.
[[UIColor whiteColor] set];
UIFont * font = [UIFont systemFontOfSize: [UIFont smallSystemFontSize]];
CGPoint point = CGPointMake(2, 1);
ContainerAnnotation * containerAnnotation = (ContainerAnnotation *) [self annotation];
NSString * text = [NSString stringWithFormat: @"%d", containerAnnotation.annotations.count];
[text …Run Code Online (Sandbox Code Playgroud) 我有一个可以快速更新的注释数据集.目前我删除所有注释,然后将它们重新绘制回地图上.
NSArray *existingpoints = [mapView.annotations filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"!(self isKindOfClass: %@)", [MKUserLocation class]]];
[mapView removeAnnotations:existingpoints];
Run Code Online (Sandbox Code Playgroud)
我在自定义对象中计算它们的位置,因此希望能够调用它并"移动"注释而不删除并将其重新添加回地图.我制作的示例调用工作,我想几乎"民意调查"在下面.
- (CLLocationCoordinate2D) coordinate
{
CLLocationCoordinate2D coord;
coord.latitude = [lat doubleValue];
coord.longitude = [lon doubleValue];
double differencetime = exampleTime;
double speedmoving;
double distanceTravelled = speedmoving * differencetime;
CLLocationDistance movedDistance = distanceTravelled;
double radiansHeaded = DEG2RAD([self.heading doubleValue]);
CLLocation *newLocation = [passedLocation newLoc:movedDistance along:radiansHeaded];
coord = newLocation.coordinate;
return coord;
}
Run Code Online (Sandbox Code Playgroud)
根据要求,Object的.h文件,我没有SetCoordinate方法..
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
@interface TestObject : NSObject <MKAnnotation>{
NSString *adshex;
NSString *lat;
NSString *lon;
NSString …Run Code Online (Sandbox Code Playgroud) 一旦点击,我想使用注释.我查了一下Apple的文档并且用谷歌搜索了但我无法找到为什么这与应该如何完成有什么不同.基本上; 我没有得到println("Pin clicked");
为什么不?
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView!
{
if !(annotation is MKPointAnnotation) {
return nil
}
let reuseId = "test"
var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
if anView == nil {
anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
anView.canShowCallout = true
}
else {
anView.annotation = annotation
}
return anView
}
func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!)
{
println("Pin clicked");
}
func setAnnotationPinOnMap(annotation : NSManagedObject)
{
var subject: AnyObject? = annotation.valueForKey("subject")
var desc: AnyObject? = annotation.valueForKey("desc")
var …Run Code Online (Sandbox Code Playgroud) 我正在MKAnnotationView我的项目中创建一个子类.它需要有两个属性来存储子视图,我需要在开头的某个地方进行初始化.
MKAnnotationView在其文档中列出了一个初始化程序initWithAnnotation:reuseIdentifier:,所以我想我只是覆盖它:
class PulsatingDotMarker: MKAnnotationView {
let innerCircle: UIView
let outerCircle: UIView
override init!(annotation: MKAnnotation!, reuseIdentifier: String!) {
innerCircle = ...
outerCircle = ...
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
}
...
}
Run Code Online (Sandbox Code Playgroud)
但这会导致运行时异常:
致命错误:对'PulsatingDotMarker'类使用未实现的初始化程序'init(frame :)'
好的,所以我猜initWithAnnotation:reuseIdentifier:内部调用initWithFrame:,所以可能是我应该覆盖的那个.我们试试看:
class PulsatingDotMarker: MKAnnotationView {
let innerCircle: UIView
let outerCircle: UIView
override init(frame: CGRect) {
innerCircle = ...
outerCircle = ...
super.init(frame: frame)
}
...
}
Run Code Online (Sandbox Code Playgroud)
但是,这会在创建注释视图对象时导致编译错误:
调用中的额外参数'reuseIdentifier'
嗯,所以如果我实现(必需的)初始化器initWithFrame:,它现在会丢失默认的初始化器initWithAnnotation:reuseIdentifier:?
也许如果我添加一个覆盖initWithAnnotation:reuseIdentifier:,只是调用super …
我创建了一个注释,我将其添加到MKMapView中.如何使用自定义图像而不是标准红色针?
@interface AddressAnnotation : NSObject<MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
MKPinAnnotationColor pinColor;
}
@property (nonatomic,retain) NSString *title;
@property (nonatomic,retain) NSString *subtitle;
@property (nonatomic, assign) MKPinAnnotationColor pinColor;
@end
Run Code Online (Sandbox Code Playgroud) 我有一个用于显示地图和自定义注释的应用程序.当用户触摸注释时,我的应用程序崩溃.它仅在iOS 7中发生.它适用于iOS 6和iOS 5.
以下是控制台中显示的崩溃报告:
ERROR: Trying to select an annotation which has not been added
-[__NSSetM coordinate]: unrecognized selector sent to instance 0x18c9d580
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSSetM coordinate]: unrecognized selector sent to instance 0x18c9d580'
*** First throw call stack:
(
0 CoreFoundation 0x020415e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x016918b6 objc_exception_throw + 44
2 CoreFoundation 0x020de903 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
3 CoreFoundation 0x0203190b ___forwarding___ + 1019
4 CoreFoundation 0x0206e46e __forwarding_prep_1___ + 14
5 MapKit …Run Code Online (Sandbox Code Playgroud) 我在iOS 7上,我有以下问题.经过一些奇怪的研究后,我找不到线索......
我有一个左右配件的地图视图注释,但在中间是透明的(我认为默认的iOS 7行为).
如何将默认的半透明/透明背景更改为我想要的任何其他颜色?
我知道这个问题已被多次询问,但所有答案似乎与我的应用程序中发生的情况略有不同.
我的理解是,一旦mapView将其委托设置为显示它的ViewController,就会调用viewForAnnotation函数.当mapView滚动时,会在地图中添加注释,以便在mapView区域内显示.
目前我有一个主viewController(mainVC),它包含一个MKMapView(mapView)这个viewController控制四个不同的地图在mapView中显示.
func moveViews(sender:Int) {
// This function handles which button on the Segmented Control was clicked and the loads the appropriate map into the mapView (passed as a para
removeAnnotationsAndOverlays() // ~~~
if sender == 0 {
// First Map was selected
let map1VC = map1VC()
map1VC.loadMap1View(mapView)
map1VC.centerMapOnLocation()
}
else if sender == 1 {
// Second Map was selected
let map2VC = map2VC()
map2VC.loadMap2View(mapView)
}
else if sender == 2 {
// Third Map was …Run Code Online (Sandbox Code Playgroud) 任何有关以下问题的建议都将受到赞赏.
我正在使用以下代码将自定义图像添加到注释中.
- (MKAnnotationView *)mapView:(MKMapView *)mapView
viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isMemberOfClass:[MKUserLocation class]])
{
return nil;
}
if ([annotation isMemberOfClass:[SpectatorPin class]])
{
SpectatorPin *sp = (SpectatorPin *)annotation;
MKAnnotationView *view = [self.mapView dequeueReusableAnnotationViewWithIdentifier:@"specPin"];
if (view == nil) {
view = [[[MKPinAnnotationView alloc] initWithAnnotation:sp reuseIdentifier:@"specPin"] autorelease];
}
view.image = [UIImage imageNamed:@"mylocation20x20.png"];
view.canShowCallout = YES;
view.annotation=annotation;
return view;
}
//Should not get here
return nil;
}
Run Code Online (Sandbox Code Playgroud)
最初正确显示图像.
我有一个段控件,可以更改地图类型(标准,卫星,混合).标准是默认值.一旦我选择卫星,图像立即变为一个引脚.不再调用mapView:viewforAnnotation方法.
问候,
吉姆
我CABasicAnimation在AnnotationView图层中添加了一个模拟在mapview上移动的汽车.
这工作正常,直到我在动画进行过程中尝试放大或缩小mapview.
我发现在缩放mapview时动画注释视图会消失!!
我想这可能是由于在缩放mapview时删除了关联动画对象的图层.
我尝试通过缩放时停止动画来解决这个问题.但结果并不好.汽车似乎跳到了目标点.
有人有这方面的想法吗?
任何人都知道在缩放mapview时如何让动画仍然运行?
mkannotationview ×10
ios ×6
mkmapview ×4
mapkit ×3
iphone ×2
mkannotation ×2
swift ×2
animation ×1
annotations ×1
drawrect ×1
initializer ×1
ios7 ×1
objective-c ×1
swift2 ×1
uiimage ×1