目标是针对存储在结构数组中的某些值自定义图钉颜色。
通过这里的一些帮助,我实现了以下viewForAnnotation委托方法,该方法很好地基于结构数据数组的大小在循环中迭代调用此委托方法。因此,如果我要将所有引脚设置为一种颜色(例如,紫色)(这是下面代码中的注释行),则可以使用。
问题是当我放入一个开关以根据数组中的值设置颜色时,它会通过此代码,但不遵守任何大小写值来将其设置为备用颜色,并且所有内容都变为红色针(看似默认值)。我已经打印出状态并进行调试,以知道它正在进入交换机内部并相应地设置pinColor,但是它们似乎不粘。
func mapView(aMapView: MKMapView!,
viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
let theindex = mystructindex // grab the index from a global to be used below
if annotation is MKUserLocation {
//return nil so map view draws "blue dot" for standard user location
return nil
}
let reuseId = "pin"
var pinView = aMapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
if pinView == nil {
//println("Pinview was nil")
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView!.canShowCallout = true
pinView!.animatesDrop = true
// Preventive if to keep this from being called beyond my arrays index value as the delegate getting called beyond the for loop for some unknown reason
if (theindex < MySupplierData.count) {
// Set the pin color based on the status value in MySupplierData structure array
switch MySupplierData[mystructindex].status {
case 0,1:
println("Case 0 or 1 - setting to Red")
pinView!.pinColor = .Red // Needs help, show red pin
case 2:
println("Case 2 - Setting to Green")
pinView!.pinColor = .Green // Looking Good
case 3:
println("Case 3 - Setting to Purple")
pinView!.pinColor = .Purple // Could use a follow-up
default:
println("Case default - Should Never Happen")
break;
} // end switch
} // end if
// pinView!.pinColor = .Purple // This works fine without the switch and respects any color I set it to.
}
else {
pinView!.annotation = annotation
}
return pinView
}
在ViewController的for循环中,我将其称为如下,但对返回值不做任何事情。
// previous to this I setup some Titles and Subtitle which work fine
self.theMapView.addAnnotation(myAnnotation)
// Call to my mapview
mapView(theMapView, viewForAnnotation: myAnnotation)
我对返回的Pinview不执行任何操作-认为我不需要这样做,但是在使用切换代码时,所有的引脚在此时都被绘制为红色。从根本上说,我在这里一定错过了一些东西。
7-8-14更新以解决Anna的大力帮助/辅导中的修订代码问题。TKS!
几乎可以使用,地图中的所有图钉都有正确的颜色,但即时显示之外的图钉有时是错误的。在此处发布所有涉及的代码,因为它可能对其他人有所帮助,因为这似乎是关于如何在Google
Maps中进行自定义工作的非常普遍的问题。
建议使用一个自定义类将其他变量保存在自定义批注中-在这种情况下,状态值来自我的数据结构MySupplierData。
class CustomMapPinAnnotation : NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
var title: String
var subtitle: String
var status: Int
init(coordinate: CLLocationCoordinate2D, title: String, subtitle: String, status: Int) {
self.coordinate = coordinate
self.title = title
self.subtitle = subtitle
self.status = status
}
}
修改后的mapView-现在利用传递给它的新CustomMapPinAnnotation:
func mapView(aMapView: MKMapView!,
viewForAnnotation annotation: CustomMapPinAnnotation!) -> MKAnnotationView! {
let reuseId = "pin"
var pinView = aMapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
if pinView == nil {
//println("Pinview was nil")
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView!.canShowCallout = true
pinView!.animatesDrop = true
// Code to catch my custom CustomMapPinAnnotation so we can check the status and set the color
if annotation.isKindOfClass(CustomMapPinAnnotation)
{
println("FOUND OUR CustomMapPinAnnotation CLASS IN mapView")
println(" Custom Title = \(annotation.title)")
println(" Custom status passed = \(annotation.status)")
switch annotation.status {
case 0,1:
println("Case 0 or 1 - Setting to Red")
pinView!.pinColor = .Red
case 2:
println("Case 2 - Setting to Green")
pinView!.pinColor = .Green
case 3:
println("Case 3 - Setting to Purple")
pinView!.pinColor = .Purple
default:
println("Case default - Should Never Happen")
break;
} // switch
} // if
}
else {
pinView!.annotation = annotation
}
return pinView
} //func mapView
在viewDidLoad中,使用设置和For循环设置注释
override func viewDidLoad() {
super.viewDidLoad()
// setup the region and Span
var theSpan:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)
// Set the region to the the first element of the structure array.
var theRegion:MKCoordinateRegion = MKCoordinateRegionMake(CLLocationCoordinate2DMake(MySupplierData[0].latitude, MySupplierData[0].longitude), theSpan)
// This set the Map Type (Standard, Satellite, Hybrid)
self.theMapView.mapType = MKMapType.Standard
// Now loop through the structure data from 1 top the end of the structure to map the data
var mytitle: String = ""
var mysubtitle: String = ""
var myCustomPinAnnotation: CustomMapPinAnnotation
for mystructindex = 0; mystructindex < MySupplierData.count; ++mystructindex {
println("INSIDE SUPPLIER LOOP INDEX = \(mystructindex)" )
switch MySupplierData[mystructindex].status {
case 0:
mytitle = "(Red) " + MySupplierData[mystructindex].company
case 1:
mytitle = "(Red) " + MySupplierData[mystructindex].company
case 2:
mytitle = "(Geeen) " + MySupplierData[mystructindex].company
case 3:
mytitle = "(Purple) " + MySupplierData[mystructindex].company
default:
mytitle = "? " + MySupplierData[mystructindex].company
}
mysubtitle = MySupplierData[mystructindex].subtitle
// Create the Custom Annotations with my added status code
myCustomPinAnnotation = CustomMapPinAnnotation(
coordinate: CLLocationCoordinate2DMake(MySupplierData[mystructindex].latitude,MySupplierData[mystructindex].longitude),
title: mytitle, // custom title
subtitle: mysubtitle, // custom subtitle
status: MySupplierData[mystructindex].status) // status that will drive pin color
// put this annotation in the view.
self.theMapView.addAnnotation(myCustomPinAnnotation)
} // For
// This line brings up the display with the specific region in mind, otherwise it seems to default to a US Map.
self.theMapView.setRegion(theRegion, animated: true)
} // viewDidLoad
调试输出显示,在mapView中的自定义viewForAnnotation在内部自行执行之前,For循环按预期完成了创建myCustomPinAnnotation的执行。当我将地图移到即时视图之外的区域时,我确实注意到mapView中的viewForAnnotation根据需要被调用,并且我看到我的开关相应地执行了,但是针脚颜色并不总是正确的。初始显示映射中的所有图钉每次都是正确的,因此我一直坚持使用这些外部区域的图钉,以了解它们为何断开。
首先 ,代码应该 _ 不会_ 被调用viewForAnnotation
明确自身。
删除viewForAnnotation
该addAnnotation
行后的显式调用。
viewForAnnotation
是委托方法, 并且地图视图 需要显示注释时 会自动调用它
。如果没有自动调用它,请确保delegate
已设置地图视图的属性(self
例如)。
其次 (也是真正的问题),该代码假定viewForAnnotation
委托方法只会在添加每个注释后立即被调用一次。
并非如此,并且不能保证。地图视图将viewForAnnotation
在需要显示注释的任何时候调用,并且可以针对同一注释多次调用,也可以在实际添加注释之后很长时间(例如,在用户平移或缩放地图并使注释进入视图之后)被调用。
看到MKAnnotationView是否缓冲其输入队列?了解一些其他详细信息以及指向其他答案(包括示例代码)的相关链接。
基本上,您必须使用注解对象本身存储影响注解视图的属性,并从annotation
传递到中的参数中检索这些属性viewForAnnotation
。
我为您的情况建议的是:
我假设您正在使用内置注释类MKPointAnnotation
。除了使用MKPointAnnotation
哪种不允许您将自定义status
属性与注释对象本身存储在一起之外,还可以使用以下两种方法之一:
创建一个既实现MKAnnotation
协议又具有status
属性的自定义类。在创建注释时设置此属性,并从annotation
传入的参数中提取其值viewForAnnotation
并进行pinColor
相应的设置。请参阅链接答案中的示例代码。
在实现协议的对象MySupplierData
本身中创建 对象MKAnnotation
。因此,如果中的对象MySupplierData
是某个类的实例,例如,Supplier
使Supplier
该类符合MKAnnotation
协议,则可以MySupplierData
在调用时将对象本身添加到地图视图中addAnnotation
。
3.4.4 使用混淆 自从 Gradle plugin for ProGuard 4.10 版本以后,Gradle 开始支持混淆。如果通过 Build Type 的 minifyEnabled 属性配置了使用混淆后,The ProGuard plugin 会自动被应用,并且自动创建一些任务。 android { buildTypes { release {
在Kotlin中,< code>var是可变的,而< code>val只能赋值一次。 但是,请考虑以下示例中的< code>val foo: 每次尝试访问,都会执行方法,从而产生不同的val值。 由于< code>foo的值在变化,所以我尝试使用< code>var。然后编译器抱怨“属性必须初始化”。所以我必须给它一个默认值: 这两种方法我都不喜欢。正确的做法是什么?
问题内容: 我应该如何自定义unittest.mock.mock_open来处理此代码? 我第一次尝试。 这失败了,大概是因为代码不使用read,readline或readlines。unittest.mock.mock_open的文档说:“ read_data是一个字符串,供文件句柄的read(),readline()和readlines()方法返回。对这些方法的调用将从read_data中获取
问题内容: 我正在研究[Alex Marteli的《Nutshell》中的Python,这本书建议具有方法的任何对象都是(或至少可以用作) 迭代器 。它还建议大多数迭代器是通过对称为的方法的隐式或显式调用构建的。 阅读本书中的内容后,我感到有尝试的渴望。我启动了python 2.7.3解释器并执行以下操作: 但是结果是这样的: 在混乱中,我尝试通过研究x对象的结构,并发现它具有一个功能对象。因此,
我一直在摆弄Proguard配置,我想测试只是为了优化 但我仍然会遇到这样的错误: java.lang.IllegalArgumentExc0019:找不到[org/apache/log/log4j/core/jackson/Log4jXmlMoules](有1个已知的超级类)和[org/apache/log/log4j/core/jackson/Log4jJsonMoules](有4个已知的超级
Powershell的混淆目前已经使用的越来越多,国内外也有了较多的研究,在今年的BH大会上也有对应的议题,关注点是反混淆,那么里面的一些姿势很值得我们学习,我们提供一些混淆实例,来让大家对于PS的混淆做到一个初步了解,也为防御混淆提供一些思路。 实例 在混淆之前,先看看powershell编码执行的方式。 -EC,-EncodedCommand,-EncodedComman,-EncodedCo