我有一个使用谷歌地图iOS SDK为县绘制的多边形。
对于每个县,有多个纬度和经度,所以我们可以绘制它。
代码适用于1个国家/地区。有没有办法让我可以为多个国家制作它?
所以,结构将是:
County 1 -
lat long
32.4757 -86.41182
31.7557 -88.41482
32.4357 -87.65335
County 2 -
lat long
42.4757 -96.41182
41.7557 -98.41482
42.4357 -97.65335
County 3 -
lat long
52.4757 -76.41182
51.7557 -78.41482
52.4357 -77.65335
代码:
import UIKit
import GoogleMaps
class ViewController: UIViewController {
override func loadView() {
let camera = GMSCameraPosition.cameraWithLatitude(37.0902, longitude: -95.7129, zoom: 3.0)
let mapView = GMSMapView.mapWithFrame(CGRect.zero, camera: camera)
mapView.myLocationEnabled = true
view = mapView
struct county {
let lat: CLLocationDegrees
let long: CLLocationDegrees
}
let counties = [
county(lat: 32.4757, long: -86.41182),
county(lat: 32.46599, long: -86.41177),
county(lat: 32.45054, long: -86.41167),
county(lat: 32.44245, long: -86.41157),
county(lat: 32.43993, long: -86.41154),
county(lat: 32.42573, long: -86.41138),
county(lat: 32.42417, long: -86.41135),
county(lat: 32.42185, long: -86.41128),
county(lat: 32.41017, long: -86.41117),
county(lat: 32.40994, long: -86.41117),
county(lat: 32.4072, long: -86.41615),
county(lat: 32.40132, long: -86.43178),
county(lat: 32.40025, long: -86.43926),
county(lat: 32.40036, long: -86.44653),
county(lat: 32.40573, long: -86.45876),
county(lat: 32.40285, long: -86.4612),
county(lat: 32.38769, long: -86.46247),
county(lat: 32.37729, long: -86.46356),
county(lat: 32.37368, long: -86.46836),
county(lat: 32.37136, long: -86.47092),
county(lat: 32.36874, long: -86.47306),
county(lat: 32.36588, long: -86.47476),
county(lat: 32.36418, long: -86.47777),
county(lat: 32.36497, long: -86.48023),
county(lat: 32.36667, long: -86.48342),
county(lat: 32.36674, long: -86.4871),
county(lat: 32.36532, long: -86.49047),
county(lat: 32.36286, long: -86.49265),
county(lat: 32.36032, long: -86.49263),
county(lat: 32.35787, long: -86.49181),
county(lat: 32.35513, long: -86.4908),
county(lat: 32.35264, long: -86.48994),
county(lat: 32.34985, long: -86.4899),
county(lat: 32.34705, long: -86.49071),
county(lat: 32.34509, long: -86.49308),
county(lat: 32.34451, long: -86.49637),
county(lat: 32.34444, long: -86.49677),
county(lat: 32.34441, long: -86.49697),
county(lat: 32.34039, long: -86.51978),
county(lat: 32.36286, long: -86.54242),
county(lat: 32.37296, long: -86.56658),
county(lat: 32.36775, long: -86.58826),
county(lat: 32.38072, long: -86.61646),
county(lat: 32.40307, long: -86.61548),
county(lat: 32.40372, long: -86.62671),
county(lat: 32.40055, long: -86.64956),
county(lat: 32.37904, long: -86.65426),
county(lat: 32.36937, long: -86.66333),
county(lat: 32.35659, long: -86.67726),
county(lat: 32.35356, long: -86.68276),
county(lat: 32.35677, long: -86.69448),
county(lat: 32.36186, long: -86.71309),
county(lat: 32.36921, long: -86.71624),
county(lat: 32.3884, long: -86.71982),
county(lat: 32.40129, long: -86.71721),
county(lat: 32.40061, long: -86.73083),
county(lat: 32.38914, long: -86.75543),
county(lat: 32.39047, long: -86.78214),
county(lat: 32.37337, long: -86.78238),
county(lat: 32.32449, long: -86.7843),
county(lat: 32.30819, long: -86.80352),
county(lat: 32.3184, long: -86.82039),
county(lat: 32.3402, long: -86.81563),
county(lat: 32.3408, long: -86.81491),
county(lat: 32.34299, long: -86.81229),
county(lat: 32.36582, long: -86.81294),
county(lat: 32.38605, long: -86.8318),
county(lat: 32.41849, long: -86.8411),
county(lat: 32.4304, long: -86.83652),
county(lat: 32.43893, long: -86.85357),
county(lat: 32.43869, long: -86.86242),
county(lat: 32.44592, long: -86.86415),
county(lat: 32.4606, long: -86.86997),
county(lat: 32.47188, long: -86.87364),
county(lat: 32.48191, long: -86.88497),
county(lat: 32.48886, long: -86.88087),
county(lat: 32.49656, long: -86.88465),
county(lat: 32.5124, long: -86.89084),
county(lat: 32.52588, long: -86.89979),
county(lat: 32.53685, long: -86.90667),
county(lat: 32.54915, long: -86.90521),
county(lat: 32.55933, long: -86.90023),
county(lat: 32.57819, long: -86.89905),
county(lat: 32.59242, long: -86.90385),
county(lat: 32.60835, long: -86.90759),
county(lat: 32.62085, long: -86.91163),
county(lat: 32.63178, long: -86.9119),
county(lat: 32.64452, long: -86.9185),
county(lat: 32.65322, long: -86.91827),
county(lat: 32.66411, long: -86.91767),
county(lat: 32.66417, long: -86.91759),
county(lat: 32.66417, long: -86.91757),
county(lat: 32.66425, long: -86.91241),
county(lat: 32.66343, long: -86.8929),
county(lat: 32.66258, long: -86.87358),
county(lat: 32.66137, long: -86.84557),
county(lat: 32.66082, long: -86.83382),
county(lat: 32.66016, long: -86.81764),
county(lat: 32.66017, long: -86.81319),
county(lat: 32.66052, long: -86.78767),
county(lat: 32.66098, long: -86.74998),
county(lat: 32.66159, long: -86.72508),
county(lat: 32.66738, long: -86.71349),
county(lat: 32.70574, long: -86.71257),
county(lat: 32.70609, long: -86.66704),
county(lat: 32.70633, long: -86.63189),
county(lat: 32.70629, long: -86.61778),
county(lat: 32.70632, long: -86.60783),
county(lat: 32.70653, long: -86.59515),
county(lat: 32.70678, long: -86.57015),
county(lat: 32.70704, long: -86.52944),
county(lat: 32.7073, long: -86.4963),
county(lat: 32.7076, long: -86.48468),
county(lat: 32.70778, long: -86.47197),
county(lat: 32.70786, long: -86.46803),
county(lat: 32.70809, long: -86.45035),
county(lat: 32.70747, long: -86.41583),
county(lat: 32.70739, long: -86.41312),
county(lat: 32.67818, long: -86.413),
county(lat: 32.67406, long: -86.41298),
county(lat: 32.61636, long: -86.41261),
county(lat: 32.58942, long: -86.4125),
county(lat: 32.57816, long: -86.41244),
county(lat: 32.55023, long: -86.41234),
county(lat: 32.53344, long: -86.41226),
county(lat: 32.52876, long: -86.41223),
county(lat: 32.52668, long: -86.4122),
county(lat: 32.52082, long: -86.4123),
county(lat: 32.51273, long: -86.41231),
county(lat: 32.50446, long: -86.41234),
county(lat: 32.50006, long: -86.4122),
county(lat: 32.49626, long: -86.41212),
county(lat: 32.49065, long: -86.41197),
county(lat: 32.48577, long: -86.41197),
county(lat: 32.48112, long: -86.41187),
]
let rect = GMSMutablePath()
for county in counties {
rect.addCoordinate(CLLocationCoordinate2D(latitude: county.lat, longitude: county.long))
}
// Create the polygon, and assign it to the map.
let polygon = GMSPolygon(path: rect)
polygon.fillColor = UIColor(red: 0.25, green: 0, blue: 0, alpha: 0.05);
polygon.strokeColor = UIColor.blackColor()
polygon.strokeWidth = 2
polygon.map = mapView
}
}
答案 0 :(得分:1)
选项1
您可以尝试在Location
结构上创建存储lat-long并更改将存储国家/地区名称和CountryLocation数组的国家/地区struct
。
struct CountryLocation {
let lat: CLLocationDegrees
let long: CLLocationDegrees
}
struct Country {
var name: String
var locations = [CountryLocation]()
}
现在创建一个国家/地区阵列,其中所有对象都将存储该特定国家/地区的位置。
选项2
您可以将包含密钥作为国家/地区名称和值的Dictionary
用作您所在国家/地区的数组,只需创建字典即可,无需更改struct
中的任何内容。
var countries = [String: [Country]]()
编辑:要存储选项1的对象,请尝试这样做。
var countries = [Country(name:"Ind",locations:[CountryLocation(lat:22.663696,long:87.746803),CountryLocation(lat:22.542061,long:88.318954)],
Country(name:"UK",locations:[CountryLocation(lat:52.412811,long:-1.778197),CountryLocation(lat:51.481583,long:-3.179090),CountryLocation(lat:50.768036,long:0.290472)]
]