IOS has a complete set of map frameworks that come with the system — MapKit.Framework and CoreLocation.Framework.
After you add these two frameworks to your project, you are ready for map development.
#import <MapKit/MapKit.h>
Copy the code
Let’s start by defining a map view
@property (nonatomic,retain) MKMapView *mapView;
Copy the code
And the initializers of the mapView, you can just initialize them as you like. Nothing more than Frame Settings.
Sets the desired display style of the map, controlled by the MapType attribute, which is an enumeration variable with three enumerators:
As a developer, it’s especially important to have a learning atmosphere and a networking community, and this is one of my iOS networking groups:812157648, no matter you are small white or big ox welcome to enter, share BAT, Ali interview questions, interview experience, discuss technology, we exchange learning growth together!
MKMapTypeStandard ,
MKMapTypeSatellite,
MKMapTypeHybrid
Text descriptions do not give us an intuitive idea of the effect of the map style. So let’s go straight to the picture above.
We need to set up the map style of our choice
[_mapView setMapType:MKMapTypeStandard];
Copy the code
The next step is to set up whether to display the phone’s GPS location — that little blue dot.
[_mapView setShowsUserLocation:YES];
Copy the code
Load the mapView onto the main view.
[self.view addSubview:_mapView];
Copy the code
So now we have our MapView set up for MapView. But it doesn’t get the user’s location, so we need to go to CLLocationManager to get the user’s current location and display it in the map view. Let’s first define each CLLocationManager.
@property (nonatomic,retain) CLLocationManager *locaManager;
Copy the code
And set on the view controller agent CLLocationManagerDelegate next initialize we define CLLocationManager.
// Initialize LocationManager self.locaManager = [[[CLLocationManager alloc] init] autoRelease; // Set the delegate [_locaManager setDelegate:self]; / / set the position accuracy of [_locaManager setDesiredAccuracy: kCLLocationAccuracyBest]; [_locaManager setDistanceFilter:5.0f]; // Start location [_locaManager startUpdatingLocation];Copy the code
Once the CLLocationManager is initialized and set up, we also need to set its delegate method.
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{// stop obtaining location information [self.locaManager stopUpdatingLocation]; // CLLocation *lastLocation = [locations lastObject]; Cllocation2d myLocation extracts the longitude and latitude in the location information; // latitude mylocate.latitude = [lastLocation coordinate].latitude; // mylocate.longitude = [lastLocation coordinate].longitude; MKCoordinateRegion region = MKCoordinateRegionMake(myLocation, mkatespanmake (0.1f, 0.1f)); [self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES]; }Copy the code
After implementing the proxy method, we have this simple location map ready! Of course, this is just the beginning of map development. Map development is far from that simple, and some advanced custom pins are what I’ll talk about later. If you are interested, you can keep following my blog!
Junye FastPlay
The original address: my.oschina.net/zhongjunye/…