在iOS中为google maps分配多个标记,并为每个标记初始化
我需要按纬度和经度在地图上显示多个标记,我尝试在下面的代码中显示两个标记。它工作得很好,但如果需要在20或30个位置上放置标记,我需要分配并初始化GMSMarker很多次。有没有其他方法可以在地图上显示多个标记,而无需为每个地方启动GMSMarker在iOS中为google maps分配多个标记,并为每个标记初始化,ios,objective-c,google-maps,google-maps-api-3,Ios,Objective C,Google Maps,Google Maps Api 3,我需要按纬度和经度在地图上显示多个标记,我尝试在下面的代码中显示两个标记。它工作得很好,但如果需要在20或30个位置上放置标记,我需要分配并初始化GMSMarker很多次。有没有其他方法可以在地图上显示多个标记,而无需为每个地方启动GMSMarker GMSMarker *marker = [[GMSMarker alloc] init]; marker.position = CLLocationCoordinate2DMake(17.4368, 78.4439); #warning titl
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(17.4368, 78.4439);
#warning title and snipped must be dynamic
marker.icon = [UIImage imageNamed:@"one.png"];
marker.title = @"Sydney";
marker.snippet = @"Australia";
marker.appearAnimation = kGMSMarkerAnimationPop;
marker.map = _mapView;
GMSMarker *marker1 = [[GMSMarker alloc] init];
marker1.position = CLLocationCoordinate2DMake(17.398932, 78.472718);
marker1.icon = [UIImage imageNamed:@"one.png"];
marker1.map=_mapView;
将每个标记的数据放入一个数组中,然后在其上运行一个循环
NSArray* arrMarkerData = @[
@{@"title": @"Sydney", @"snippet": @"Australia", @"position": [[CLLocation alloc]initWithLatitude:17.4368 longitude:78.4439]},
@{@"title": @"Other location", @"snippet": @"other snippet", @"position": [[CLLocation alloc]initWithLatitude:17.398932 longitude:78.472718]}
];
for (NSDictionary* dict in arrMarkerData)
{
GMSMarker *marker = [[GMSMarker alloc] init];
marker.icon = [UIImage imageNamed:@"one.png"];
marker.position = [(CLLocation*)dict[@"position"] coordinate];
marker.title = dict[@"title"];
marker.snippet = dict[@"snippet"];
marker.appearAnimation = kGMSMarkerAnimationPop;
marker.map = _mapView;
}
将每个标记的数据放入一个数组中,然后在其上运行一个循环
NSArray* arrMarkerData = @[
@{@"title": @"Sydney", @"snippet": @"Australia", @"position": [[CLLocation alloc]initWithLatitude:17.4368 longitude:78.4439]},
@{@"title": @"Other location", @"snippet": @"other snippet", @"position": [[CLLocation alloc]initWithLatitude:17.398932 longitude:78.472718]}
];
for (NSDictionary* dict in arrMarkerData)
{
GMSMarker *marker = [[GMSMarker alloc] init];
marker.icon = [UIImage imageNamed:@"one.png"];
marker.position = [(CLLocation*)dict[@"position"] coordinate];
marker.title = dict[@"title"];
marker.snippet = dict[@"snippet"];
marker.appearAnimation = kGMSMarkerAnimationPop;
marker.map = _mapView;
}
对于那些使用Swift 5的人
let arrMarkerData = [
[
"title": "Sydney",
"snippet": "Australia",
"position": CLLocationCoordinate2D(latitude: 17.4368, longitude: 78.4439)
],
[
"title": "Other location",
"snippet": "other snippet",
"position": CLLocationCoordinate2D(latitude: 17.398932, longitude: 78.472718)
]
]
for dict in arrMarkerData {
guard let dict = dict as? [AnyHashable : Any] else {
continue
}
let marker = GMSMarker()
//marker.icon = UIImage(named: "one.png")
marker.position = (dict["position"] as? CLLocationCoordinate2D)!
marker.title = dict["title"] as? String
marker.snippet = dict["snippet"] as? String
marker.appearAnimation = .pop
marker.map = mapView
}
对于那些使用Swift 5的人
let arrMarkerData = [
[
"title": "Sydney",
"snippet": "Australia",
"position": CLLocationCoordinate2D(latitude: 17.4368, longitude: 78.4439)
],
[
"title": "Other location",
"snippet": "other snippet",
"position": CLLocationCoordinate2D(latitude: 17.398932, longitude: 78.472718)
]
]
for dict in arrMarkerData {
guard let dict = dict as? [AnyHashable : Any] else {
continue
}
let marker = GMSMarker()
//marker.icon = UIImage(named: "one.png")
marker.position = (dict["position"] as? CLLocationCoordinate2D)!
marker.title = dict["title"] as? String
marker.snippet = dict["snippet"] as? String
marker.appearAnimation = .pop
marker.map = mapView
}
只需添加带数组的位置并写入for循环即可创建多个标记。只需添加带数组的位置并写入for循环即可创建多个标记。发现多个名为“坐标”的方法的结果、参数类型或属性不匹配----这是错误找到的多个名为“坐标”的方法的结果不匹配,参数类型或属性——这是错误