iOS開發(fā)之離線地圖核心代碼
更新時(shí)間:2016年04月19日 09:52:20 作者:Livia.Chen
本文給大家分享ios開發(fā)之離線地圖核心代碼,代碼簡(jiǎn)單易懂,非常實(shí)用,有需要的朋友參考下
一,效果圖。

二,工程圖。

三,代碼。
ViewController.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import "MapLocation.h"
@interface ViewController : UIViewController
<MKMapViewDelegate>
{
MKMapView *_mapView;
NSString *addressString;
}
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//調(diào)用系統(tǒng)自帶的高德地圖
//顯示當(dāng)前某地的離線地圖
_mapView = [[MKMapView alloc] init];
_mapView.frame = CGRectMake(0, 40, 320,400);
_mapView.delegate = self;
_mapView.mapType = MKMapTypeStandard;
[self.view addSubview:_mapView];
addressString=@"光啟城";
NSLog(@"---addressString---%@",addressString);
[self geocodeQuery];
}
- (void)geocodeQuery{
if (addressString == nil || [addressString length] == 0) {
return;
}
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:addressString completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(@"查詢記錄數(shù):%ld",[placemarks count]);
if ([placemarks count] > 0) {
[_mapView removeAnnotations:_mapView.annotations];
}
for (int i = 0; i < [placemarks count]; i++) {
CLPlacemark* placemark = placemarks[i];
//調(diào)整地圖位置和縮放比例
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(placemark.location.coordinate, 10000, 10000);
[_mapView setRegion:viewRegion animated:YES];
MapLocation *annotation = [[MapLocation alloc] init];
annotation.streetAddress = placemark.thoroughfare;
annotation.city = placemark.locality;
annotation.state = placemark.administrativeArea;
annotation.zip = placemark.postalCode;
annotation.coordinate = placemark.location.coordinate;
[_mapView addAnnotation:annotation];
}
}];
}
#pragma mark Map View Delegate Methods
- (MKAnnotationView *) mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>) annotation {
MKPinAnnotationView *annotationView
= (MKPinAnnotationView *)[_mapView dequeueReusableAnnotationViewWithIdentifier:@"PIN_ANNOTATION"];
if(annotationView == nil) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:@"PIN_ANNOTATION"];
}
annotationView.pinColor = MKPinAnnotationColorPurple;
annotationView.animatesDrop = YES;
annotationView.canShowCallout = YES;
return annotationView;
}
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
_mapView.centerCoordinate = userLocation.location.coordinate;
}
- (void)mapViewDidFailLoadingMap:(MKMapView *)theMapView withError:(NSError *)error {
NSLog(@"error : %@",[error description]);
}
@end
MapLocation.h
#import <MapKit/MapKit.h> @interface MapLocation : NSObject<MKAnnotation> //街道信息屬性 @property (nonatomic, copy) NSString *streetAddress; //城市信息屬性 @property (nonatomic, copy) NSString *city; //州、省、市信息 @property (nonatomic, copy) NSString *state; //郵編 @property (nonatomic, copy) NSString *zip; //地理坐標(biāo) @property (nonatomic, readwrite) CLLocationCoordinate2D coordinate; @end
MapLocation.m
//地圖調(diào)用函數(shù)
#import "MapLocation.h"
@implementation MapLocation
- (NSString *)title {
return @"您的位置!";
}
- (NSString *)subtitle {
NSMutableString *ret = [NSMutableString new];
if (_state)
[ret appendString:_state];
if (_city)
[ret appendString:_city];
if (_city && _state)
[ret appendString:@", "];
if (_streetAddress && (_city || _state || _zip))
[ret appendString:@" • "];
if (_streetAddress)
[ret appendString:_streetAddress];
if (_zip)
[ret appendFormat:@", %@", _zip];
return ret;
}
@end
相關(guān)文章
加載帶有手勢(shì)識(shí)別器的XIB文件需注意哪些問題
手勢(shì)識(shí)別在iOS上非常重要,手勢(shì)操作移動(dòng)設(shè)備的重要特征,極大的增加了移動(dòng)設(shè)備使用便捷性。通過本教程給大家介紹加載帶有手勢(shì)識(shí)別器的XIB文件需注意哪些問題,需要的朋友可以參考下2015-08-08
iOS開發(fā)之tableView點(diǎn)擊下拉擴(kuò)展與內(nèi)嵌collectionView上傳圖片效果
這篇文章主要介紹了iOS開發(fā)之tableView點(diǎn)擊下拉擴(kuò)展與內(nèi)嵌collectionView上傳圖片效果的相關(guān)資料,需要的朋友可以參考下2016-04-04
IOS App圖標(biāo)和啟動(dòng)畫面尺寸詳細(xì)介紹
這篇文章主要介紹了IOS App圖標(biāo)和啟動(dòng)畫面尺寸詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2017-02-02
Objective-C中NSNumber與NSDictionary的用法簡(jiǎn)介
這篇文章主要介紹了Objective-C中NSNumber與NSDictionary的用法簡(jiǎn)介,雖然Objective-C即將不再是iOS的主流開發(fā)語言...well,需要的朋友可以參考下2015-09-09

