加高德的framework,bundle,库.在info中添加NSLocationAlwaysUsageDescription:YES
-(void)address
{
[MAMapServicessharedServices].apiKey =@"9c541b01ee867f9ecc2dab9d940bc8c9";
_mapView = [[MAMapViewalloc] init];
_mapView.delegate =self;
_mapView.showsUserLocation =YES;//YES为打开定位,NO为关闭定位
[self.viewaddSubview:_mapView];
isAddress=1;
}
-(void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation
updatingLocation:(BOOL)updatingLocation
{
if(updatingLocation)
{
//取出当前位置的坐标
NSLog(@"latitude : %f,longitude: %f",userLocation.coordinate.latitude,userLocation.coordinate.longitude);
CLLocation *loc = [[CLLocationalloc] initWithLatitude:userLocation.coordinate.latitudelongitude:userLocation.coordinate.longitude];
CLGeocoder * geoCoder = [[CLGeocoderalloc] init];
// 3.CLGeocoder反向通过经纬度,获得城市名
[geoCoder reverseGeocodeLocation:loccompletionHandler:
^(NSArray *placemarks,NSError *error) {
CLPlacemark *place = placemarks[0];
_cityName = place.addressDictionary[@"State"];
NSString *street=place.addressDictionary[@"Street"];
NSLog(@"%@",_cityName);
NSLog(@"%@",street);
/*
以下是我用这个addressDictionary属性倒出来的字典。我们分析看看。
{
City = "\U897f\U5b89\U5e02";// 城市名字
Country = "\U4e2d\U56fd";// 国家名字
CountryCode = CN;// 国家编码
FormattedAddressLines = (
"\U4e2d\U56fd",
"\U9655\U897f\U7701\U897f\U5b89\U5e02\U96c1\U5854\U533a",
"\U9ad8\U65b0\U516d\U8def34\U53f7"
); // 这个应该是格式化后的地址了
State = "\U9655\U897f\U7701"; // 省
Street = "\U9ad8\U65b0\U516d\U8def 34\U53f7";// 街道完整名称
SubLocality = "\U96c1\U5854\U533a";//区名
SubThoroughfare = "34\U53f7";//具体地址
Thoroughfare = "\U9ad8\U65b0\U516d\U8def";//街道名称
}
*/
_autoCityName=_cityName;
if (_num==0) {
[selfloadDataAddress];//进入地址数组比较
_num=1;
}
}];
}
}