Zoom on multiple locations using 'MKMapView' - ios

I am using the code below to retrieve data from MySQL to show multiple locations on MKMapView using Swift.
The data and locations shows on the map, but what I could not figure out is how to adjust the zoom to cover all locations in that area.
func parseJSON(_ data:Data) {
var jsonResult = NSArray()
do {
jsonResult = try JSONSerialization.jsonObject(with: data, options:JSONSerialization.ReadingOptions.allowFragments) as! NSArray
} catch let error as NSError {
print(error)
}
var jsonElement = NSDictionary()
let locations = NSMutableArray()
for i in 0 ..< jsonResult.count
{
jsonElement = jsonResult[i] as! NSDictionary
let location = LocationModel()
//the following insures none of the JsonElement values are nil through optional binding
if let evIdL = jsonElement["id"] as? String,
let evUserNameL = jsonElement["username"] as? String,
let evNotikindL = jsonElement["notikind"] as? String,
let evLatiL = jsonElement["lati"] as? String,
let evLongiL = jsonElement["longi"] as? String,
let evLocatL = jsonElement["locat"] as? String,
let evTimedateL = jsonElement["timedate"] as? String,
let evDistanceL = jsonElement["distance"] as? String
{
location.evId = evIdL
location.evUsername = evUserNameL
location.evNotikind = evNotikindL
location.evLati = evLatiL
location.evLongi = evLongiL
location.evLocat = evDistanceL
location.evTimedate = evTimedateL
location.evDisatnce = evDistanceL
location.evLocat = evLocatL
// the code to show locations
let latiCon = (location.evLati as NSString).doubleValue
let longiCon = (location.evLongi as NSString).doubleValue
let annotations = locations.map { location -> MKAnnotation in
let annotation = MKPointAnnotation()
annotation.title = evNotikindL
annotation.coordinate = CLLocationCoordinate2D(latitude:latiCon, longitude: longiCon)
return annotation
}
self.map.showAnnotations(annotations, animated: true)
self.map.addAnnotations(annotations)
}
locations.add(location)
}
DispatchQueue.main.async(execute: { () -> Void in
self.itemsDownloaded(items: locations)
})
}
I am using PHP file to connect with MySQL, as I said the code working and showing the locations but the zoom focus on one location only.

You can try
DispatchQueue.main.async {
self.map.addAnnotations(annotations)
self.map.showAnnotations(annotations, animated: true)
// make sure itemsDownloaded needs main ??
self.itemsDownloaded(items: locations)
}

Related

Showing multiple location using 'MKMapView'

I am trying to show multiple locations which saved in Mysql using the code below. The data is loading but I have no idea how to show multiple locations depending on latitude and longitude.
Mysql is connected to application via PHP file.
Here is my code, the part which I called from NSObject:
func downloadItems() {
// the download function
// return the nsuserdefaults which hold the lati and longi from the notification table
UserDefaults.standard.string(forKey: "test");
let myUrl = UserDefaults.standard.string(forKey: "test");
let urlPath: String = myUrl!
let url: URL = URL(string: urlPath)!
let defaultSession = Foundation.URLSession(configuration: URLSessionConfiguration.default)
let task = defaultSession.dataTask(with: url) { (data, response, error) in
if error != nil {
print("Failed to download data")
}else {
print("Data downloaded")
self.parseJSON(data!)
}
}
task.resume()
}
func parseJSON(_ data:Data) {
var jsonResult = NSArray()
do{
jsonResult = try JSONSerialization.jsonObject(with: data, options:JSONSerialization.ReadingOptions.allowFragments) as! NSArray
} catch let error as NSError {
print(error)
}
var jsonElement = NSDictionary()
let locations = NSMutableArray()
for i in 0 ..< jsonResult.count
{
jsonElement = jsonResult[i] as! NSDictionary
let location = LocationModel()
//the following insures none of the JsonElement values are nil through optional binding
if let evIdL = jsonElement["id"] as? String,
let evUserNameL = jsonElement["username"] as? String,
let evNotikindL = jsonElement["notikind"] as? String,
let evLatiL = jsonElement["lati"] as? String,
let evLongiL = jsonElement["longi"] as? String,
let evLocatL = jsonElement["locat"] as? String,
let evTimedateL = jsonElement["timedate"] as? String,
let evDistanceL = jsonElement["distance"] as? String
{
location.evId = evIdL
location.evUsername = evUserNameL
location.evNotikind = evNotikindL
location.evLati = evLatiL
location.evLongi = evLongiL
location.evLocat = evDistanceL
location.evTimedate = evTimedateL
location.evDisatnce = evDistanceL
location.evLocat = evLocatL
}
locations.add(location)
}
DispatchQueue.main.async(execute: { () -> Void in
self.delegate.itemsDownloaded(items: locations)
})
}
}
I have no idea how to show few location on map.
try this code....
var locations = NSMutableArray()
var mapView = GMSMapView()
for i in 0..< location.count{
let obj = location[i]
lat = obj["lati"] as? Double
lng = obj["longi"] as? Double
let markerPoint = GMSMarker()
markerPoint.position = CLLocationCoordinate2D(latitude: lat!, longitude: lng!)
markerPoint.iconView = self.avtarImage() // Your image name
markerPoint.map = mapView // your mapview object name
markerPoint.zIndex = Int32(i)
markerPoint.infoWindowAnchor = CGPoint(x: 0, y: 0)
markerPoint.accessibilityLabel = String(format: "%d", i)
}

