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

[CPP]View Plaincopy
    1. #import <UIKit/UIKit.h>
    2. #import <MapKit/MapKit.h>
    3. #import <CoreLocation/CoreLocation.h>
    4. @interface Viewcontroller:uiviewcontroller
    5. <mkmapviewdelegate, cllocationmanagerdelegate> {
    6. Mkmapview *map;
    7. Cllocationmanager *locationmanager;
    8. }
    9. @end

1.2 Adding in VIEWCONTROLLER.M

[CPP]View Plaincopy
    1. -(void) Viewdidload
    2. {
    3. Map = [[Mkmapview alloc] initwithframe:[self.view bounds];
    4. Map.showsuserlocation = YES;
    5. Map.maptype = Mkmaptypesatellite;
    6. [Self.view Addsubview:map];
    7. [Super Viewdidload];
    8. additional setup after loading the view, typically from a nib.
    9. }

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[CPP]View Plaincopy
    1. cllocationcoordinate2d coords = Cllocationcoordinate2dmake (39.915352,116.397105);
    2. float zoomlevel = 0.02;
    3. Mkcoordinateregion region = Mkcoordinateregionmake (coords, Mkcoordinatespanmake (Zoomlevel, zoomlevel));
    4. [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:

[CPP]View Plaincopy
  1. #import <Foundation/Foundation.h>
  2. #import <MapKit/MapKit.h>
  3. @interface Customannotation:nsobject
  4. <MKAnnotation>
  5. {
  6. cllocationcoordinate2d coordinate;
  7. NSString *title;
  8. NSString *subtitle;
  9. }
  10. -(ID) initwithcoordinate: (cllocationcoordinate2d) coords;
  11. @property (nonatomic, readonly) cllocationcoordinate2d coordinate;
  12. @property (nonatomic, retain) NSString *title;
  13. @property (nonatomic, retain) NSString *subtitle;
  14. @end
[CPP]View Plaincopy
    1. #import "CustomAnnotation.h"
    2. @implementation Customannotation
    3. @synthesize coordinate, title, subtitle;
    4. -(ID) initwithcoordinate: (cllocationcoordinate2d) coords
    5. {
    6. if (self = [super init]) {
    7. coordinate = coords;
    8. }
    9. return self;
    10. }
    11. @end


3.1 Using a PIN,

New method to add a pin

[CPP]View Plaincopy
    1. -(void) Createannotationwithcoords: (cllocationcoordinate2d) coords {
    2. Customannotation *annotation = [[Customannotation alloc] Initwithcoordinate:
    3. Coords];
    4. Annotation.title = @"title";
    5. Annotation.subtitle = @"sub-title";
    6. [Map addannotation:annotation];
    7. }


Call

[CPP]View Plaincopy
    1. cllocationcoordinate2d coords = Cllocationcoordinate2dmake (39.915352,116.397105);
    2. float zoomlevel = 0.02;
    3. Mkcoordinateregion region = Mkcoordinateregionmake (coords, Mkcoordinatespanmake (Zoomlevel, zoomlevel));
    4. [Map Setregion:[map regionthatfits:region] Animated:yes];
    5. [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

[CPP]View Plaincopy
    1. Locationmanager = [[Cllocationmanager alloc] init];
    2. Locationmanager.delegate = self;
    3. [Locationmanager startupdatinglocation];

Realization of protocol method latitude and longitude after successful positioning

[CPP]View Plaincopy
  1. -(void) Locationmanager: (Cllocationmanager *) Manager didupdatetolocation: (cllocation *) NewLocation fromlocation :(cllocation *) oldlocation {
  2. [Locationmanager stopupdatinglocation];
  3. NSString *strlat = [NSString stringwithformat:@"%.4f", NewLocation.coordinate.latitude];
  4. NSString *strlng = [NSString stringwithformat:@"%.4f", NewLocation.coordinate.longitude];
  5. NSLog (@"Lat:%@ Lng:%@", Strlat, STRLNG);
  6. }
  7. -(void) Locationmanager: (Cllocationmanager *) Manager didfailwitherror: (nserror *) Error {
  8. NSLog (@"locerror:%@", error);
  9. }

Run, allow to get current location, print log

[CPP]View Plaincopy
    1. 2012-06-28 23:58:32.237 mapdemo[8202:11603] lat:39.9011 lng:116.3000

If not allowed: Print out error log

[CPP]View Plaincopy
    1. 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:

[CPP]View Plaincopy
  1. -(void) Locationmanager: (Cllocationmanager *) Manager didupdatetolocation: (cllocation *) NewLocation fromlocation :(cllocation *) oldlocation {
  2. [Locationmanager stopupdatinglocation];
  3. NSString *strlat = [NSString stringwithformat:@"%.4f", NewLocation.coordinate.latitude];
  4. NSString *strlng = [NSString stringwithformat:@"%.4f", NewLocation.coordinate.longitude];
  5. NSLog (@"Lat:%@ Lng:%@", Strlat, STRLNG);
  6. cllocationcoordinate2d coords = Cllocationcoordinate2dmake (NewLocation.coordinate.latitude, NewLocation.coordinate.longitude);
  7. float zoomlevel = 0.02;
  8. Mkcoordinateregion region = Mkcoordinateregionmake (Coords,mkcoordinatespanmake (Zoomlevel, zoomLevel));
  9. [Map Setregion:[map regionthatfits:region] Animated:yes];
  10. }


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

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.