IOS learning-Map, positioning, and location Marking

Source: Internet
Author: User

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
[Cpp]
# 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
[Cpp]
-(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
[Cpp]
CLLocationCoordinate2D coords = CLLocationCoordinate2DMake (39.915352, 116.397105 );

Float zoomlevels = 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:
[Cpp]
# 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
[Cpp]
# 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
[Cpp]
-(Void) createAnnotationWithCoords :( CLLocationCoordinate2D) coords {
CustomAnnotation * annotation = [[CustomAnnotation alloc] initWithCoordinate:
Coords];
Annotation. title = @ "title ";
Annotation. subtitle = @ "subtitle ";
[Map addAnnotation: annotation];
}

Call
[Cpp]
CLLocationCoordinate2D coords = CLLocationCoordinate2DMake (39.915352, 116.397105 );

Float zoomlevels = 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.
[Cpp]
LocationManager = [[CLLocationManager alloc] init];
LocationManager. delegate = self;
[LocationManager startUpdatingLocation];

Implementation Protocol method receives the longitude and latitude after the positioning is successful
[Cpp]
-(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. longpolling];
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.
[Cpp]
23:58:32. 237 MapDemo [8202: 11603] Lat: 39.9011 Lng: 116.3000
If not, print the error log.
[Cpp]
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]
-(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. longpolling];
NSLog (@ "Lat: % @ Lng: % @", strLat, strLng );

CLLocationCoordinate2D coords = CLLocationCoordinate2DMake (newLocation. coordinate. latitude, newLocation. coordinate. longpolling );
Float zoomlevels = 0.02;
MKCoordinateRegion region = MKCoordinateRegionMake (coords, MKCoordinateSpanMake (zoomLevel, zoomLevel ));
[Map setRegion: [map regionThatFits: region] animated: YES];
}


Author: totogo2010

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.