iOS learning map, targeting, use of tagged locations

Source: Internet
Author: User

Using maps on iOS is easier than Android, just create a new mkmapview,addsubview. The effect of this is as follows:

There are labels (pins), positioning, maps.

1. Add a Map

1.1 New single View app, select Default, after creation, in ViewController.h

#import <UIKit/UIKit.h><MapKit/MapKit.h><CoreLocation/CoreLocation.h>  <mkmapviewdelegate, cllocationmanagerdelegate> {    *map;     *Locationmanager;} @end

1.2 Adding in VIEWCONTROLLER.M

-(void) viewdidload{    = [[Mkmapview alloc] initwithframe:[self.view bounds]];     = YES;     = Mkmaptypesatellite;    [Self.view Addsubview:map];    [Super Viewdidload];     // Do any additional setup after loading the view, typically from a nib.}

Run:

OMG, see the map of the world. How do you navigate to the specified location? such as locating the great capital of the motherland?

Here map. Maptype =mkmaptypesatellite; I use a satellite map, you can use a standard map,

Map. Maptype =mkmaptypestandard;


Note that if you compile errors at this point, please pull to blog last viewed: 5. Problems encountered

2. Positioning to the specified latitude and longitude
cllocationcoordinate2d coords = Cllocationcoordinate2dmake (39.915352,116.397105);         float 0.02 ;     = Mkcoordinateregionmake (coords, Mkcoordinatespanmake (Zoomlevel, Zoomlevel));    [Map Setregion:[map regionthatfits:region] animated:yes];        

In this way, we will locate the Forbidden City.

3. Add a callout Pin

3.1 Create a new label class: Customannotation

Press Command+n to inherit NSObject. Add the following code to the CustomAnnotation.h and customannotation.m files:

#import <Foundation/Foundation.h><MapKit/MapKit.h><MKAnnotation>{    cllocationcoordinate2d coordinate;     *title;     *subtitle;} -readonly**subtitle; @end

3.1 Using a PIN,

New method to add a pin

-(void) createannotationwithcoords: (cllocationcoordinate2d) coords {    *annotation = [[ Customannotation alloc] Initwithcoordinate:                                     coords];     @" title " ;     @" Sub-headings " ;    [Map addannotation:annotation];}

Call

cllocationcoordinate2d coords = Cllocationcoordinate2dmake (39.915352,116.397105);         float 0.02 ;     = Mkcoordinateregionmake (coords, Mkcoordinatespanmake (Zoomlevel, Zoomlevel));    [Map Setregion:[map regionthatfits:region] Animated:yes];        [Self createannotationwithcoords:coords];

So we can locate the pin in the Forbidden City.



4. Navigate to current position and get current latitude and longitude

We have added Locationmanager in front and now call directly in Didviewload

Locationmanager = [[Cllocationmanager alloc] init];    Locationmanager. delegate = self ;    [Locationmanager startupdatinglocation];

Realization of protocol method latitude and longitude after successful positioning

- (void) Locationmanager: (Cllocationmanager *) Manager didupdatetolocation: (cllocation *) newlocation fromlocation: ( Cllocation *) oldlocation {[Locationmanager stopupdatinglocation]; NSString*strlat = [NSString stringWithFormat:@"%.4f", NewLocation.coordinate.latitude]; NSString*STRLNG = [NSString stringWithFormat:@"%.4f", NewLocation.coordinate.longitude]; NSLog (@"Lat:%@ Lng:%@", Strlat, STRLNG);}- (void) Locationmanager: (Cllocationmanager *) Manager didfailwitherror: (Nserror *) Error {NSLog (@"locerror:%@", error);}

Run, allow to get current location, print log

--£ º32.237 mapdemo[8202:11603  39.9011  116.3000

If not allowed: Print out error log

---£º03.109 mapdemo[7531:11603 ] Locerror:error domain=kclerrordomain code=1"Theoperation couldn ' t be completed. (kclerrordomain error 1.) "

After positioning, move to the current location:

- (void) Locationmanager: (Cllocationmanager *) Manager didupdatetolocation: (cllocation *) newlocation fromlocation: ( Cllocation *) oldlocation {[Locationmanager stopupdatinglocation]; NSString*strlat = [NSString stringWithFormat:@"%.4f", NewLocation.coordinate.latitude]; NSString*STRLNG = [NSString stringWithFormat:@"%.4f", NewLocation.coordinate.longitude]; NSLog (@"Lat:%@ Lng:%@", Strlat, STRLNG); cllocationcoordinate2d coords=Cllocationcoordinate2dmake (newlocation.coordinate.latitude,newlocation.coordinate.longitude); floatZoomlevel =0.02; Mkcoordinateregion Region=Mkcoordinateregionmake (Coords,mkcoordinatespanmake (Zoomlevel, zoomlevel)); [Map Setregion:[map regionthatfits:region] animated:yes];}


Navigates to the current position.

5, the problem will be encountered:

The Run is a compilation error was found:

Undefined Symbols for Architecture i386:

"_cllocationcoordinate2dmake", referenced from:

-[viewcontroller Viewdidload] in VIEWCONTROLLER.O

-[viewcontroller locationManager:didUpdateToLocation:fromLocation:] in VIEWCONTROLLER.O

"_objc_class_$_mkmapview", referenced from:

Objc-class-ref in VIEWCONTROLLER.O

"_objc_class_$_cllocationmanager", referenced from:

Objc-class-ref in VIEWCONTROLLER.O

Ld:symbol (s) not found for architecture i386

Clang:error:linker command failed with exit code 1 (use-v to see invocation)

What is this for? The corresponding framework was not added. I'm using the 4.3.2 version of Xcode and add the following method:

Select item, TARGETS, Dot Plus, add two framework

Just fine.

How do I send the iOS simulator latitude and longitude?

More than 5.0 of the simulator can use this function to open the simulation:

This allows you to send the current location of the simulation.

Example code: http://download.csdn.net/detail/totogo2010/4400001 Click to open the link

Https://github.com/schelling/YcDemo/tree/master/MapDemo

Copyright statement: This article by http://blog.csdn.net/totogo2010/Original, welcome reprint share. Please respect the author Labor, reproduced when the statement and the author of the blog link, thank you!

iOS learning map, targeting, use of tagged locations

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.