Skip to content

Instantly share code, notes, and snippets.

@dacur
Last active August 29, 2015 14:17
Show Gist options
  • Save dacur/5f8646244cd7e54b8cc4 to your computer and use it in GitHub Desktop.
Save dacur/5f8646244cd7e54b8cc4 to your computer and use it in GitHub Desktop.
This gist uses MapKit to display a map on the user's screen (iOS). Then, one point is added to a map. When the button is clicked, another point is added to the map. The data is from Raleigh's open data on crime. This code is a work in progress. I wanted to see if I could get the data back, and then on a button click, add it to the map. As you ca…
//
// ViewController.swift
// RaleighCrime
//
// Created by David Curtis on 3/15/15.
// Copyright (c) 2015 David Curtis. All rights reserved.
//
import UIKit
import Foundation
import MapKit
import CoreLocation
class ViewController: UIViewController, MKMapViewDelegate {
@IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
// Set starting point of map: downtown Raleigh
var latitude:CLLocationDegrees = 35.779632
var longitude:CLLocationDegrees = -78.638094
// Zoom level
var latDelta:CLLocationDegrees = 0.09
var longDelta:CLLocationDegrees = 0.09
var span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)
var location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
var region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)
mapView.setRegion(region, animated: true)
var annotation = MKPointAnnotation()
annotation.coordinate = location
annotation.title = "Raleigh"
annotation.subtitle = "Crime"
mapView.addAnnotation(annotation)
// var lat2:CLLocationDegrees = 35.779419
// var long2:CLLocationDegrees = -78.645497
// var location2:CLLocationCoordinate2D = CLLocationCoordinate2DMake(lat2, long2)
// var annotation2 = MKPointAnnotation()
// annotation2.coordinate = location2
// mapView.addAnnotation(annotation2)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func retrieveJsonFromData(data: NSData) -> AnyObject?{
println("Hello")
var error : NSError?
let jsonObject : AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments, error: &error)
if error == nil {
println("Successfully deserialized")
if jsonObject is NSDictionary{
let deserializedDictionary = jsonObject as NSDictionary
println("Deserialized JSON Dictionary = \(deserializedDictionary)")
}
else if jsonObject is NSArray{
let deserialzedArray = jsonObject as NSArray
// println("Deserialzed JSON Array = \(deserialzedArray[0])")
return deserialzedArray
}
else{
println("None")
}
} else if error != nil {
println("An error happened while deserializing JSON data")
}
return nil
}
@IBAction func buttonClick(sender: AnyObject) {
var query = "lcr=81A"
let url = NSURL(string: "https://data.raleighnc.gov/resource/csw9-dd5k.json?\(query)")
let request = NSURLRequest(URL: url!)
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { (response, data, error) -> Void in
let parsedObject: AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments, error: nil)
var newData: AnyObject? = self.retrieveJsonFromData(data)
var latitudes : [NSString] = []
var longitudes : [NSString] = []
var dataDictionary = newData? as NSArray
var length = dataDictionary.count - 1
for index in 0...length {
if var location = dataDictionary[index]["location"] as? NSDictionary{
latitudes.append(location["latitude"] as NSString)
longitudes.append(location["longitude"] as NSString)
}
}
var latDelta:CLLocationDegrees = 0.09
var longDelta:CLLocationDegrees = 0.09
var span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)
var arrayLength = latitudes.count - 1
for index in 0...arrayLength {
var newLat:CLLocationDegrees = latitudes[index].doubleValue
var newLong:CLLocationDegrees = longitudes[index].doubleValue
var newLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(newLat, newLong)
var region:MKCoordinateRegion = MKCoordinateRegionMake(newLocation, span)
self.mapView.setRegion(region, animated: true)
var annotation = MKPointAnnotation()
annotation.coordinate = newLocation
annotation.title = "Raleigh"
annotation.subtitle = "Crime"
self.mapView.addAnnotation(annotation)
}
// need to add for loop for longitudes = same as latitudes
// then add a loop to loop through the long/lat arrays and put points on map = magic
// var lat3:CLLocationDegrees = latitudes[0].doubleValue
// var long3:CLLocationDegrees = -78.638094
}
} // end button click
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment