Change color pin iOS 9 Mapkit - ios

I don't know how to change the code for the pin color in iOS 9 (because lately Apple changed the code for it), and I'm still new in Swift. So, I don't know how to integrate pinTintColor in my code now.
Please find below my code:
import UIKit
import MapKit
class ViewController: UIViewController, MKMapViewDelegate {
#IBOutlet var map: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
let annotation = MKPointAnnotation()
let latitude:CLLocationDegrees = 40.5
let longitude:CLLocationDegrees = -74.6
let latDelta:CLLocationDegrees = 150
let lonDelta:CLLocationDegrees = 150
let span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)
let location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
let region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)
map.setRegion(region, animated: false)
annotation.coordinate = location
annotation.title = "Niagara Falls"
annotation.subtitle = "One day bla bla"
map.addAnnotation(annotation)
}
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
// simple and inefficient example
let annotationView = MKPinAnnotationView()
annotationView.pinColor = .Purple
return annotationView
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

pinColor is deprecated in iOS 9, use pinTintColor instead.
Example:
let annotationView = MKPinAnnotationView()
annotationView.pinTintColor = UIColor.purpleColor()
Although the OP specifically asks for iOS 9, the following could ensure that the "non-deprecated" method prior to iOS 9 could be called:
if #available(iOS 9, *) {
annotationView.pinTintColor = UIColor.purpleColor()
} else {
annotationView.pinColor = .Purple
}
If your minimal target is iOS 9 as you specifically ask here, the above would then be redundant - Xcode would let you know that with a warning, as well, for your information.

Related

Can i use the same map with the same location in another View Controller?

In my app the user can search for a location and it adds a pin. I want a next button to go to another view controller and show the exact same map but in a smaller version. How can I move the exact same map to another view controller?
import UIKit
import MapKit
protocol HandleMapSearch {
func dropPinZoomIn(placemark:MKPlacemark)
}
class ViewController: UIViewController {
let locationManager = CLLocationManager()
var resultSearchController:UISearchController? = nil
var selectedPin:MKPlacemark? = nil
#IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.requestLocation()
//
let locationSearchTable = storyboard!.instantiateViewControllerWithIdentifier("LocationSearchTable") as! LocationSearchTable
resultSearchController = UISearchController(searchResultsController: locationSearchTable)
resultSearchController?.searchResultsUpdater = locationSearchTable
//
let searchBar = resultSearchController!.searchBar
searchBar.sizeToFit()
searchBar.placeholder = "Search for places"
navigationItem.titleView = resultSearchController?.searchBar
//
resultSearchController?.hidesNavigationBarDuringPresentation = false
resultSearchController?.dimsBackgroundDuringPresentation = true
definesPresentationContext = true
//
locationSearchTable.mapView = mapView
//
locationSearchTable.handleMapSearchDelegate = self
//
let button = UIButton(type: UIButtonType.System) as UIButton
button.frame = CGRectMake(100, 100, 100, 50)
button.backgroundColor = UIColor.greenColor()
button.setTitle("Button", forState: UIControlState.Normal)
button.addTarget(self, action: Selector("Action:"), forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(button)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension ViewController : CLLocationManagerDelegate {
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == .AuthorizedWhenInUse {
locationManager.requestLocation()
}
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.first {
let span = MKCoordinateSpanMake(0.05, 0.05)
let region = MKCoordinateRegion(center: location.coordinate, span: span)
mapView.setRegion(region, animated: true)
}
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print("error:: \(error)")
}
}
extension ViewController: HandleMapSearch {
func dropPinZoomIn(placemark:MKPlacemark){
// cache the pin
selectedPin = placemark
// clear existing pins
mapView.removeAnnotations(mapView.annotations)
let annotation = MKPointAnnotation()
annotation.coordinate = placemark.coordinate
annotation.title = placemark.name
if let city = placemark.locality,
let state = placemark.administrativeArea {
annotation.subtitle = "\(city) \(state)"
}
mapView.addAnnotation(annotation)
let span = MKCoordinateSpanMake(0.05, 0.05)
let region = MKCoordinateRegionMake(placemark.coordinate, span)
mapView.setRegion(region, animated: true)
}
}
extension ViewController : MKMapViewDelegate {
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
//return nil so map view draws "blue dot" for standard user location
return nil
}
let reuseId = "pin"
var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView?.pinTintColor = UIColor.orangeColor()
pinView?.canShowCallout = true
pinView?.rightCalloutAccessoryView = UIButton(type: UIButtonType.DetailDisclosure) as UIButton
return pinView
}
func mapView(mapView: MKMapView, annotationView: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
if control == annotationView.rightCalloutAccessoryView {
print("Disclosure Pressed!")
}
}
}
you can pass annotation or lat long to another view controller while performing segue or push new controller. If you are using segue then you can use prepare for segue method.
Second thing you can make a custom class for mapview. and can put some properties in it like annotation or lat long then create object of this class and set this property so according that properties this class return map view (make method accordingly). you can use class in many viewcontroller not only in just two.
Update as per comment :
refer this link to know how to push new view controller.
and refer this storyboard segue tutorial. Actually it concepts and not possible to explain here everything if you have some error in code then i can solve here but for learn whole concepts you should follows different tutorials and notes. do research. google it you will find many links.
hope this will help :)