How to show an user Location on Live. like Uber IOS Swift

I am new to swift and Maps. I am facing problem with displaying user live location. I have to display user location like Uber and Ola. I am getting array of coordinates from server.
This is the way i am fetching coordinates from server. I want to show moving user location. see following my code.
func SetUpMapsUI()
{
AdminAPIManager.sharedInstance.getAdminRunningStatusFromURL(){(resignationsJson)-> Void in
let swiftyJsonVar = JSON(resignationsJson)
let status = swiftyJsonVar["status"].rawString() as! String
print("status",status)
let message = swiftyJsonVar["message"].rawString()
if status.isEqual("0"){
if (message?.isEqual("No trips done so far."))!
{
self.mapViewBottomCons.constant = 0
}else
{
self.mapViewBottomCons.constant = 70
}
self.Bottom_view.isHidden = true
Toast.short(message: message as! String)
return
}
let busVar = swiftyJsonVar["bus_details"].rawString()!
let jsonData = busVar.data(using: .utf8)!
let LocationArray = try? JSONSerialization.jsonObject(with: jsonData, options: []) as! Array< Any>
for data in LocationArray!
{
let dic = data as! NSDictionary
guard let lat = dic.value(forKey: "latitude") as? Double else {
return
}
print("latlatlatlat",lat)
guard let lon = dic.value(forKey: "longitude") as? Double else {
return
}
print("longitude",lon)
self.arrayMapPath.append(NewMapPath(lat: Double(lat), lon: Double(lon)))
}
if self.arrayMapPath.count > 0
{
self.drawPathOnMap()
}
}
here is library that does that functionality it is written for both swift and ObjC..
ARCarMovement
here is a Stackoverflow SO answer

Using Firebase Location Coordinates - Cannot Load Annotations

I am currently trying to plug in a users coordinates from firebase into my function that will then represent these coordinates on my map. I don't know if this has something to do with me changing the coordinates from a string to a double.
An issue could also be that swift is not recognizing mapView as a part of the viewcontroller's class. I used my #IBOutlet weak var map: MKMapView! as a replacement in hopes that the annotations would show.
func retrieveUsers(){
let ref = Database.database().reference()
ref.child("users").queryOrderedByKey().observeSingleEvent(of: .value, with: { snapshot in
let users = snapshot.value as! [String: AnyObject]
self.user.removeAll()
for (_,value) in users {
if let uid = value["uid"] as? String {
if uid != Auth.auth().currentUser!.uid {
let userToShow = User()
if let fullName = value["full name"] as? String, let imagePath = value["urlToImage"] as? String
,let snap = snapshot.value as? [String: Any],
let userLongitude = Double(snap["long"] as! String),
let userLatitude = Double(snap["lat"] as! String)
{
let otherUserLocation:CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: userLatitude, longitude: userLongitude)
let userAnnotation = MKPointAnnotation()
userAnnotation.coordinate = otherUserLocation
userAnnotation.title = fullName
DispatchQueue.main.async {
self.map.addAnnotation(userAnnotation)
}
}

How to get accurate route for map

I'm trying to build a route using Google Directions API. It returns an array of waypoints, however, the number of waypoints is not enough to build a smoothed route, and as the result I receive quite inaccurate route what is super crucial and inappropriate for transport application. I also tried HERE maps, almost the same result.
Is there any other services that may build more accurate route, or any solution applicable to the Google Directions API?
Here is the function:
public func getWaypoints(startLocation: [Double]!, endLocation: [Double]!, mode:String? = "walking", lang:String? = "en") -> [Dictionary<String,Any>] {
var resultedArray:[Dictionary<String,Any>] = []
let routeGM = Just.get("https://maps.googleapis.com/maps/api/directions/json?", params: ["origin":"\(startLocation[0]),\(startLocation[1])", "destination": "\(endLocation[0]),\(endLocation[1])", "mode": mode!, "key":self.KEY, "language": lang!])
if let _ = routeGM.error {
} else {
do {
if let data = jsonToNSData(routeGM.json! as AnyObject), let jsonData = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? NSDictionary {
let status = jsonData["status"] as! String
if(status == "OK") {
let results = jsonData["routes"] as! Array<Dictionary<String, AnyObject>>
let legs = results[0]["legs"] as! Array<Dictionary<String, AnyObject>>
let steps = legs[0]["steps"] as! Array<Dictionary<String, AnyObject>>
for i in 0...steps.count-1 {
let item = steps[i]
let start = item["start_location"] as! Dictionary<String,Any>
let end = item["end_location"] as! Dictionary<String,Any>
resultedArray.append(["start_location_lat": start["lat"]!,"start_location_lng": start["lng"]!,"end_location_lat": end["lat"]!,"end_location_lng": end["lng"]!])
}
}
else {
print("not found")
}
}
} catch {
print(error)
}
}
return resultedArray
}
Calling function:
func buildRoute(startCoord:[Double]!, endCoord:[Double]!) {
DispatchQueue.global(qos: .background).async {
let route:[Dictionary<String,Any>] = self.GMSRequest.getWaypoints(startLocation: startCoord,endLocation: endCoord, mode: "driving")
DispatchQueue.main.async {
let path = GMSMutablePath()
for item in route {
path.add(CLLocationCoordinate2D(latitude: item["start_location_lat"]! as! CLLocationDegrees, longitude: item["start_location_lng"]! as! CLLocationDegrees))
path.add(CLLocationCoordinate2D(latitude: item["end_location_lat"]! as! CLLocationDegrees, longitude: item["end_location_lng"]! as! CLLocationDegrees))
}
// Create the polyline, and assign it to the map.
self.polyline.path = path
self.polyline.strokeColor = Styles.colorWithHexString("#3768CD")
self.polyline.strokeWidth = 3.0
self.polyline.geodesic = true
self.polyline.map = self.mapView
}
}
}

Is it possible to not take over screen? Google Places IOS swift

Below codes from Google only return a place if I pick one from the list like the one I attached.
My question:
Is there any function available for me to store all the place's detail in a given coordinate? For example, if I have a coordinate of (51.5108396, -0.0922251), how can I get all the information of nearby places? I am not familiar with Json. Is there any example close to what I want? Thanks a lot.
This function placesClient.currentPlaceWithCallback is somehow close to what I want but it cannot use custom coordinate because it uses user's current coordinate.
//https://developers.google.com/places/ios-api/placepicker
let center = CLLocationCoordinate2DMake(51.5108396, -0.0922251)
let northEast = CLLocationCoordinate2DMake(center.latitude + 0.001, center.longitude + 0.001)
let southWest = CLLocationCoordinate2DMake(center.latitude - 0.001, center.longitude - 0.001)
let viewport = GMSCoordinateBounds(coordinate: northEast, coordinate: southWest)
let config = GMSPlacePickerConfig(viewport: viewport)
let placePicker = GMSPlacePicker(config: config)
placePicker?.pickPlaceWithCallback({ (place: GMSPlace?, error: NSError?) -> Void in
if let error = error {
print("Pick Place error: \(error.localizedDescription)")
return
}
if let place = place {
print("Place name \(place.name)")
print("Place address \(place.formattedAddress)")
print("Place attributions \(place.attributions)")
} else {
print("No place selected")
}
})
Fetching nearby places using google maps
Something is changed due to upgraded iOS version.
complete changed code
func fetchPlacesNearCoordinate(coordinate: CLLocationCoordinate2D, radius: Double, types:[String]) {
var urlString = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=\("your api key")&location=\(coordinate.latitude),\(coordinate.longitude)&radius=\(radius)&rankby=prominence&sensor=true"
let typesString = types.count > 0 ? types.joinWithSeparator("|") : "food"
urlString += "&types=\(typesString)"
urlString = urlString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
let session = NSURLSession.sharedSession()
let placesTask = session.dataTaskWithURL(NSURL(string: urlString)!) {data, response, error in
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
if let jsonResult = (try? NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)) as? NSDictionary {
let returnedPlaces: NSArray? = jsonResult["results"] as? NSArray
if returnedPlaces != nil {
for index in 0..<returnedPlaces!.count {
if let returnedPlace = returnedPlaces?[index] as? NSDictionary {
var placeName = ""
var latitude = 0.0
var longitude = 0.0
if let name = returnedPlace["name"] as? NSString {
placeName = name as String
}
if let geometry = returnedPlace["geometry"] as? NSDictionary {
if let location = geometry["location"] as? NSDictionary {
if let lat = location["lat"] as? Double {
latitude = lat
}
if let lng = location["lng"] as? Double {
longitude = lng
}
}
}
print("index", index, placeName, latitude, longitude)
}
}
}
}
}
placesTask.resume()
}

Resources