Redraw direction paths in didUpdateLocations - ios

I want to redraw my direction path and move my marker to the current location. I am currently doing this inside didUpdateLocations function but it gets glitchy because it draws multiple markers and path at the same time.
Here is my code:
let location = locations.last
let destination = CLLocation(latitude: order!.user_coord![0], longitude: order!.user_coord![1])
let origin = CLLocation(latitude: (location?.coordinate.latitude)!, longitude: (location?.coordinate.longitude)!)
self.drawPath(startLocation: origin, endLocation: destination)
self.createMarker(titleMarker: order!.store_name!, iconMarker: UIImage(named: "icons8-user_filled")!, latitude: order!.user_coord![0], longitude: order!.user_coord![1])
self.createMarker(titleMarker: "User", iconMarker: UIImage(named: "icons8-mountain_biking")!, latitude: (location?.coordinate.latitude)!, longitude: (location?.coordinate.longitude)!)
let camera = GMSCameraPosition.camera(withLatitude: (location?.coordinate.latitude)!, longitude: (location?.coordinate.longitude)!, zoom: 17.0)
self.map.animate(to: camera)
locationManager.stopUpdatingLocation()

Declare a variable
var drawFinished:Bool!
in viewDidLoad
drawFinished = false
in didUpdate
if(!drawFinished)
{
drawFinished = true
let location = locations.last
let destination = CLLocation(latitude: order!.user_coord![0], longitude: order!.user_coord![1])
let origin = CLLocation(latitude: (location?.coordinate.latitude)!, longitude: (location?.coordinate.longitude)!)
self.drawPath(startLocation: origin, endLocation: destination)
self.createMarker(titleMarker: order!.store_name!, iconMarker: UIImage(named: "icons8-user_filled")!, latitude: order!.user_coord![0], longitude: order!.user_coord![1])
self.createMarker(titleMarker: "User", iconMarker: UIImage(named: "icons8-mountain_biking")!, latitude: (location?.coordinate.latitude)!, longitude: (location?.coordinate.longitude)!)
let camera = GMSCameraPosition.camera(withLatitude: (location?.coordinate.latitude)!, longitude: (location?.coordinate.longitude)!, zoom: 17.0)
self.map.animate(to: camera)
locationManager.stopUpdatingLocation()
}
when you want to update set
drawFinished = false
and clear paths and annotations like this
func clearMapOverlays()
{
for dl in self.myMapView.overlays
{
if(dl is GMSPolyline )
{
self.myMapView.remove(dl)
}
}
self.myMapView.setNeedsDisplay()
for name in self.myMapView.annotations
{
self.myMapView.removeAnnotation(anno)
}
}

Related

How to Convert coordinates to address using Swift

