Open maps with lat,long and link based iOS swift
Problem:
I want a common function that i can handle one location object with lat,long and address.That address can have a link that copied from other apps like google maps,Apple maps and whatsapp.
Also want save option that will save the location into my app local database.
How to achieve all the above us.
Solution:
func openMaps(lat: Double,long: Double,address:String = "",isSave:Bool = true) {
let addressURL = URL(string: address.trimStr ?? "")
if let topview = UIApplication.topViewControlle
if (lat != 0 && long != 0) {
let actionSheet = UIAlertController(title: "", message: "", preferredStyle: .actionSheet)
if let add = addressURL,UIApplication.share
actionSheet.addAction(UIAlertA
UIApplication.shared.open(add)
}))
} else {
actionSheet.addAction(UIAlertA
if let url = URL(string: "https://maps.google.com/?q=@\
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}))
actionSheet.addAction(UIAlertA
let coordinate = CLLocationCoordinate2DMake(
let mapItem = MKMapItem(placemark: MKPlacemark(coordinate: coordinate, addressDictionary: nil))
mapItem.name = address != "" ? address : "Destination"
mapItem.openInMaps(
}))
}
if isSave {
actionSheet.addAction(UIAlertA
saveLocation(lat: lat, long: long, address: address)
}))
}
actionSheet.addAction(UIAlertA
if actionSheet.actions.count == 2 {
if let add = addressURL,UIApplication.share
UIApplication.shared.open(add)
}
} else {
topview.present(actionSheet, animated: true, completion: nil)
}
}
else {
if let topController = UIApplication.topViewControlle
topController.view.makeToast("error location")
}
}
}
}
Comments
Post a Comment