我正在添加自定义注释而不是引脚注释.基本上,我在这些注释中绘制一个圆圈并将它们添加到地图中.这些自定义注释会遮挡用户位置视图(蓝点)的问题如何将蓝点带到前面.
- (void)mapView:(MKMapView *)mapView
didUpdateUserLocation:(MKUserLocation *)userLocation
{
UIView *user = [mapView viewForAnnotation:userLocation];
[mapView bringSubviewToFront:user];
}
但是,这对我不起作用.我的自定义注释仍然覆盖了蓝色用户点.任何想法如何让用户位置视图出现在前面?干杯
我正在尝试将引脚颜色从默认红色更改为自定义图像,但无论我尝试的是什么都不起作用.
我已经从这个网站下载了示例代码:
http://icodeblog.com/2009/12/21/introduction-to-mapkit-in-iphone-os-3-0/
代码可以自己运行,但是当我将注释类导入到我的代码中时,它们不起作用,我不知道为什么.我也从其他网站尝试了许多不同的方法,但我甚至无法改变引脚颜色.
我的项目有4个选项卡,MapView位于其中一个选项卡上.当我选择它时,它会解析JSON字符串并将单独的注释添加到地图上.当我点击图钉时,我会显示标题和副标题,但无法更改颜色或图像.
以下是我添加注释的方法 - MapAnnotation遵循MKAnnotation:
MapAnnotation *ann = [[MapAnnotation alloc] initWithCoordinate:newCoord];
ann.title = [locationDictionary objectForKey:@"name"];
ann.subtitle = [locationDictionary objectForKey:@"name"];
[mapView addAnnotation:ann];
[ann release]
Run Code Online (Sandbox Code Playgroud)
以下是我尝试尝试更改颜色的方法 - 我在视图控制器中有MapView.delegate = self:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
MKPinAnnotationView *pin = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:[annotation title]];
if (pin == nil) {
pin = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:[annotation title]] autorelease];
}else {
pin.annotation = annotation;
}
pin.pinColor = MKPinAnnotationColorGreen;
pin.animatesDrop = YES;
pin.canShowCallout = TRUE;
return pin;
Run Code Online (Sandbox Code Playgroud)
}
我得到的注释与标题和副标题一起出现,而不是绿色标记.我使用颜色或图像总是红色的.如果有人能帮助我,那就太好了!
谢谢
编辑:
mapView委托在viewDidLoad方法中指定.我还在地图的某个部分添加了叠加层.这工作正常,我也已经把它拿出来试试了它没有它导致问题,但它仍然没有解决它.
- …Run Code Online (Sandbox Code Playgroud) 当我使用自定义注释时,我无法看到蓝色当前位置点
- (MKAnnotationView *) mapView:(MKMapView *) mapView viewForAnnotation:(id<MKAnnotation> ) annotation {
MKAnnotationView *customAnnotationView=[[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil] autorelease];
UIImage *pinImage = [UIImage imageNamed:@"ann.png"];
[customAnnotationView setImage:pinImage];
customAnnotationView.canShowCallout = YES;
//UIImageView *leftIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ann.png"]];
//customAnnotationView.leftCalloutAccessoryView = leftIconView;
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self action:@selector(showDetails:) forControlEvents:UIControlEventTouchUpInside];
customAnnotationView.rightCalloutAccessoryView = rightButton;
return customAnnotationView;
}
Run Code Online (Sandbox Code Playgroud) 当用户引脚添加更改其标题和副标题时,我会将注释的颜色从红色更改为绿色.
我真的迷路了.我搜索了如何制作自定义注释引脚,好的.当用户触摸引脚时,我发现了该方法的实现,didSelectAnnotationView当我点击注释时它可以工作NSLog(@"Tap") ;,但现在我无法更改触摸的引脚.
非常感谢大家的贡献.
再见
1.)我需要在地图上有一个蓝色的圆圈/位置点,如下所示.

2.)如何在弹出屏幕上显示如下箭头?