I try to use reverseGeocoordinate to show the address from locationEnd at my DestinationSearchBar but i don't know how to do it.
func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
let camera = GMSCameraPosition.camera(withLatitude: place.coordinate.latitude, longitude: place.coordinate.longitude, zoom: 15.0)
if locationSelected == .startLocation {
addressSearchBar.text = "\(place.coordinate.latitude), \(place.coordinate.longitude)"
locationStart = CLLocation(latitude: place.coordinate.latitude, longitude: place.coordinate.longitude)
createMarker(titleMarker: "Punto de recojo", iconMarker: #imageLiteral(resourceName: "marker"), latitude: place.coordinate.latitude, longitude: place.coordinate.longitude)
} else {
DestinationSearchBar.text = "\(place.coordinate.latitude), \(place.coordinate.longitude)"
locationEnd = CLLocation(latitude: place.coordinate.latitude, longitude: place.coordinate.longitude)
createMarker(titleMarker: "Punto de destino", iconMarker: #imageLiteral(resourceName: "marker"), latitude: place.coordinate.latitude, longitude: place.coordinate.longitude)
self.mapView.camera = camera
self.dismiss(animated: true, completion: nil)
}
}
Apple providing reverse geo location api
let address = CLGeocoder.init()
address.reverseGeocodeLocation(CLLocation.init(latitude: latitude, longitude:longitude)) { (places, error) in
if error == nil{
if let place = places{
//here you can get all the info by combining that you can make address
}
}
}
and don't forget imports
import MapKit
import CoreLocation

ARKit navigation in iOS Swift?

I would like to do ar navigation using arkit 2.0. I tried using github sample projects https://github.com/chriswang101/AR-Navigation but dont know how to pass static data of user current location and destination lat long.
From github link user search destination and then goes to ar navigation, But in my case user directly goes to ar navigation screen by using lat long of source and destination .
How can i do that. Here is what i tried so far:
let l1 = CLLocationCoordinate2D(latitude: 34.0987346, longitude: -117.7123592)
let l2 = CLLocationCoordinate2D(latitude: 34.0990761, longitude: -117.7099627)
let l3 = CLLocationCoordinate2D(latitude: 34.0972554, longitude: -117.7093386)
let l4 = CLLocationCoordinate2D(latitude: 34.0986692, longitude: -117.7136621)
let l5 = CLLocationCoordinate2D(latitude: 34.0996506, longitude: -117.7143718)
let l6 = CLLocationCoordinate2D(latitude: 34.1001445, longitude: -117.7129978)
let sc = CLLocationCoordinate2D(latitude: 34.0195564, longitude: -118.2889155)
override func viewDidLoad() {
super.viewDidLoad()
print("route:::\(routeCoordinates)")
routeCoordinates = [CLLocationCoordinate2D(latitude: 12.873058, longitude: 80.226391)]
//Set to true to display an arrow which points north.
//Checkout the comments in the property description and on the readme on this.
// sceneLocationView.orientToTrueNorth = false
// sceneLocationView.locationEstimateMethod = .coreLocationDataOnly
sceneLocationView.showAxesNode = true
sceneLocationView.locationDelegate = self
if displayDebugging {
sceneLocationView.showFeaturePoints = true
}
self.plotARRoute()
view.addSubview(sceneLocationView)
if showMapView {
mapView.delegate = self
mapView.showsUserLocation = true
mapView.alpha = 0.8
view.addSubview(mapView)
updateUserLocationTimer = Timer.scheduledTimer(
timeInterval: 0.5,
target: self,
selector: #selector(ViewController.updateUserLocation),
userInfo: nil,
repeats: true)
}
}

Resetting/Updating Markers in Google Maps ios (swift3)

I am building an ios application that uses a google map to display the location of the places return in the server. I manage to display the result in the initial load of the map, but when I want to search new plaaces and reload the markers in the map. Unfortunately the app was crashes.
Hope for your help.
Thank you.
Here is my code in the ViewController to initially display the markers:
func setUpMap() {
let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 10.0)
self.mapView.camera = camera
let map_view = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
for data in arrData {
let long = data["LONGITUDE"].string!
let lat = data["LATITUDE"].string!
if long != "" || lat != "" {
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: CLLocationDegrees(lat)!, longitude: CLLocationDegrees(long)!)
marker.title = ""
marker.snippet = "Hey, this is sample"
marker.icon = UIImage(named: "locator")
marker.map = self.mapView
bounds = bounds.includingCoordinate(marker.position)
}
}
// fit to bounds
mapView.animate(with: GMSCameraUpdate.fit(bounds, withPadding: 50.0))
self.mapView = map_view
}
Here is my code to reload/reset the markers in the map.
func reloadMarkers() {
self.mapView.clear()
let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 10.0)
self.mapView.camera = camera
let map_view = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
var _bounds = GMSCoordinateBounds()
for data in arrData {
let long = data["LONGITUDE"].string!
let lat = data["LATITUDE"].string!
if long != "" || lat != "" {
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: CLLocationDegrees(lat)!, longitude: CLLocationDegrees(long)!)
marker.title = ""
marker.snippet = "Hey, this is sample"
marker.icon = UIImage(named: "locator")
marker.map = self.mapView
_bounds = _bounds.includingCoordinate(marker.position)
}
}
// fit to bounds
mapView.animate(with: GMSCameraUpdate.fit(bounds, withPadding: 50.0))
self.mapView = map_view
}
Thanks for the help.
Cheers!
I found a solution, please see sample code below.
func reloadMarkers() {
let camera = GMSCameraPosition.camera(withLatitude: 12.0377995, longitude: 122.6408281, zoom: 10.0)
let map_view = GMSMapView.map(withFrame: self.brachMapView.bounds, camera: camera)
map_view.camera = camera
var bounds = GMSCoordinateBounds()
for data in arrData {
let long = data["LONGITUDE"].string!
let lat = data["LATITUDE"].string!
if long != "" || lat != "" {
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: CLLocationDegrees(lat)!, longitude: CLLocationDegrees(long)!)
marker.title = ""
marker.snippet = "Hey, this is sample"
marker.icon = UIImage(named: "locator")
marker.map = map_view
bounds = bounds.includingCoordinate(marker.position)
}
}
// fit to bounds
map_view.animate(with: GMSCameraUpdate.fit(bounds, withPadding: 50.0))
self.brachMapView.addSubview(map_view)
}
Thanks.
Cheers

