I'm try to create a func that should calculate the coordinate at 90deg and 270deg from each of the given coordinate..
and save it in an array of arrayLocationOffset
var arrayLocations : [CLLocationCoordinate2D] =
[
CLLocationCoordinate2D(latitude: 45.15055543976834, longitude: 11.656891939801518 ),
CLLocationCoordinate2D(latitude: 45.154446871287924, longitude: 11.66058789179949),
]
// simplify only 2 coordinate
I use this for loop in order to perform the action:
func createCoordinatePoly() {
let polypoint = arrayLocations.count
for i in 0..<polypoint {
let coord = arrayLocations[i]
// calc LH
let lhCoord = coord270(coord: coord)
arrayLocationOffset.append(lhCoord)
}
for i in 0..<polypoint { // shuld revers this for loop and get first the last index
let coord = arrayLocations[i]
// calc LH
let RhCoord = coord90(coord: coord)
arrayLocationOffset.append(RhCoord)
}
debugPrint("aggiunto array poly \(arrayLocationOffset.count)")
}
The arrayLocationOffset must have a specific sequence otherwise I can't properly draw a polygon in the Mapkit.
In order get the specific order on the second forLoop (where I calculate RHside) I should start to calculate from last array index back to the first...
is this possible?
thanks
As the name implies you can reverse an array just with reversed().
Your code is rather objective-c-ish. A better way to enumerate the array is fast enumeration because you actually don't need the index.
for coord in arrayLocations { ...
and
for coord in arrayLocations.reversed() { ...
However there is a still swiftier way mapping the arrays
func createCoordinatePoly() {
arrayLocationOffset.append(contentsOf: arrayLocations.map{coord270(coord: $0)})
arrayLocationOffset.append(contentsOf: arrayLocations.reversed().map{coord90(coord: $0)})
debugPrint("aggiunto array poly \(arrayLocationOffset.count)")
}
solved:
var reversIndex = polypoint-1
for _ in 0..<polypoint {
let coord = arrayLocations[reversIndex]
// calc LH
let RhCoord = coord90(coord: coord)
arrayLocationOffset.append(RhCoord)
reversIndex -= 1
}
I use a reverse index to start from the end...
I am using an API that returns a String that contains coordinate. How do I convert this response:
"route_pins":
"#30.0539983,30.943465#30.0539033,30.9434167#30.05379,30.9434467#30.0536117,30.943865#30.0535133,30.9439867#30.0534633,30.9440967#30.05353,30.94428#30.053675,30.944525#30.0539933,30.9449667#30.0541517,30.9452317#30.0542917,30.9454717#30.054365,30.9455717#30.0544667,30.945725#30.05471,30.9460733#30.0548667,30.94631#30.0550417,30.9465683#30.0553733,30.9471133#30.0557133,30.9476383#30.0558667,30.947905#30.0560083,30.9481767#30.0562517,30.94872#30.0564917,30.9492217#30.0565783,30.9494567#30.056645,30.9496883#30.0566167,30.9501883"
into an Array of CLLocationCoordinate2D to draw a line using MapKit using:
let polyLine = MKPolyline(coordinates: Locations , count: Locations.count)
busMapView.add(polyLine)
You can do it with a combination of components(separatedBy and map
Split the string by character # and drop the first (empty) item.
Map each item to CLLocationCoordinate2D by splitting the string by the comma, convert the strings to CLLocationCoordinate2D and create the coordinate.
let routePins = "#30.0539983,30.943465#30.0539033,30.9434167#30.05379,30.9434467#30.0536117,30.943865#30.0535133,30.9439867#30.0534633,30.9440967#30.05353,30.94428#30.053675,30.944525#30.0539933,30.9449667#30.0541517,30.9452317#30.0542917,30.9454717#30.054365,30.9455717#30.0544667,30.945725#30.05471,30.9460733#30.0548667,30.94631#30.0550417,30.9465683#30.0553733,30.9471133#30.0557133,30.9476383#30.0558667,30.947905#30.0560083,30.9481767#30.0562517,30.94872#30.0564917,30.9492217#30.0565783,30.9494567#30.056645,30.9496883#30.0566167,30.9501883"
let coordinates = routePins.components(separatedBy: "#").dropFirst().map { (pin) -> CLLocationCoordinate2D in
let latLng = pin.components(separatedBy: ",").map{ CLLocationDegrees($0)! }
return CLLocationCoordinate2D(latitude: latLng[0], longitude: latLng[1])
}
The result is [CLLocationCoordinate2D].
let dic:Dictionary<String,Any> = ["route_pins": "#30.0539983,30.943465#30.0539033,30.9434167#30.05379,30.9434467#30.0536117,30.943865#30.0535133,30.9439867#30.0534633,30.9440967#30.05353,30.94428#30.053675,30.944525#30.0539933,30.9449667#30.0541517,30.9452317#30.0542917,30.9454717#30.054365,30.9455717#30.0544667,30.945725#30.05471,30.9460733#30.0548667,30.94631#30.0550417,30.9465683#30.0553733,30.9471133#30.0557133,30.9476383#30.0558667,30.947905#30.0560083,30.9481767#30.0562517,30.94872#30.0564917,30.9492217#30.0565783,30.9494567#30.056645,30.9496883#30.0566167,30.9501883"]
let strLatLng = dic["route_pins"] as! String
let arrayOflatLng = strLatLng.components(separatedBy: "#")
var testcoords = [CLLocationCoordinate2D]()
for latLngStr in arrayOflatLng {
if let strLat = latLngStr.components(separatedBy: ",") as? [String], strLat.count == 2 {
testcoords.append(CLLocationCoordinate2D(latitude: CLLocationDegrees(strLat[0])!, longitude: CLLocationDegrees(strLat[1])!))
}
}
print("testcoords \(testcoords.count)")
let polyLine = MKPolyline(coordinates: testcoords , count: testcoords.count)
busMapView.add(polyLine)
The API string is parsed using 2 levels of parsing ,first we split the API string to locations strings , then using for loop to split each location to its coordinates and save coordinates to CLLocationCoordinate2D and append it to result array
let routePins = "#30.0539983,30.943465#30.0539033,30.9434167#30.05379,30.9434467#30.0536117,30.943865#30.0535133,30.9439867#30.0534633,30.9440967#30.05353,30.94428#30.053675,30.944525#30.0539933,30.9449667#30.0541517,30.9452317#30.0542917,30.9454717#30.054365,30.9455717#30.0544667,30.945725#30.05471,30.9460733#30.0548667,30.94631#30.0550417,30.9465683#30.0553733,30.9471133#30.0557133,30.9476383#30.0558667,30.947905#30.0560083,30.9481767#30.0562517,30.94872#30.0564917,30.9492217#30.0565783,30.9494567#30.056645,30.9496883#30.0566167,30.9501883"
let pins = routePins.components(separatedBy: "#")//first step is splitting the fetched array to pins array
var LocationsArray = [CLLocationCoordinate2D]()//this is the result array
for location in pins {
if(location.contains(",")){//checking that the location is containing lat ,long separtor
let coordinates = location.components(separatedBy: ",")//splitting the location to lat ,long
//safe parsing the string value to double vale for coordinates
let lat = Double(coordinates[0]) ?? 0.0
let long = Double( coordinates[1]) ?? 0.0
LocationsArray.append(CLLocationCoordinate2D(latitude: lat
,longitude: long))// appending new coordinates to locations array
}
}
Hi I am making an app that show me interesting places. Its showing places in radius. I am using REALM to store values.
However realm don't know how to make Unique values. I am using this for Unique rows.
let result:[String] = realm.objects(E21).sorted("name").uniqueValue("Id_prov", type: String.self)
This for finding things in region around me
var datasourceE21Distance:Results<E21> = realm.findInRegion(E21.self, region: curentRegion).filter(tempRequest)
But i don't know how to combine these things to one and then sort it from closes one to me to the most far.
I will be glad for any help here.
EDIT i am using these two extensions found:
func findInRegion<T: Object>(type: T.Type, region: MKCoordinateRegion, latitudeKey: String = "lat", longitudeKey: String = "lng") -> Results<T> {
// Query
return self
.objects(type)
.filterGeoBox(region.geoBox, latitudeKey: latitudeKey, longitudeKey: longitudeKey)
}
func uniqueValue<U : Equatable>(paramKey: String, type: U.Type)->[U]{
var uniqueValues : [U] = [U]()
for obj in self {
if let val = obj.valueForKeyPath(paramKey) {
if (!uniqueValues.contains(val as! U)) {
uniqueValues.append(val as! U)
}
}
}
return uniqueValues
}
RealmGeoQueries, the library you're using for filtering your entities by a bounding box, supports sorting objects by distance via sortByDistance. This returns an array as this operation has to be done in memory with cached distances.
You would need to make sure that you're uniqueValue method is defined in an extension on Array.
I came up with something like this. But it can't filter right away :-/
let currentLocation = CLLocationCoordinate2D(latitude: currentLat!, longitude: currentLng!)
sortedObjectsByDistance = realm.findNearby(E21.self, origin: currentLocation, radius: 50000.0, sortAscending: true, latitudeKey: "lat", longitudeKey: "lng")
var seenIdProv:[String:Bool] = [:]
sortedObjectsByDistance = sortedObjectsByDistance.filter {
seenIdProv.updateValue(false, forKey: $0.Id_prov) ?? true
}
I have two polylines on the map:
var polylineRoute : MKGeodesicPolyline!
var polylineFlight : MKGeodesicPolyline!
I assign each of them a title and add them to the map like this (in different methods):
let polyline = MKGeodesicPolyline(coordinates: &routeCoordinates, count: routeCoordinates.count)
polyline.title = "route"
self.mapView.addOverlay(polyline)
self.polylineRoute = polyline
and
let polyline = MKGeodesicPolyline(coordinates: &routeCoordinates, count: routeCoordinates.count)
polyline.title = "flight"
self.mapView.addOverlay(polyline)
self.polylineFlight = polyline
Now, when a specific action is triggered, I would like to remove only the flight overlay and leave the route overlay intact.
This does not work at all:
func removeFlightPath()
{
self.mapView.removeOverlay(self.polylineFlight)
self.polylineFlight = nil
}
The following works but removes both polylines:
func removeFlightPath()
{
var overlays = mapView.overlays
mapView.removeOverlays(overlays)
}
Is there a working way to remove only one polyline? I searched the forum and there is only one response that is saying that it is possible using the title. However, it does not specify how it can be done.
Thanks a lot!
EDIT:
This solves the issue:
func removeFlightPath()
{
if self.polylineFlight != nil
{
// Overlays that must be removed from the map
var overlaysToRemove = [MKOverlay]()
// All overlays on the map
let overlays = self.mapView.overlays
for overlay in overlays
{
if overlay.title! == "flight"
{
overlaysToRemove.append(overlay)
}
}
self.mapView.removeOverlays(overlaysToRemove)
}
}
I think your source code is correct. Could be that the reference counting is messing it up. As long as the object is referred to, MKGeodesicPolyline will not be removed. In your code, you have used a local variable to create the polyline object. I have tried it without using a local variable and it is removing the polyline.
self.polylineFlight = MKGeodesicPolyline(coordinates: &routeCoordinates, count: routeCoordinates.count)
self.polylineFlight.title = "flight"
polylineFlight doesn't look right. It's built from routeCoordinates, the same as polylineRoute. So removing it would produce no change in the map.
Are you building from the right coordinates?
Can we see before/after screenshots? Or can we see a clarification of "does not work at all"?
Currently working with Map Views and adding pins to the map. I know how to add a single point to the map using addAnotation() method. Now, I am trying to add multiple points to the MapView in the easiest way. I've fetched the data (latitude, longitude and name from an online XML file) and stored it in an array and now I want to add all those coordinates+name as pins in the map. For doing so I've declared an array of MKPointAnnotation objects like so:
var pinsArray: [MKPointAnnotation] = []
And then for dumping the collected data to I've done the following:
for i in 0...(myFeed.count-1) {
pinsArray[i].title = myFeed.objectAtIndex(i).objectForKey("NOMBRE")!.stringValue
pinsArray[i].coordinate = CLLocationCoordinate2D(latitude: myFeed[i].objectForKey("LATITUD")!.doubleValue, longitude: myFeed[i].objectForKey("LONGITUD")!.doubleValue)
pinsArray[i].subtitle = ""
mapView.addAnnotation(pinsArray[i])
}
But when I run the app I get an error saying that the array index is out of range (fatal error: Array index out of range). I guess this is a problem on the declaration of the pinsArray, I do not really know how to solve this one.
Try this:
var pinsArray: [MKPointAnnotation] = []
for i in 0...(myFeed.count-1)
{
let pointAnnotation = MKPointAnnotation() // First create an annotation.
pointAnnotation.title = myFeed.objectAtIndex(i).objectForKey("NOMBRE")!.stringValue
pointAnnotation.coordinate = CLLocationCoordinate2D(latitude: myFeed[i].objectForKey("LATITUD")!.doubleValue, longitude: myFeed[i].objectForKey("LONGITUD")!.doubleValue)
pointAnnotation.subtitle = ""
pinsArray.append(pointAnnotation) // Now append this newly created annotation to array.
}
mapView.addAnnotations(pinsArray) // Add all the annotations to map view at once.