Simple production of iPhone map applications

Source: Internet
Author: User

1. Create a project, such as mapeye. Click the project extension named XIB (similar to storeboard) to go
Operation Interface: Pull the mapview in the data view in the IB Library to the interface (mapview must be in the libraries Section
Expansion)
2. To make the map more powerful, you can set the mapview Delegate of the control, and then use mapviewdidfinishloadingmap: Method
.
(1) first declare class as mkmapviewdelegate in mapeyeviewcontroller. h. The Code is as follows:

1 #import<UIKit/UIKit.h>2 #iimport<MapKit/MapKit.h>3 @interface mapEyeViewController.h:UIViewController4 <MKMapViewDelegate>{}5 @end

(2) Call the mapview delegate method after the map stops being loaded.

1-(void) mapviewdidfinishloadingmap :( mkmapview *) mapview {2 // check the user's location repeatedly. 3 if (0.00001> [mapview userlocation]. location. coordinate. latitude) {4 [self defined mselector: @ selector (mapviewdidfinishloadingmap :) withobject: mapview afterdelay: 1.0]; 5 return; 6} 7 mkcoordinatereginon region = [mapview region]; 8 // the user's location is the center 9 region. center = [mapview userlocation]. location. coordinate; 10 // zoom out Area 11 region. span. latitudedelta = 0.02; 12 region. span. longitudedelta = 0.02; 13 // Add animation 14 [mapview setregion: Region animated: Yes]; 15}

In the UI Editor, select map view and view its connections. Set object for file's owner
Delegate, that is, the mapeyeviewcontroller instance.
Use corelocation to find user locations
Without mapview, You need to obtain the longitude and latitude of the user's location.
You can create a cllocationmanager instance, set it to delegate, call startupdatinglocation,
The class set as delegate also needs to be connected to a clloactionmanagerdelegate protocol.

(1) Start and process corelacation updates

1-(void) startupdates {2 if (nil = locationmanager) 3 locationmanager = [[cllocationmanager alloc] init]; 4 locationmanager. delegate = self; 5 locationmanager. desriedaccuracy = kcllocationaccuracykilom eter; 6 // set the precision to 7 locationmanager. distancefilter = 10; 8 // update que value 9 [locationmanager startupdatinglocation] 10} 11-(void) locationmanager :( cllocationmanager *) manager12 didupdatetolcocation :( cllocation *) newlocati On13 fromlocation :( cllocation *) oldlocation14 {15 nsdate * eventdate = new location timestamp; 16 nstimeinterval howrecent = [eventdate timeintervalsincenow]; 17 if (ABS (howrecent) <120.0) {18 [Manager stopupdatinglocation]; 19 thelocation = new location; 20 [thelocation retain]; 21 you can control the number of updates based on the accuracy and filter settings. At the same time, you can use 22 stopupdatinglocation. To stop location updates as needed. 23} 24}

(3) add an anchor to show the user location
As an interface element, this anchor is implemented in mapeyeviewcontroller. The button name
For parkbtn, add the ibaction of the anchor to the header file. Use mapview as the outlet
The ibaction declaration of mapeyeviewcontroller and park buttons is as follows:

1 @interface mapEyeViewController:UIViewController2 〈MKMapViewDelegate〉{3 IBOutlet MKMapView *mapParking4 }5 -(IBAction)doParkBtn:(id)sender;6 @end

Pull the toolbar in the library and change the above item button to the new name: Park ,:

In the connection of the file's owner, associate mapparking outlet to mapview.

Double-click the default item button on the toolbar and enter a new name: Park. In the connection window, set the park button
Selector for the ibaction you just defined in mapeyeparkedviewcontroller
(Named doparkbtn). The selector of the return Park button is the file's owner in interface builder.
Connect to doparkbtn and set the user's location as the user's parking location. Now let's see how to do it.
The location Annotation on the map is called annotations. The ios sdk provides a prototype of mkannotation,
However, by default, the new prototype is connected. You must create an annotation class, which is very easy to return. You can
To add it to the whereamiparkedviewcontroller file, as shown in the following code:
Location implementation of the mkannotation header file

 1 @interface SpotAnnotation:NSObject<MKAnnotation> 2 { 3 CLLocationCoordinate2D coordinate; 4 NSString *title; 5 NSString *subtitle; 6  7 } 8 @property(nonatomic,assign)CLLocationCoordinate2D coordinate; 9 @property(nonatomic,retain)NSString *title;10 @property(nonatomic,retain)NSString *subtitle;11 @end;


Location implementation file

1 @ implementation sportannotation 2 @ synthesize title, subtitle, coordinate; 3 @ end

Add the anchor to mapview

1 -(IBAction)doParkBtn(id)sender{2 SpotAnnotation *ann=[[SpotAnnotation alloc] init];3 [ann setCoordinate:[mapParking userLocation] location coordinate];4 [mapParking addAnnotation:ann];5 }

Set annotation view to display on View

 1 -(MKAnnotation *)mapView:(MKMapView *)mapView 2 viewForAnnotation:(id<MKAnnotation>)annotation{ 3 if(![annotation isKindOfClass:[SpotAnnotation class]]) 4 return nil; 5 NSString *dqret=@"ParkingAnnon"; 6 id av=[mapView dequeueReusableAnnotationViewWithIdentifier:dqref]; 7 if(nil==av ){ 8 av=[[MKPinAnnotationView alloc] initWithAnnotation:annotation] 9 reuseIdentifier:dqref];10 [av setPinColor:MKPinAnnotationColorRed];11 [av setAnimatesDrop:YES];12 }13 return av;14 }

This prototype is ready.

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.