IOS comes with a map

Source: Internet
Author: User

1. Configure the Environment:

1>ios9 to enhance the security of data access, all HTTP requests are changed to HTTPS, in order to be able to use the Map SDK in IOS9, please configure the following in "Info.plist", otherwise affect the use of the SDK.

<key>NSAppTransportSecurity</key><dict>    <key>NSAllowsArbitraryLoads</key>    <true/></dict>

2> in iOS9 in order to be able to adjust the function of the gold Map app, must be in the "info.plist" the URL scheme of the German map app as a whitelist, otherwise cannot be adjusted, configured as follows:

   <key>

     LSApplicationQueriesSchemes

   </key>

<array>    <string>iosamap</string></array>

2. Understanding the Map:

Mkmapview *mapview =[[mkmapview Alloc]init];

Mapview.frame =self.view.bounds;

1. Set the map type

Mapview.maptype = Mkmaptypestandard;

2. Set the tracking mode (Mkusertrackingmodefollow = = tracking)

Self.mapView.userTrackingMode = Mkusertrackingmodefollow; Here is also an important step in the positioning function implementation

3. Set up the agent (to monitor the map's related behavior: for example, the displayed area has changed)

Self.mapView.delegate = self;

3. Mkmapviewdelegate

/**

* Called when updating to the user's location (display location, display range change) This is also an important step in the positioning function implementation

* Userlocation: Pin model data, an encapsulation of the pin position (here Userlocation describes the blue pin used to display the user's location)

*/

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

{

Cllocationcoordinate2d Center = userLocation.location.coordinate;

Mkcoordinatespan span = Mkcoordinatespanmake (0.2509, 0.2256);

Mkcoordinateregion region = Mkcoordinateregionmake (center, span);

[Self.mapview setregion:region animated:yes];//here is the area in which the map is displayed (latitude and longitude, and span (latitude span and latitudinal span))

Usually here to request the surrounding data, add a PIN

}

The area of the map display changes and will be called (display location, display range change) Note: This is usually done when the map is dragged to show the surrounding group buy or the thing will use this method

-(void) Mapview: (Mkmapview *) Mapview regiondidchangeanimated: (BOOL) animated

The area of the map display is about to change and will call

-(void) Mapview: (Mkmapview *) Mapview regionwillchangeanimated: (BOOL) animated

When the user wants to return to the original location:

-(void) Backtouserlocation {

Cllocationcoordinate2d Center = self.mapView.userLocation.location.coordinate;

[Self.mapview Setcentercoordinate:center Animated:yes];

}

4. Geocoding and geo-coding

@property (nonatomic, strong) Clgeocoder *geocoder;

-(Clgeocoder *) Geocoder

{

if (!_geocoder) {

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

}

return _geocoder;

}

//geo-coding: Geographical Names--latitude and longitude

-(void) GeoCode

{

1. Get the address you entered

NSString *address = Self.addressField.text;

if (Address.length = = 0) return;

2. Start coding

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

if (Error | | placemarks.count = = 0) return;

Successful encoding (location information is found)

Show the front landmark information

Clplacemark *firstplacemark = [Placemarks firstobject];//here is the landmark (including the longitude and latitude and the name of the information, so you can add a pin operation)

Cllocationdegrees latitude = firstPlacemark.location.coordinate.latitude;

Cllocationdegrees longitude = firstPlacemark.location.coordinate.longitude;

Self.latitudeLabel.text = [NSString stringwithformat:@ "%.2f", Latitude];

Self.longitudeLabel.text = [NSString stringwithformat:@ "%.2f", longitude];

}];

}

Anti-geocoding: Geographical Names by latitude and longitude

-(void) Reversegeocode

{

Start Reverse coding

Cllocation *location = [[Cllocation alloc] Initwithlatitude:latitude longitude:longtitude];

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

if (Error | | placemarks.count = = 0) return;

Show the front landmark information

Clplacemark *firstplacemark = [Placemarks firstobject];

Cllocationdegrees latitude = firstPlacemark.location.coordinate.latitude;

Cllocationdegrees longitude = firstPlacemark.location.coordinate.longitude;

Self.latitudeField.text = [NSString stringwithformat:@ "%.2f", Latitude];

Self.longtitudeField.text = [NSString stringwithformat:@ "%.2f", longitude];

}];

}

5. Add pin (implement mkannotation) protocol

You can also customize the pin

Hyannotation *anno1 = [[Hyannotation alloc] init];

Anno1.coordinate = Cllocationcoordinate2dmake (39, 119);

Anno1.title = @ "Beijing";

Anno1.subtitle = @ "China's cool Place";

Add a PIN model (model: information that describes a pin)

[Self.mapview Addannotation:anno1];

6. Draw the line (where you need to start and focus, which you can get when you geocode)

-(void) DRAWLINEWITHSOURCECLPM: (Clplacemark *) SOURCECLPM DESTINATIONCLPM: (Clplacemark *) DESTINATIONCLPM

{

if (SOURCECLPM = = Nil | | destinationclpm = nil) return;

1. Initializing direction requests

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

Set the starting point

Mkplacemark *SOURCEMKPM = [[Mkplacemark alloc] INITWITHPLACEMARK:SOURCECLPM];

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

SELF.SOURCEMKPM = SOURCEMKPM;

Set End point

Mkplacemark *DESTINATIONMKPM = [[Mkplacemark alloc] INITWITHPLACEMARK:DESTINATIONCLPM];

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

SELF.DESTINATIONMKPM = DESTINATIONMKPM;

2. Create directions on request

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

3. Execution of requests

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

if (error) return;

For (Mkroute *route in response.routes) {

Add route masking (masking model data for passing routes)

[Self.mapview AddOverlay:route.polyline];

}

}];

//Cover Overlay

}

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

{

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

Redender.linewidth = 5;

Redender.strokecolor = [Uicolor Bluecolor];

return redender;

}

IOS comes with a map

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.