Google Maps not zooming to my current location

I have to plot multiple pins in a Google Map view, and then zoom to the user's current location. Pins are plotted successfully but not zoomed to current location. Any help would be appreciated
override func viewDidAppear(_ animated: Bool) {
if (self.googleMapsView == nil) {
self.googleMapsView = GMSMapView(frame: self.mapViewContainer.frame)
self.view.addSubview(self.googleMapsView)
print(AppDelegate.getAppDelegate().mapArray)
googleMapsView.clear()
for i in 0...AppDelegate.getAppDelegate().mapArray.count - 1 {
let lon = Double ((AppDelegate.getAppDelegate().mapArray[i]["lon"] as? String)!)
let lat = Double ((AppDelegate.getAppDelegate().mapArray[i]["lat"] as? String)!)
let tit = AppDelegate.getAppDelegate().mapArray[i]["location"] as? String
locateWithLongitude(lon: lon!,andLatitude: lat! ,andTitle: tit!)
}
let marker = GMSMarker(position: AppDelegate.getAppDelegate().myLocation)
let camera = GMSCameraPosition.camera(withLatitude: AppDelegate.getAppDelegate().myLocation.latitude, longitude: AppDelegate.getAppDelegate().myLocation.longitude, zoom: 11)
self.googleMapsView.camera = camera
marker.title = AppDelegate.getAppDelegate().sublocality
marker.map = self.googleMapsView
self.googleMapsView.animate(to: camera)
}
}
func locateWithLongitude(lon: Double, andLatitude lat: Double, andTitle title: String) {
DispatchQueue.main.async() { () -> Void in
let position = CLLocationCoordinate2DMake(lat, lon)
let marker = GMSMarker(position: position)
let camera = GMSCameraPosition.camera(withLatitude: lat, longitude: lon, zoom: 0)
self.googleMapsView.camera = camera
marker.title = title
marker.map = self.googleMapsView
}
}
Your zoom is set to 0, increase it to 15/16 like bellow.
let camera = GMSCameraPosition.camera(withLatitude: lat, longitude: lon, zoom: 16)

Swift CLLocationDistance error

I am building a learning app where i want to create pins and show the distance between current location and the pin but i get a really weird output
func createPin(){
var coord = CLLocationCoordinate2D(latitude: 51.50, longitude: -0.13)
var coord2 = CLLocation(latitude: coord.latitude, longitude: coord.longitude)
var kilometers:CLLocationDistance = coord2.distanceFromLocation(locNow)
var str = NSString(format: "%.2f", kilometers)
let pin = Annotation(coordinate: coord, title: "LocationAlfa", subtitle: "distance : \(str)" + " meters", dist: kilometers)
map.addAnnotation(pin)
println("\(kilometers)")
}
this is my create pin method and here i get my location distance
func locationManager(manager: CLLocationManager!, didUpdateToLocation newLocation: CLLocation!, fromLocation oldLocation: CLLocation!){
let location = newLocation
let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
var region: () = centerMapOnLocation(location)
// self.map.setRegion(region, animated: true)
println("Latitude = \(newLocation.coordinate.latitude)")
println("Longitude = \(newLocation.coordinate.longitude)")
locNow = newLocation
}
and this is the shown distance in meters on the map : 5718215.17 (when the pin is made next to the pointed location on the map as the device location)

Resources