IOS learning-Map, positioning, and location Marking

Source: Internet
Author: User
Document directory
  • Select a project, TARGETS, and click the plus sign to add two frameworks.

It is easier to use maps on iOS than Android. You only need to create a MKMapView and addSubView. The following figure shows the implementation result:

There are labels (PINs), positioning, and maps.

1. Add a map

1.1 select the default option for a new Single View app. After the app is created, go to ViewController. h

#import <UIKit/UIKit.h>#import <MapKit/MapKit.h>#import <CoreLocation/CoreLocation.h>@interface ViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate> {    MKMapView *map;    CLLocationManager *locationManager;}@end

1.2 add in ViewController. m

- (void)viewDidLoad{    map = [[MKMapView alloc] initWithFrame:[self.view bounds]];map.showsUserLocation = YES;map.mapType = MKMapTypeSatellite;    [self.view addSubview:map];    [super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.}

Run:

OMG, see the world map. How to locate the specified location? For example, how can we locate the great capital of our motherland?

Here map. mapType = MKMapTypeSatellite; I use a satellite map and can use a standard map,

Map. mapType = MKMapTypeStandard;


Note: if there is an error in compilation at this time, please go to the blog and check: 5. Problems Encountered

2. Locate the specified longitude and latitude

CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(39.915352,116.397105);float zoomLevel = 0.02;MKCoordinateRegion region = MKCoordinateRegionMake(coords, MKCoordinateSpanMake(zoomLevel, zoomLevel));[map setRegion:[map regionThatFits:region] animated:YES];

In this way, we are positioning the Palace Museum.

3. Add a pin

3.1 create a new annotation class: CustomAnnotation

Press Command + N to inherit NSObject. Add the following code to the CustomAnnotation. h and CustomAnnotation. m Files:

#import <Foundation/Foundation.h>#import <MapKit/MapKit.h>@interface CustomAnnotation : NSObject <MKAnnotation>{CLLocationCoordinate2D coordinate;NSString *title;NSString *subtitle;}-(id) initWithCoordinate:(CLLocationCoordinate2D) coords;@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;@property (nonatomic, retain) NSString *title;@property (nonatomic, retain) NSString *subtitle;@end
#import "CustomAnnotation.h"@implementation CustomAnnotation@synthesize coordinate, title, subtitle;-(id) initWithCoordinate:(CLLocationCoordinate2D) coords{if (self = [super init]) {coordinate = coords;}return self;}@end

3.1 Use a pin,

Create a method to add a pin

-(Void) createAnnotationWithCoords :( CLLocationCoordinate2D) coords {CustomAnnotation * annotation = [[CustomAnnotation alloc] initWithCoordinate: coords]; annotation. title = @ "title"; annotation. subtitle = @ "subtitle"; [map addAnnotation: annotation];}

Call

CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(39.915352,116.397105);float zoomLevel = 0.02;MKCoordinateRegion region = MKCoordinateRegionMake(coords, MKCoordinateSpanMake(zoomLevel, zoomLevel));[map setRegion:[map regionThatFits:region] animated:YES];[self createAnnotationWithCoords:coords];

In this way, we can locate the pin in the Forbidden City.

4. Locate the current location and obtain the current latitude and longitude

We have already added locationManager. Now we can directly call it in DidViewLoad.

locationManager = [[CLLocationManager alloc] init];    locationManager.delegate = self;    [locationManager startUpdatingLocation];

Implementation Protocol method receives the longitude and latitude after the positioning is successful

- (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. You can obtain the current location and print logs.

2012-06-28 23:58:32.237 MapDemo[8202:11603] Lat: 39.9011  Lng: 116.3000

If not, print the error log.

2012-06-28 23:25:03.109 MapDemo[7531:11603] locError:Error Domain=kCLErrorDomain Code=1 "The operation 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);float zoomLevel = 0.02;MKCoordinateRegion region = MKCoordinateRegionMake(coords,MKCoordinateSpanMake(zoomLevel, zoomLevel));[map setRegion:[map regionThatFits:region] animated:YES];}

To the current location.

5. Problems:

A compilation error is found during running:

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)

Why? No corresponding FrameWork is added. I am using XCode 4.3.2. the method is as follows:

Select a project, TARGETS, and click the plus sign to add two frameworks.

That's all.

How do I send the longitude and latitude of the IOS simulator?

More than 5.0 of simulators can use this function to enable simulation:

In this way, the current simulated location can be sent.

Code example: http://download.csdn.net/detail/totogo2010/4420.1click to open the link

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

Copyright Disclaimer: This article will be published at http://blog.csdn.net/totogo2010/, and you will be welcomed to enjoy the transfer and sharing. Please respect the work of the author. Keep this note and the author's blog link when reprinting. Thank you!

Related Article

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.