dang dang - 2 months ago 6
Swift Question

Dynamic code for storing multiple values

I have a polygon which has been plotted for a county using Google Maps iOS SDK.

For each county, there are multiple latitude and longitude so we can plot it.

The code is for 1 Country. Is there a way where I can make it for multiple countries?

So, the structure would be:

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


Code:

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

}
}

Answer

Option 1

You can try like this create on Location struct that store lat-long and change Country struct that will store country name and CountryLocation array.

struct CountryLocation {
    let lat: CLLocationDegrees
    let long: CLLocationDegrees
}

struct Country {
    var name: String
    var locations = [CountryLocation]()
}

Now create array of country and all object of it will store locations for that specific country.

Option 2

You can use Dictionary that contain key as Country name and value as your Array of your Country for that you just need to create the dictionary no need to change any thing in your struct.

var countries = [String: [Country]]()

Edit: To store object for option 1 try like this.

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)]
                ]