我正在学习在我初出茅庐的iOS应用程序中使用MapKit.我正在使用我的一些模型实体作为注释(将<MKAnnotation>协议添加到其头文件中).我还创建自定义MKAnnotationViews并将draggable属性设置为YES.
我的模型对象有一个location属性,它是一个CLLocation*.为了符合<MKAnnotation>协议,我在该对象中添加了以下内容:
- (CLLocationCoordinate2D) coordinate {
return self.location.coordinate;
}
- (void) setCoordinate:(CLLocationCoordinate2D)newCoordinate {
CLLocation* newLocation = [[CLLocation alloc]
initWithCoordinate: newCoordinate
altitude: self.location.altitude
horizontalAccuracy: self.location.horizontalAccuracy
verticalAccuracy: self.location.verticalAccuracy
timestamp: nil];
self.location = newLocation;
}
- (NSString*) title {
return self.name;
}
- (NSString*) subtitle {
return self.serialID;
}
Run Code Online (Sandbox Code Playgroud)
所以,我有4个必需的方法.而且他们非常直截了当.当我阅读苹果文档MKAnnotationView和@draggable财产时,它说如下:
将此属性设置为YES会使用户可以拖动注释.如果是,则关联的注释对象还必须实现setCoordinate:方法.此属性的默认值为NO.
在其他地方,MKAnnotation文档说:
您对此属性的实现必须符合键值观察(KVO).有关如何实现对KVO的支持的更多信息,请参阅键值观察编程指南.
我已经读过那篇(简短的)文件了,我根本不清楚我应该做些什么来实现这一目标,以便coordinate我从我的location财产中得到的是一个适当的财产本身.
但我有理由相信它不能正常工作.当我拖动它时,它会移动,但是当我平移地图时它不再重新定位.
UPDATE
所以我试着玩MKPinAnnotationView股票.为此,我只是注释了我的委托mapView:viewForAnnotation:方法.我发现默认情况下这些都不可拖动.我添加了mapView:didAddAnnotationViews:我的委托以设置 …
我必须在MKMapView上显示MKMapRoute,它必须显示在我的地图上的特定区域,而不是在中心,而是在顶部区域.它必须放在约80个高度的"框架"中.
我想我可以通过使用MKMapView的方法来实现这一点
- (MKMapRect)mapRectThatFits:(MKMapRect)mapRect edgePadding:(UIEdgeInsets)insets
但我面临着一些奇怪的现象.确切地说,如果我使用这段代码:
//obj is the returned mkroute object.
MKRoute *rout = obj;
MKPolyline *line = [rout polyline];
MKMapRect boundingMapRect = [line boundingMapRect];
MKMapRect fittedRect = [self.mapView mapRectThatFits:[line boundingMapRect]
edgePadding:UIEdgeInsetsMake( 100, 0, 0, 0)];
MKCoordinateRegion r = MKCoordinateRegionForMapRect(fittedRect);
[self.mapView setRegion: r animated:YES];
Run Code Online (Sandbox Code Playgroud)
结果在以下屏幕截图中可见:

但是,如果我(0,0,0,0)通过更改前一个代码段的第四行来设置边缘插入:
MKMapRect fittedRect = [self.mapView mapRectThatFits:[line boundingMapRect]
edgePadding:UIEdgeInsetsMake( 100, 0, 0, 0)];
Run Code Online (Sandbox Code Playgroud)
结果是这一个:

您可以清楚地看到,偏移量已从顶部改变了50(而不是100)点.当我使用MKMapRegion的跨度纬度和经度增量时,或者更改设备屏幕时,事情似乎变得更加奇怪.
现在,我的目的只是告诉mapView在一个框架中绘制该路线,说"从顶部64点,高度为80点,宽度为320点",我找不到一个简单的方法来做到这一点而且我害怕我错过了一些东西.我不想通过改变区域的纬度进行游戏,因为内容大小可能会有所不同(可能会显示更小或更大的路线).我错过了什么,这可能是一个简单的方法吗?为什么插图没有正确计算?
下面是我难以理解的代码:
let rectToDisplay = self.treasures.reduce(MKMapRectNull) { //1
(mapRect: MKMapRect, treasure: Treasure) -> MKMapRect in //2
let treasurePointRect = MKMapRect(origin: treasure.location.mapPoint, size: MKMapSize(width: 0, height: 0)) //3
return MKMapRectUnion(mapRect, treasurePointRect)
}
Run Code Online (Sandbox Code Playgroud)
我对reduce功能的理解是:
var people [] // an array of objects
var ageSum = 0
ageSum = people.reduce(0) { $0 + $1.age}
//(0) = initial value
//$0 = running total
//$1 = an object in an array
Run Code Online (Sandbox Code Playgroud)
我对封闭的理解是:
{ (params) -> returnType in
statements
}
Run Code Online (Sandbox Code Playgroud)
我对顶部代码的理解是:
// 1 = reduce函数的初始值设置为(MKMapRectNull)
// …
我的目标是将视图的左上角和右下角转换为纬度/经度坐标.这些lat/lon坐标将用于查询仅存在于视图中的注释位置(不是全部5000+).
我在Stackoverflow上找到了这个Objective-C技巧.但我遇到的问题是它正在从mapView转换0,0(纬度/经度为-180,-180.Aka,南极).
所以代替:
topLeft = mapView.convertPoint(CGPointMake(0, 0), toCoordinateFromView: self.mapView)
Run Code Online (Sandbox Code Playgroud)
我想我可以做到:
topLeft = view.convertPoint(CGPointMake(0, 0), toCoordinateFromView: self.mapView)
Run Code Online (Sandbox Code Playgroud)
但我得到错误:
无法使用类型'(CGPoint,toCoordinateFromView:MKMapView!)的参数列表调用'convertPoint'
我花了一天时间试图解决这个问题,但无济于事,我来找你寻求指导.任何帮助将非常感激.
这是完整的功能:
func findCornerLocations(){
var topLeft = CLLocationCoordinate2D()
let mapView = MKMapView()
topLeft = view.convertPoint(CGPointMake(0, 0), toCoordinateFromView: self.mapView)
print(topLeft.latitude, topLeft.longitude)
}
Run Code Online (Sandbox Code Playgroud) 在文档中它们看起来完全相同,它们都可以采用Coordinate,Title和Subtitle.
mapkit ×10
ios ×6
iphone ×4
mkmapview ×3
objective-c ×3
swift ×2
annotations ×1
gps ×1
ios7 ×1
maps ×1
mkannotation ×1
mkmaprect ×1
properties ×1
reduce ×1