Swift Encode Lat&Lon to Polyline - swift

I have a list of lat&lon, want to show it on map as a path. I could show polyline on the map by using GMSPath(fromEncodePath: polyline).
How could I convert lat&lon list as a polyline or display it on the map?
Much appreciated!

You can create a path from coordinates like this
let path = GMSMutablePath()
path.add(CLLocationCoordinate2DMake(29.520,30.856))
path.add(CLLocationCoordinate2DMake(25.17,40.1235))
let pLine = GMSPolyline.init(path: path)

Related

Extract faces information from SCNGeometry in SceneKit

I want to extract faces information from a SCNNode geometry just like we can extract vertices information from geometry sources. Any idea on how that can be achieved?
If you have a SCNGeometry object (which you can get from a SCNNode with node.geometry) then you can look at the elements property which will contain the face information as an array of SCNGeometryElement objects.
e.g. assuming you just want the first element
let element = geometry.elements[0]
let faces = element.data.withUnsafeBytes {(ptr: UnsafeRawBufferPointer) -> [Int32] in
guard let boundPtr = ptr.baseAddress?.assumingMemoryBound(to: Int32.self) else {return []}
let buffer = UnsafeBufferPointer(start: boundPtr, count: element.data.count / 4)
return Array<Int32>(buffer)
}
print(faces)
Depending on element.primitiveType you will need to interpret the indices differently. See the documentation for SCNGeometryPrimitiveType.

Convert point to LatLng on Leaflet

I'm trying to get the right location of my point on the map as Latlng:
let point = L.point(0,0) // x=0,y=0
let latlng = map.unproject(point)
but the latlng value is not the real location!
what am i missing ?
the function: unproject is not correct.
you should use: layerPointToLatLng:
let point = L.point(0,0); // x=0,y=0
let latlng = map.layerPointToLatLng(point);
regards
Rachel

Swift: Removing only one MKPolyline of several

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"?

Create an array of MKPointAnnotation objects

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.

Storing and deleting placemarks

I am trying to store placemarks in a global array but not sure what I'm actually trying to store. I want to be able to remove the placemark at a later time. Do I just need to store the lat and lng or do I need to store some kind of placemark obj in the array. I've tried to look at the google docs but they are I think written in greek. Could someone point me in the right direction??
Inside a for loop on the global array PASSENGERS we have the following code:
// prepare placemark
var placemark = earth.createPlacemark("");
placemark.setName(PASSENGERS[i].name + " to " + PASSENGERS[i].house);
// prepare icon
var icon = earth.createIcon("");
icon.setHref(url + "/img/" + PASSENGERS[i].username + ".jpg");
// prepare style
var style = earth.createStyle("");
style.getIconStyle().setIcon(icon);
style.getIconStyle().setScale(4.0);
// prepare stylemap
var styleMap = earth.createStyleMap("");
styleMap.setNormalStyle(style);
styleMap.setHighlightStyle(style);
// associate stylemap with placemark
placemark.setStyleSelector(styleMap);
// prepare point
var point = earth.createPoint("");
point.setAltitudeMode(earth.ALTITUDE_RELATIVE_TO_GROUND);
point.setLatitude(building.lat);
point.setLongitude(building.lng);
point.setAltitude(0.0);
// associate placemark with point
placemark.setGeometry(point);
I thought I could print out placemark with:
for (var prop in placemark)
{
console.log(prop + " = " + placemark[prop]);
}
but that doesn't seem to work.
I think you need to take some time and try to understand the docs.
See https://developers.google.com/earth/documentation/placemarks?csw=1
Once you add placemarks to the the plugin they are in memory inside the plugin and there is no need to store them in another array.
You can assign the placemark an id.
// to add
var placemark= ge.createPlacemark('your_id_here');
ge.getFeatures().appendChild(placemark);
// to remove
var myPlacemark= ge.getElementById('your_id_here');
ge.getFeatures().removeChild(myPlacemark);