Map Development of iOS

Source: Internet
Author: User

First, the system comes with map development

There are three ways to locate the iOS map, namely cellular base station location, WiFi location, GPS positioning

1, Corelocation

1.1 Define Manager Manager Note that this manager must be set to a global variable, or the proxy method will not callback, has been released

@property (nonatomic, strong) Cllocationmanager *locationmanager;

Initialize Self.locationmanager = [[Cllocationmanager alloc] init];

1.2 Set the proxy to self and then follow the proxy protocol

Set up Proxy

_locationmanager.delegate = self;

1.3 Setting Properties: Distancefilter How often do you position the Desiredaccuracy setting accuracy, the more accurate the more the cost of electricity

Setting properties

Self.locationManager.distanceFilter = 100;

Self.locationManager.desiredAccuracy = Kcllocationaccuracybest;

1.4 Turn on positioning

Start positioning

[Self.locationmanager startupdatinglocation];

Stop positioning, turn off positioning when not using positioning

[Self.locationmanager stopupdatinglocation];

1.5 callback method after successful positioning

-(void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (Nsarray *) Locations

{

NSLog (@ "%@", locations);

Remove Position Object

Cllocation *location = Locations.firstobject;

Remove longitude and latitude

cllocationcoordinate2d coordinate = location.coordinate;

Print latitude and longitude

NSLog (@ "%f%f", Coordinate.longitude, Coordinate.latitude);

}

Note : Before iOS7, the system will automatically request authorization, iOS8 and later we need to proactively request authorization, and need to add two additional fields Info.plist file

Nslocationalwaysusagedescription application foreground and background location (always located)

nslocationwheninuseusagedescription apply for background positioning (when needed)

if ([[Uidevice currentdevice].systemversion Doublevalue] >= 8.0) {

Unsolicited Request for authorization

[Self.locationmanager requestalwaysauthorization];

}

1.6 geocoding and anti-geocoding

@property (nonatomic, strong) Clgeocoder *geocoder;

Self.geocoder =[[Clgeocoder alloc] init];

Return geographical coordinates according to place names

[Self.geocoder geocodeaddressstring:@ "Beijing" completionhandler:^ (Nsarray *placemarks, Nserror *error) {

NSLog (@ "Placemark-%@", Placemarks.firstobject

);

Clplacemark *place = Placemarks.lastobject;

Remove longitude and latitude

CGFloat latitude = place.location.coordinate.latitude;

CGFloat LNG = place.location.coordinate.longitude;

Traverse the resulting address

For (Clplacemark *pl in Placemarks) {

NSLog (@ "Longitude%f latitude%f", Pl.location.coordinate.latitude, Pl.location.coordinate.longitude);

}

Traverse the print address stored in the

CountryCode CN

Name Beijing

State Beijing

[Place.addressdictionary enumeratekeysandobjectsusingblock:^ (ID key, id obj, BOOL *stop) {

NSLog (@ "%@%@", key, obj);

}];

}];

Return place names based on geographical coordinates

Cllocation *CL1 = [[Cllocation alloc] initwithlatitude:114.0 longitude:45.0];

Cllocation *cl2 = [[Cllocation alloc] initwithlatitude:-235 longitude:22];

[Self.geocoder reversegeocodelocation:cl1 completionhandler:^ (Nsarray *placemarks, NSError *error) {

if (Error) {

NSLog (@ "Error%@", error);

return;

}

Clplacemark *place = Placemarks.firstobject;

NSLog (@ "Place---%@", place.name);

}];

2, Mapkit

2.1

@property (nonatomic, strong) Mkmapview *mapview;

Self.mapview = [[Mkmapview alloc] Initwithframe:[uiscreen mainscreen].bounds];

Setting properties

_mapview.delegate = self;

_mapview.maptype = Mkmaptypestandard;

Track User's location

_mapview.usertrackingmode = Mkusertrackingmodefollow;

[Self.view Addsubview:_mapview];

2.2 Proxy methods

-(void) Mapview: (Mkmapview *) Mapview didupdateuserlocation: (mkuserlocation *) userlocation

{

NSLog (@ "%f%f", UserLocation.location.coordinate.longitude, UserLocation.location.coordinate.latitude);

Userlocation.title = @ "Beijing";

Userlocation.subtitle = @ "Beijing is a good place";

cllocationcoordinate2d coordinate = userLocation.location.coordinate;

Set the center of the map to the user's location

self.mapView.centerCoordinate = coordinate;

Set the zoom range of a map

Mkcoordinatespan span = mkcoordinatespanmake (0.5, 0.5);

Mkcoordinateregion region = mkcoordinateregionmake (coordinate, span);

[Mapview setregion:region Animated:yes];

}

2.3 Adding a PIN

2.4 Navigating the drawing line

Navigation

NSString *start = @ "Beijing";

NSString *end = @ "Hangzhou";

[Self.geocoder geocodeaddressstring:start completionhandler:^ (Nsarray *placemarks, NSError *error) {

if (Error) {

return;

}

Clplacemark *startplace = Placemarks.firstobject;

[Self.geocoder geocodeaddressstring:end completionhandler:^ (Nsarray *placemarks, Nserror *error) {

if (Error) {

return;

}

Clplacemark *endplace = Placemarks.firstobject;

Navigation Drawing Line

[Self addlinefrom:startplace to:endplace];

The system comes with the navigation

[Self statrnavigationwithclplacemark:startplace endclplacemark:endplace];

}];

/**

* Add a navigation line

*

* @param frompm start position

* @param topm End Position

*/

-(void) Addlinefrom: (Clplacemark *) Startplace to: (Clplacemark *) endplace

{

Add a two Pin

Myannotation *start = [[Myannotation alloc] init];

Start.title = Startplace.name;

Start.coordinate = startPlace.location.coordinate;

[Self.mapview Addannotation:start];

Myannotation *end = [[Myannotation alloc] init];

End.title = Endplace.name;

End.coordinate = endPlace.location.coordinate;

[Self.mapview Addannotation:end];

Find a route

Mkdirectionsrequest *request = [[Mkdirectionsrequest alloc] init];

Set the starting point

Mkplacemark *sourceplace = [[Mkplacemark alloc] initwithplacemark:startplace];

Request.source = [[Mkmapitem alloc] initwithplacemark:sourceplace];

Set End point

Mkplacemark *destination = [[Mkplacemark alloc] initwithplacemark:endplace];

Request.destination = [[Mkmapitem alloc] initwithplacemark:destination];

Orientation Object

Mkdirections *directions = [[Mkdirections alloc] initwithrequest:request];

Calculate route

[Directions calculatedirectionswithcompletionhandler:^ (Mkdirectionsresponse *response, NSError *error) {

NSLog (@ "%ld routes in total", response.routes.count);

For (Mkroute *route in response.routes) {

[Self.mapview AddOverlay:route.polyline];

}

}];

}

#pragma mark-Draw line

-(Mkoverlayrenderer *) Mapview: (Mkmapview *) Mapview Rendererforoverlay: (id<mkoverlay>) Overlay

{

Mkpolylinerenderer *renderer = [[Mkpolylinerenderer alloc] initwithoverlay:overlay];

Renderer.strokecolor = [Uicolor Redcolor];

return renderer;

}

#pragma mark-the system comes with navigation

-(void) Statrnavigationwithclplacemark: (Clplacemark *) Startclplacemark Endclplacemark: (Clplacemark *) EndPlacemark

{

Create a start and end point

Mkplacemark *start = [[Mkplacemark alloc] initwithplacemark:startclplacemark];

Mkmapitem *startitem = [[Mkmapitem alloc] initwithplacemark:start];

Mkplacemark *end = [[Mkplacemark alloc] initwithplacemark:endplacemark];

Mkmapitem *enditem = [[Mkmapitem alloc] initwithplacemark:end];

Sets the number of arrays for start and end points

Nsarray *itemarray = @[startitem, Enditem];

Setting Navigation parameters

Map display Mode

Md[mklaunchoptionsmaptypekey] = @ (Mkmaptypehybrid);

Simply call Mkmapitem's Open method to navigate the system's own map app

Items: Tell the system where the map app is going from

Launchoptions: Additional parameters to launch the system's own map app (mode of navigation/Whether you need traffic/Map mode/...)

Nsmutabledictionary *dic = [Nsmutabledictionary dictionary];

Dic[mklaunchoptionsdirectionsmodekey] = mklaunchoptionsdirectionsmodedriving;

[Mkmapitem Openmapswithitems:itemarray Launchoptions:dic];

Clplacemark *place = [[Clplacemark alloc] init];

Place.location

Mkplacemark *PL = [Mkplacemark alloc] initwithplacemark:<# (Clplacemark *) #>

//

//

//

Mkmapitem *s = [Mkmapitem alloc] initwithplacemark:<# (Mkplacemark *) #>

//

//

Nsmutabledictionary *dic = [Nsmutabledictionary dictionary];

Dic[mklaunchoptionsdirectionsmodekey] = mklaunchoptionsdirectionsmodedriving;

//

//

[Mkmapitem Openmapswithitems:@[s] launchoptions:dic];

}

Map Development of iOS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.