我有一个模型类,我可以在其中从JSON
文件中解析我的项目:
class Venue: NSObject, GMUClusterItem {
let name: String?
let locationName: String?
let position: CLLocationCoordinate2D
let image = GMSMarker()
init(name: String, locationName: String?, position: CLLocationCoordinate2D, image: GMSMarker)
{
self.name = name
self.locationName = locationName
self.position = position
//self.image = image
super.init()
}
var subtitle: String? {
return locationName
}
class func from(json: JSON) -> Venue?
{
var name: String
if let unwrappedTitle = json["name"].string {
name = unwrappedTitle
} else {
name = ""
}
let locationName = json["location"]["address"].string
let lat = json["location"]["lat"].doubleValue
let long = json["location"]["lng"].doubleValue
let position = CLLocationCoordinate2D(latitude: lat, longitude: long)
let image = GMSMarker()
return Venue(name: name, locationName: locationName, position: position, image: image)
}
}
在这里,在我获取数据后,我想在 mapView 中引入标记,并且我想用图像对其进行自定义。
var venues = [Venue]()
private func generateClusterItems() {
for venue in venues {
let name = venue.name
let position = venue.position
let locationName = venue.locationName
let image = GMSMarker()
let item = Venue(name: name!, locationName: locationName, position: position, image: image)
let markerView = UIImage(named: "K_Annotation.png")!
image.icon = markerView
clusterManager.add(item)
}
clusterManager.cluster()
clusterManager.setDelegate(self, mapDelegate: self)
}
但它不起作用。它给我带来了谷歌地图的默认标记。我不知道我做错了什么?
找到的解决方案:这里在集群文件夹中启动 GMUDefaultClusterRenderer.m 中的类
- (GMSMarker *)markerWithPosition:(CLLocationCoordinate2D)position
from:(CLLocationCoordinate2D)from
userData:(id)userData
clusterIcon:(UIImage *)clusterIcon
animated:(BOOL)animated {.....
......
我用这个替换原来的:
if (clusterIcon != nil) {
marker.icon = clusterIcon;
marker.groundAnchor = CGPointMake(0.5, 0.5);
}else{
marker.icon = [UIImage imageNamed:@"K_Annotation.png"];
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句