Swift Mapkit Received memory warning

Im developing ipad app and looking memory using good all viewcontrollers but my MapDetailViewController gives me Received memory warning how can i resolve it .
All viewcontrollers use my app max 34.MB memory but MapDetailViewController using 100 MB More. My codes under below what is wrong ?
import UIKit
import MapKit
class MapDetailViewController: UIViewController,MKMapViewDelegate {
#IBOutlet var mapView:MKMapView!
#IBOutlet weak var mapArea: DesignableView!
#IBOutlet weak var nameSales: DesignableLabel!
var showmapListcomming = String()
override func viewDidLoad() {
super.viewDidLoad()
// Show Map
let myString: String = showmapListcomming
var myStringArr = myString.componentsSeparatedByString(",")
nameSales.text = myStringArr[0]
let annotation = MKPointAnnotation()
let latitude:CLLocationDegrees = (myStringArr[1] as NSString).doubleValue
let longitude:CLLocationDegrees = (myStringArr[2] as NSString).doubleValue
let latDelta:CLLocationDegrees = 150
let lonDelta:CLLocationDegrees = 150
let span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)
let location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
let region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)
mapView.setRegion(region, animated: false)
annotation.coordinate = location
annotation.title = myStringArr[0]
annotation.subtitle = "User"
mapView.addAnnotation(annotation)
}
// MARK: - MKMapViewDelegate methods
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
let annotationView = MKPinAnnotationView()
return annotationView
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

How can I add a button to MKPointAnnotation and open Maps?

I would have to add a button for my particular point of entry , where I click to open the Maps app already passing the coordinates of the selected point. Does anyone know how to do? my code is this. The image is what I would have.
My code:
import Foundation
import UIKit
import MapKit
import CoreLocation
class PuntiVenditaController: UIViewController, MKMapViewDelegate {
#IBOutlet weak var menuButton:UIBarButtonItem!
#IBOutlet weak var mapView: MKMapView!
let regionRadius: CLLocationDistance = 1000
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
if revealViewController() != nil {
menuButton.target = revealViewController()
menuButton.action = "revealToggle:"
view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
let location = CLLocationCoordinate2D(latitude: 41.225891, longitude: 16.291489)
let span = MKCoordinateSpanMake(0.05, 0.05)
let region = MKCoordinateRegion(center: location, span: span)
mapView.setRegion(region, animated: true)
mapView.showsPointsOfInterest = false
mapView.showsUserLocation = true
displayMarkers()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func centerMapOnLocation(location: CLLocation) {
let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate,
regionRadius * 2.0, regionRadius * 2.0)
mapView.setRegion(coordinateRegion, animated: true)
}
func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {
if control == view.rightCalloutAccessoryView{
println(view.annotation.title) // annotation's title
println(view.annotation.subtitle) // annotation's subttitle
//Perform a segue here to navigate to another viewcontroller
// On tapping the disclosure button you will get here
}
}
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
println("viewForannotation")
if annotation is MKUserLocation {
//return nil
return nil
}
let reuseId = "pin"
var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
if pinView == nil {
//println("Pinview was nil")
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView!.canShowCallout = true
pinView!.animatesDrop = true
}
var button = UIButton.buttonWithType(UIButtonType.DetailDisclosure) as! UIButton // button with info sign in it
pinView?.rightCalloutAccessoryView = button
return pinView
}
func displayMarkers() -> Void
{
let annotationView = MKAnnotationView()
// Adding button here wont do anything so remove these two lines
let detailButton: UIButton = UIButton.buttonWithType(UIButtonType.DetailDisclosure) as! UIButton
annotationView.rightCalloutAccessoryView = detailButton
// For adding button we have to use a method named as viewForAnnotation
let annotation = MKPointAnnotation()
let name = "Title"
let latitude = 41.225891
let longitude = 16.291489
annotation.title = name
var markerLatitude: Double = latitude
var markerLongitude: Double = longitude
let location = CLLocationCoordinate2D(latitude: markerLatitude, longitude: markerLongitude)
annotation.coordinate = location
annotation.subtitle = "Subtitle"
mapView.addAnnotation(annotation)
}
}
You can open an URL to open the Maps App at a specific point/address:
https://developer.apple.com/library/ios/featuredarticles/iPhoneURLScheme_Reference/MapLinks/MapLinks.html
But I don't think, you can design a custom marker within the Maps App

