how to set up array for multi annotations with swift - ios

How should the array below be set. Im trying to add multiple annotations onto my map. I was able to find the code below on stackoverflow but they did not show how to set up the array.
var objects = [
//how should the array be setup here
]
for objecters in objects!{
if let latit = objecters["Coordinates"]["Latitude"]{
self.latitudepoint = latit as! String
self.map.reloadInputViews()
}
else {
continue
}
if let longi = objecters["Coordinates"]["Longitude"]{
self.longitudepoint = longi as! String
self.map.reloadInputViews()
}
else {
continue
}
var annotation = MKPointAnnotation()
var coord = CLLocationCoordinate2D(latitude: Double(self.latitudepoint)!,longitude: Double(self.longitudepoint)!)
mapView.addAnnotation(annotation)
}

You could do, for example:
let locations = [
["title": "New York, NY", "latitude": 40.713054, "longitude": -74.007228],
["title": "Los Angeles, CA", "latitude": 34.052238, "longitude": -118.243344],
["title": "Chicago, IL", "latitude": 41.883229, "longitude": -87.632398]
]
for location in locations {
let annotation = MKPointAnnotation()
annotation.title = location["title"] as? String
annotation.coordinate = CLLocationCoordinate2D(latitude: location["latitude"] as! Double, longitude: location["longitude"] as! Double)
mapView.addAnnotation(annotation)
}
Or, alternatively, use a custom type, e.g.:
struct Location {
let title: String
let latitude: Double
let longitude: Double
}
let locations = [
Location(title: "New York, NY", latitude: 40.713054, longitude: -74.007228),
Location(title: "Los Angeles, CA", latitude: 34.052238, longitude: -118.243344),
Location(title: "Chicago, IL", latitude: 41.883229, longitude: -87.632398)
]
for location in locations {
let annotation = MKPointAnnotation()
annotation.title = location.title
annotation.coordinate = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)
mapView.addAnnotation(annotation)
}
Or you can replace that for loop with map:
let annotations = locations.map { location -> MKAnnotation in
let annotation = MKPointAnnotation()
annotation.title = location.title
annotation.coordinate = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)
return annotation
}
mapView.addAnnotations(annotations)

Related

Swift 4: multiple marker Google Map not display

I'm developing an app that have a multiple location/marker.
I created a struct
let states = [
State(name: "Zoo 1", long: 2.276537, lat: 102.2989),
State(name: "Zoo 2", long: 2.2772141, lat: 102.2984333),
// the other 51 states here...
]
and try a looping but the marker did not display
for state in states {
let state_marker = GMSMarker()
state_marker.position = CLLocationCoordinate2D(latitude: state.lat, longitude: state.long)
state_marker.title = state.name
state_marker.snippet = "Hey, this is \(state.name)"
state_marker.map = mapView
}
when i do like below, it is working. but i know it's not a better way
let state_marker = GMSMarker()
state_marker.position = CLLocationCoordinate2D(latitude: 2.276622, longitude: 102.2989)
state_marker.title = "Zoo 1"
state_marker.snippet = "Hey, this is Zoo 1"
state_marker.map = mapView
let state_marker1 = GMSMarker()
state_marker1.position = CLLocationCoordinate2D(latitude: 2.2772141, longitude: 102.2984333)
state_marker1.title = "Zoo 2"
state_marker1.snippet = "Hey, this is Zoo 2"
state_marker1.map = mapView
Does anyone know what is wrong in my code?
You swapped the lat and Lon in first try , it works in second as longitude is 102.2989 , but in first you swapped them
for state in states {
let state_marker = GMSMarker()
state_marker.position = CLLocationCoordinate2D(latitude: state.lat, longitude: state.long)
state_marker.title = state.name
state_marker.snippet = "Hey, this is \(state.name)"
state_marker.map = mapView
}
So change array to this
let states = [
State(name: "Zoo 1", long:102.2989 , lat: 2.276537),
State(name: "Zoo 2", long:102.2984333 , lat: 2.2772141),
// the other 51 states here...
]
Edit :
Latitude measurements range from 0° to (+/–)90°. Longitude measures how far east or west of the prime meridian a place is located. The prime meridian runs through Greenwich, England. Longitude measurements range from 0° to (+/–)180°

show distance on annotation from current location to another point

