Development environment: Xcode6.4
Simulator: IOS8.4
OS x:10.0.4
Small Blog Link: http://www.goofyy.com
The first example is a program that small writes a location to get latitude and longitude, then displays on the map and customizes the pin. First
First of all, the location service is two Yuan before IOS 8 : You either give an app permission to use the location service or not. You can see in Settings.app which apps can get your location in the background, but you can't do anything to prevent it from getting location information except by not letting the app use the location service at all, but after IOS8, the problem is changed. ISO8 the application into two permissions,
"During use" as the title, the program will get your location information during use. "Always" this is the same as before IOS8, will always be in the program to obtain your personal information.
So when we use the map, we need to add two lines of text inside the Info.plist to set our program's permission to use the map.
Nslocationwheninuseusagedescription //Use time to get position nslocationalwaysusagedescription //Always get user location
The following value can be filled in or not filled in. Value is the pop-up information that is allowed in the Get user locator. If you do not have these two lines, then the program will not remind users to obtain location permission, then your program has not been able to get the location. Once you understand it, start writing the program.
The first is to introduce the Corelocation framework first. Direct drying code. Too lazy to write. Comments below the code
viewcontroller.swift//get Route////Created by Goofygao on 8/18/15.//Copyright (c) goofyy. All rights reserved.//import Uikitimport Mapkitimport Corelocationclass Viewcontroller:uiviewcontroller, mkmapviewdelegate,cllocationmanagerdelegate {@IBOutlet weak var mapview:mkmapview! var Locationmanger:cllocationmanager = Cllocationmanager () var location:cllocation! var ann:mkpointannotation = mkpointannotation () override Func Viewdidload () {super.viewdidload () Locationmanger.delegate = Self Locationmanger.desiredaccuracy = kcllocationaccuracybest locationManger.dist Ancefilter = Kcllocationaccuracykilometer locationmanger.requestalwaysauthorization () LocationManger . startupdatinglocation () Mapview.delegate = self Mapview.maptype = mkmaptype.satellite Ann.title = "positioned position" ann.subtitle = "guangzhou" Mapview.addannotation (Ann)} override func D IdReceivememorywarning () {super.didreceivememorywarning ()//Dispose of any resources the can be recreated. }/** override-change pin color:p aram:mapview:p aram:annotation:returns:MKAnnotationView */FU NC mapview (mapview:mkmapview!, Viewforannotation annotation:mkannotation!), mkannotationview! {var View:mkpinannotationview = Mkpinannotationview (annotation:annotation, reuseidentifier: "Pin") View.pi Ncolor = mkpinannotationcolor.red//view.image = UIImage (named: "Localtion.png") var viewleft = UIView (frame : CGRectMake (0, 0, ()) Viewleft.backgroundcolor = Uicolor.redcolor () var viewright = UIView (frame:cgre Ctmake (0, 0, ()) Viewright.backgroundcolor = Uicolor.greencolor () View.leftcalloutaccessoryview = ViewL EFT View.rightcalloutaccessoryview = ViewRight View.draggable = True View.canshowcallout = True Return View} func LocaTionmanager (manager:cllocationmanager!, didupdatelocations locations: [anyobject]!) {location = Locations.last as! Cllocation if (location) = = nil) {locationmanger.startupdatinglocation ()} println ("\ (loc Ation.coordinate.longitude) mapview.region = Mkcoordinateregion (Center:cllocationcoordinate2dmake (Location.coor Dinate.latitude, Location.coordinate.longitude), Span:mkcoordinatespanmake () ann.coordinate = Cllocationcoo Rdinate2dmake (Location.coordinate.latitude, Location.coordinate.longitude)}}
Separately speaking, the first is the problem of positioning. Add toCllocationmanagerdelegate Agent, the method of implementing proxy
Func Locationmanager (manager:cllocationmanager!, didupdatelocations locations: [anyobject]!) { }
This method is called when it gets to the location of the phone.
Locationmanger.delegate = self locationmanger.desiredaccuracy = kcllocationaccuracybest Locationmanger.distancefilter = Kcllocationaccuracykilometer locationmanger.requestalwaysauthorization () locationmanger.startupdatinglocation ()
Set the agent to current, set the positioning precision and start positioning.
Then there is the place to set up the map. Add a map's proxy, set the main title and subtitle of the PIN.
Mapview.delegate = self mapview.maptype = mkmaptype.satellite ann.title = "Location of position" ann.subtitle = "Guangzhou" Mapview.addannotation (Ann)
The default display color for pins is red. Then we can rewrite the Pin method to redraw the pin.
Func Mapview (mapview:mkmapview!, Viewforannotation annotation:mkannotation!), mkannotationview! { var view:mkpinannotationview = Mkpinannotationview (annotation:annotation, reuseidentifier: "Pin") View.pincolor = mkpinannotationcolor.red //view.image = UIImage (named: "Localtion.png") var viewleft = UIView ( Frame:cgrectmake (0, 0, ()) Viewleft.backgroundcolor = Uicolor.redcolor () var viewright = UIView (frame: CGRectMake (0, 0, ()) Viewright.backgroundcolor = Uicolor.greencolor () View.leftcalloutaccessoryview = Viewleft View.rightcalloutaccessoryview = viewright view.draggable = True View.canshowcallout = True return View }
Set the latitude and longitude of pins and map positioning, because latitude and longitude areThe implementation method of the Cllocationmanagerdelegate protocol is obtained, so we use it in its Protocol method.
Location = Locations.last as! Cllocation if (location) = = nil) { locationmanger.startupdatinglocation () } println ("\ ( Location.coordinate.longitude) mapview.region = Mkcoordinateregion (Center:cllocationcoordinate2dmake ( Location.coordinate.latitude, Location.coordinate.longitude), Span:mkcoordinatespanmake ( a) Ann.coordinate = Cllocationcoordinate2dmake (Location.coordinate.latitude, location.coordinate.longitude) }
Sets the latitude and longitude of the pin and the latitude and longitude of the location, and the extent of the map display.
And then look at the next.
Map Overlay
To put it simply, draw on a map map.
First, draw a line on the map.
var coor = [cllocationcoordinate2d] () for (var i=0; i<5; i++) { po = cllocationcoordinate2dmake ( Cllocationdegrees (+ i), 113) coor.append (PO) } var line = mkpolyline (coordinates: &coor, Count:5) C6/>mapview.addoverlay (line)
Here's a mistake, Var line =mkpolyline(coordinates: &coor, Count:5 ).
Mkpolyline (Coordinates: < #UnsafeMutablePointer <cllocationcoordinate2d>#>, Count: < #Int #>)
A lot of people don't know.< #UnsafeMutablePointer <CLLocationCoordinate2D>#> What is the egg type of this article. To be blunt is a pointer. Most people coming from OC should know that because OC is storing a few of the latitude and longitude generated by the for loop, it usually applies a portion of the memory and then puts it in. The syntax of C is used. The pointer is directly referenced under so. Points to memory that stores latitude and longitude space. Here is the address of this array. Add a "&".
Then add the Mkmapviewdelegate proxy to implement the Proxy method.
Func Mapview (mapview:mkmapview!, Rendererforoverlay overlay:mkoverlay!), mkoverlayrenderer! { var render = Mkpolylinerenderer (overlay:overlay) render.linewidth = 3 Render.strokecolor = Uicolor.redcolor () return render }
The proxy method is automatically invoked here. When we get here, we run, and a straight line comes out. Here's the project for two projects, on GitHub.
Full Project Https://github.com/goooooooofy/originMapApple.git
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
iOS Native Map development article