No Labels being displayed in MapKit

Could someone let me know why my labels are not being displayed. I am running a loop thru an array with coordinates in it. Its displaying my 3 pins with 1 being green, two blue which is what i want but my labels are not being displayed, any ideas?
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
#IBOutlet weak var maps: MKMapView!
var locationManager = CLLocationManager()
var flag = true
override func viewDidLoad() {
super.viewDidLoad()
let CoordinatesArray = ["blah,-blah, 11:45","blah, -blah,00:00", "blah,-blah, 12:45"];
self.maps.delegate = self
sendPoints(CoordinatesArray);
}
func sendPoints(array:[String]){
let latDelta:CLLocationDegrees = 0.015 //difference of lats from one side of screen to another
let longDelta:CLLocationDegrees = 0.015 //difference of lats from one side of screen to another
let span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)
for (var i=0;i<array.count;i++){
var separateComma = array[i].componentsSeparatedByString(",")
var location:CLLocationCoordinate2D
if(flag){
location = CLLocationCoordinate2DMake(separateComma[0].doubleValue,separateComma[1].doubleValue)
let length:CLLocationDistance = 200
let cir:MKCircle = MKCircle(centerCoordinate: location, radius: length)
maps.addOverlay(cir)
}else{
location = CLLocationCoordinate2DMake(separateComma[0].doubleValue,separateComma[1].doubleValue)
}
let point = MKPointAnnotation()
point.title = "Home"
point.subtitle = "time for home"
point.coordinate = location
maps.addAnnotation(point)
maps.selectAnnotation(point, animated: true)
maps.setRegion(MKCoordinateRegionMake(point.coordinate, MKCoordinateSpanMake(latDelta,longDelta)), animated: true)
}
}
func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer {
let overlayRenderer : MKCircleRenderer = MKCircleRenderer(overlay: overlay);
overlayRenderer.lineWidth = 150
overlayRenderer.fillColor = UIColor.blueColor()
overlayRenderer.alpha = 0.15
return overlayRenderer
}
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
if (annotation.isKindOfClass(MKUserLocation)){
return nil
}
var myPin = mapView.dequeueReusableAnnotationViewWithIdentifier("MyIdentifier") as? MKPinAnnotationView
if myPin != nil {
return myPin
}
myPin = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "MyIdentifier")
if(flag){
myPin?.pinTintColor = UIColor.greenColor()
}else{
myPin?.pinTintColor = UIColor.blueColor()
}
flag = false;
return myPin
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension String {
var doubleValue: Double {
return (self as NSString).doubleValue
}
}
You have to set canShowCallout property.
myPin = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "MyIdentifier")
myPin?.canShowCallout = true
In my case I was delegating the map before being called.
So... that'd be the mistake I was facing.
self.m_map = MKMapView()
self.m_map?.delegate = self
Hope it helps somebody :-)

Adding a button to MKPointAnnotation?