I want to show my total distance from my current location to another point and I want the distance shows in subtitle of Annotation.
Here is my code but it doesn't work :
func shopDeal () {
var inKm = ""
let locationsq =
[["title": "Shop Deal","subtitle": "\(inKm) km", "latitude": 31.352792, "longitude": 74.253201],
["title": "Shop Deal", "subtitle": "\(inKm) km", "latitude": 31.403563, "longitude": 74.258200]]
// Span
for location in locationsq {
let annotation = MKPointAnnotation()
annotation.title = location["title"] as? String
annotation.subtitle = location["subtitle"] as? String
annotation.coordinate = CLLocationCoordinate2D(latitude: location["latitude"] as! Double, longitude: location["longitude"] as! Double)
mapView.addAnnotation(annotation)
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let myCurrentLoc = locations[locations.count - 1]
let coor1 = CLLocation(latitude: annotation.coordinate.latitude, longitude: annotation.coordinate.longitude)
let coor2 = CLLocation (latitude: myCurrentLoc.coordinate.latitude, longitude: myCurrentLoc.coordinate.longitude )
let distanceInMeters:CLLocationDistance = coor1.distanceFromLocation(coor2)
inKm = String(format: "%.2f", distanceInMeters / 1000)
print("\(inKm) km")
}
}
mapView.delegate = self
}
Any help is highly appreciated. Thanks in advance
Finding Distance between two points(LAT and LONG) on Map :
let coordinate1 = CLLocation(latitude: 5.0, longitude: 5.0)
let coordinate2 = CLLocation(latitude: 5.0, longitude: 3.0)
//Decalare distanceInMeters as global variables so that you can show distance on subtitles
let distanceInMeters = coordinate1.distance(from: coordinate2)
Now if you want to show distance in subtitle of annotation then :
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
return nil
}
let reuseId = "pinIdentifier"
var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
if pinView == nil {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView!.canShowCallout = true
}
else {
pinView!.annotation = annotation
}
//Adding Subtitle text
let subtitleView = UILabel()
//Decalare distanceInMeters as global variables so that you can show distance on subtitles
subtitleView.text = "\(distanceInMeters)"
pinView!.detailCalloutAccessoryView = subtitleView
return pinView
}
Feel free to comment if further any issue
Try this!
// 1. Get Source & Destination coordinates (Hard-cored here)
let locSource : CLLocation = CLLocation.init(latitude: 17.428031, longitude: 78.377837)
let locDestination : CLLocation = CLLocation.init(latitude: 17.441879, longitude: 78.429990)
// 2. Find distance between the Source & Destination
let nDistanceInMeters : CLLocationDistance = locSource.distance(from: locDestination)
let nDistanceInKiloMeters : CLLocationDistance = nDistanceInMeters / 1000.0;
// 3. Convert to String
let strDistanceInMeters : String = String.init(format: "%f", nDistanceInMeters)
let strDistanceInKM : String = String.init(format: "%f", nDistanceInKiloMeters)
// 4. Assign the calculated distance to your label,
// Probably in mapView(mapView:viewForAnnotation annotation:
lblDistanceLabelOnAnnotationView.text = strDistanceInKM
Hope that helps!

Create multiple markers using Google iOS SDK

I am a newbie in Swift. I was ale to get 2 markers on Google Maps:
import UIKit
import GoogleMaps
class ViewController: UIViewController {
// You don't need to modify the default init(nibName:bundle:) method.
override func loadView() {
let camera = GMSCameraPosition.cameraWithLatitude(37.0902, longitude: -95.7129, zoom: 3.0)
let mapView = GMSMapView.mapWithFrame(CGRect.zero, camera: camera)
mapView.myLocationEnabled = true
view = mapView
let state_marker = GMSMarker()
state_marker.position = CLLocationCoordinate2D(latitude: 61.370716, longitude: -152.404419)
state_marker.title = "Alaska"
state_marker.snippet = "Hey, this is Alaska"
state_marker.map = mapView
let state_marker1 = GMSMarker()
state_marker1.position = CLLocationCoordinate2D(latitude: 32.806671, longitude: -86.791130)
state_marker1.title = "Alabama"
state_marker1.snippet = "Hey, this is Alabama"
state_marker1.map = mapView
}
}
I need to add 51 more markers to different latitude and longitude for each state with different title and snippet.
I can probably just copy this block 51 times but is there a way to optimize this code?
You should create a struct like this:
struct State {
let name: String
let long: CLLocationDegrees
let lat: CLLocationDegrees
}
Then create an array of this struct in your VC:
let states = [
State(name: "Alaska", long: -152.404419, lat: 61.370716),
State(name: "Alabama", long: -86.791130, lat: 32.806671),
// the other 51 states here...
]
Now you can just loop through the array, adding markers in each iteration:
for state in states {
let state_marker = GMSMarker()
state_marker.position = CLLocationCoordinate2D(latitude: state.lat, longitude: state.long)
state_marker.title = state.name
state_marker.snippet = "Hey, this is \(state.name)"
state_marker.map = mapView
}
You might also want to add a dictionary that stores the names of the states as keys and the corresponding GMSMarker as value. This way, you can modify the markers later.
var markerDict: [String: GMSMarker] = [:]
override func loadView() {
for state in states {
let state_marker = GMSMarker()
state_marker.position = CLLocationCoordinate2D(latitude: state.lat, longitude: state.long)
state_marker.title = state.name
state_marker.snippet = "Hey, this is \(state.name)"
state_marker.map = mapView
markerDict[state.name] = state_marker
}
}

