1.首先加入地图依赖的两个包
#import <MapKit/MapKit.h> #import <CoreLocation/CoreLocation.h>
2.确定位置还必须依赖你的zipCode和地址Dictionary,zipCode可以根据Google提供的方法获取你所在位置的经度和维度, 地址Dictionary可以在Map上面精确的标注出来你的位置,
实现发现如下:
- (void)showMap:(NSString *)zipCode withAddress:(NSDictionary *)address { NSString *url, *result; NSArray *dataArray; double latitude; double longitude; MKCoordinateRegion mapRegion; //根据Google提供zip url url = [[NSString alloc] initWithFormat: @"http://maps.google.com/maps/geo?output=csv&q=%@", zipCode]; //根据url获取Google提供的方法一个逗号分割的经度、维度字符串 result = [[NSString alloc] initWithContentsOfURL: [NSURL URLWithString:url] encoding:NSUTF8StringEncoding error:nil]; //把逗号分隔字符串转化为数组(类似java里面的split) dataArray = [url componentsSeparatedByString: @","]; //Google返回的数据是, 如: ’2, 3, 4, 5‘后面两位数字分别是latitude、longitude if ([dataArray count] == 4) { latitude = [[dataArray objectAtIndex: 2] doubleValue]; longitude = [[dataArray objectAtIndex:3] doubleValue]; mapRegion.center.latitude = latitude; mapRegion.center.longitude = longitude; mapRegion.span.latitudeDelta = 0.2; mapRegion.span.longitudeDelta = 0.2; //设置你的维度和经度后 重新绘制地图 [map setRegion: mapRegion animated:YES]; //以下是地图中标示你的地址 if (zipAnnotation != nil) { [map removeAnnotation: zipAnnotation]; } zipAnnotation = [[MKPlacemark alloc] initWithCoordinate:mapRegion.center addressDictionary:fullAddress]; [map addAnnotation: zipAnnotation]; } }
3.以上设置后, 便可在你的iphone模拟器上面显示地图以及你所在的位置.
在附件中一个小项目计算您当前的位置距离北京多远