I just wrote few lines of code and got stuck trying add a detail button to my annotation point, I don't know how. Does anyone know how to do that? The image below shows what I want to achieve. Thanks!
http://i.stack.imgur.com/kK1Ox.jpg
Im trying to get the big i in the circle in my annotation.
Right now I just have an annotation with a title and a subtitle, which looks like below.
http://imgur.com/L3VMe5l.png
Also, if you know, when a user clicks on the i in the circle, how do i bring them to a new segue?
Thanks for your help.
import UIKit
import MapKit
class PropertyMasterViewConroller: UIViewController, MKMapViewDelegate {
#IBOutlet weak var nMapView: MKMapView!
#IBOutlet weak var testButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
//Location of Boston College
let initLocation = CLLocationCoordinate2D(
latitude: 42.335992,
longitude: -71.167333
)
//Span of the Map
let span = MKCoordinateSpanMake(0.1, 0.1)
let region = MKCoordinateRegion(center: initLocation, span: span)
nMapView.setRegion(region, animated: true)
addAnnotations()
testButton.setTitle("Test Button", forState: .Normal)
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.setBackgroundImage(UIImage(named:"nav.png"), forBarMetrics: .Default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.translucent = true
}
// When user taps on the disclosure button you can perform a segue to navigate to another view controller
func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {
if control == view.rightCalloutAccessoryView{
println(view.annotation.title) // annotation's title
println(view.annotation.subtitle) // annotation's subttitle
//Perform a segue here to navigate to another viewcontroller
// On tapping the disclosure button you will get here
}
}
// Here we add disclosure button inside annotation window
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
println("viewForannotation")
if annotation is MKUserLocation {
//return nil
return nil
}
let reuseId = "pin"
var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
if pinView == nil {
//println("Pinview was nil")
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView!.canShowCallout = true
pinView!.animatesDrop = true
}
var button = UIButton.buttonWithType(UIButtonType.DetailDisclosure) as UIButton // button with info sign in it
pinView?.rightCalloutAccessoryView = button
return pinView
}
func addAnnotations() -> Void {
let annotationView = MKAnnotationView()
let detailButton: UIButton = UIButton.buttonWithType(UIButtonType.DetailDisclosure) as UIButton
annotationView.rightCalloutAccessoryView = detailButton
//1070 Beacon
let annotation = MKPointAnnotation()
annotation.setCoordinate(CLLocationCoordinate2D (latitude: 42.345532,longitude: -71.109526))
annotation.title = "1070 Beacon Street"
annotation.subtitle = "Brookline, MA 02446"
nMapView.addAnnotation(annotation)
//1070 Beacon
let location_18 = MKPointAnnotation()
location_18.setCoordinate(CLLocationCoordinate2D (latitude: 42.347236,longitude: -71.15000))
location_18.title = "18 Shepard Street"
location_18.subtitle = "Brighton, MA 02135"
nMapView.addAnnotation(location_18)
//1070 Beacon
let location_26 = MKPointAnnotation()
location_26.setCoordinate(CLLocationCoordinate2D (latitude: 42.358663,longitude: -71.146024))
location_26.title = "26 Lincoln Street"
location_26.subtitle = "Brighton, MA 02135"
nMapView.addAnnotation(location_26)
//1070 Beacon
let location_1280 = MKPointAnnotation()
location_1280.setCoordinate(CLLocationCoordinate2D (latitude: 42.363639,longitude: -71.139366))
location_1280.title = "1280 Soldiers Field Road"
location_1280.subtitle = "Brighton, MA 02135"
nMapView.addAnnotation(location_1280)
//1070 Beacon
let location_59 = MKPointAnnotation()
location_59.setCoordinate(CLLocationCoordinate2D (latitude: 42.363147,longitude: -71.201493))
location_59.title = "59-85 Chapel Street"
location_59.subtitle = "Newton, MA 02458"
nMapView.addAnnotation(location_59)
//1070 Beacon
let location_1 = MKPointAnnotation()
location_1.setCoordinate(CLLocationCoordinate2D (latitude: 42.309260,longitude: -71.134556))
location_1.title = "1 Newton Place"
location_1.subtitle = "Newton, MA 02458"
nMapView.addAnnotation(location_1)
}
}
I do not have access to your second image url but I assume you are looking something as follows. If not, leave comment below.
func mapView(mapView: MKMapView!, annotationView: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
if control == annotationView.rightCalloutAccessoryView {
performSegueWithIdentifier("Detail", sender: self)
}
}
You could also refer to the following examples:
MKPointAnnotations touch event in swift
MapKit in Swift, Part 2
http://www.raywenderlich.com/90971/introduction-mapkit-swift-tutorial

Resources