Turn by turn navigation to annotation with Apple Maps

I have one Annotation on my map showing a business location and one button that says get directions, I'm struggling to get the button to open Apple Maps for me with directions to the Annotations location. Here is the code I have done so far:
import UIKit
import MapKit
class FourthViewController: UIViewController , MKMapViewDelegate {
#IBOutlet weak var map: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
let latitude: CLLocationDegrees = 54.647115
let longitude: CLLocationDegrees = -6.659070
let lanDelta: CLLocationDegrees = 0.05
let lonDelta: CLLocationDegrees = 0.05
let span = MKCoordinateSpan(latitudeDelta: lanDelta, longitudeDelta: lonDelta)
let coordinates = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
let region = MKCoordinateRegion(center: coordinates, span: span)
map.setRegion(region, animated: true)
let annotation = MKPointAnnotation()
annotation.title = "Pose Beauty Salon"
annotation.subtitle = "100 Moneyhaw Road"
annotation.coordinate = coordinates
map.addAnnotation(annotation)
}
#IBAction func mapType(_ sender: AnyObject) {
switch (sender.selectedSegmentIndex) {
case 0:
map.mapType = .standard
case 1:
map.mapType = .satellite
default: // or case 2
map.mapType = .hybrid
}
}
#IBAction func getDirections(_ sender: AnyObject) {
}
}
I've also seen annotations when clicked on that shows more info such as business names, addresses, phone numbers, and URLs is this hard to add also?
This is the code I used to resolve the issue:
let latitude: CLLocationDegrees = 54.647115
let longitude: CLLocationDegrees = -6.659070
let url = URL(string: "https://www.posebeautysalon.com")
let regionDistance:CLLocationDistance = 10000
let coordinates = CLLocationCoordinate2DMake(latitude, longitude)
let regionSpan = MKCoordinateRegionMakeWithDistance(coordinates, regionDistance, regionDistance)
let options = [
MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: regionSpan.center),
MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: regionSpan.span)
]
let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
let mapItem = MKMapItem(placemark: placemark)
mapItem.name = "Pose Beauty Salon"
mapItem.phoneNumber = "+442886737777"
mapItem.url = url
mapItem.openInMaps(launchOptions: options)

How to populate longitude latitude from JSON file into array

I need to find the nearest cities around my current location. Would you advice me how should I populate the coordinates into an array in my project and how to calculate the distance between my location and the nearest around me. I have to display the distance in (KM) and the city name ,so that the user can choose which city best fits for him.I also need to call the coordinates and the names in my code from a json file
My JSON File is:
{
"City A": {
"Position": {
"Longitude": 9.96233,
"Latitude": 49.80404
}
},
"City B": {
"Position": {
"Longitude": 6.11499,
"Latitude": 50.76891
}
},
"City C": {
"Position": {
"Longitude": 6.80592,
"Latitude": 51.53548
}
and my function so far:
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userlocation:CLLocation = locations[0] as CLLocation
manager.stopUpdatingLocation()
let location = CLLocationCoordinate2D(latitude: userlocation.coordinate.latitude, longitude: userlocation.coordinate.longitude)
let span = MKCoordinateSpanMake(0.5, 0.5)
let region = MKCoordinateRegion(center: location, span: span)
Mapview.setRegion(region, animated: true)
let locations = ["42.6977,23.3219","43.6977,24.3219"]
let distanceMeters = userlocation.distanceFromLocation(CLLocation(latitude: 42.6977,longitude: 23.3219))
let distanceKilometers = distanceMeters / 1000.00
let roundedDistanceKilometers = String(Double(round(100 * distanceKilometers) / 100)) + " km"
// var distanceMeter = NSString(format: "\(distanceKilometers)km")
Assuming the JSON file is in the application bundle and is named cities.json first create a custom struct City
struct City {
let name : String
let location : CLLocation
}
Then in your controller declare an array cities
var cities = [City]()
To populate the array deserialize the JSON and extract the data
guard let url = NSBundle.mainBundle().URLForResource("cities", withExtension: "json"), jsonData = NSData(contentsOfURL: url) else { return }
do {
let jsonObject = try NSJSONSerialization.JSONObjectWithData(jsonData, options: []) as! [String:AnyObject]
for (cityName, position) in jsonObject {
let coordinates = position["Position"] as! [String:CLLocationDegrees]
let longitude = coordinates["Longitude"]!
let latitude = coordinates["Latitude"]!
let location = CLLocation(latitude: latitude,longitude: longitude)
let city = City(name: cityName, location: location)
cities.append(city)
// print(cities)
}
} catch let error as NSError {
print(error)
}
To find the nearest location you could sort the array by distance
let nearestCity = cities.sort({ userLocation.distanceFromLocation($0.location) < userLocation.distanceFromLocation($1.location) }).first

Resources