iOS中的地圖屬性設置以及位置的管理

上次只是大概說了一下顯示地圖的幾種方法,以及如何設置地圖的一些屬性,今天跟大家分享的事如何在地圖上進行位置註解以及進行位置管理。

//    完成位置反編碼
    CLGeocoder *geocoder = [[CLGeocoder alloc ]init];
    CLLocation *location = [[CLLocation alloc]initWithLatitude:coordinate.latitude longitude:coordinate.longitude];
    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
        if (nil != error) {
            NSLog(@"error info is %@",error);
        }
        for (CLPlacemark *placeMark in placemarks) {
            NSLog(@"%@,%@,%@,%@,%@,%@,%@,%@,%@",placeMark.country,placeMark.areasOfInterest,placeMark.ocean,placeMark.ISOcountryCode,placeMark.inlandWater,placeMark.locality,placeMark.postalCode,placeMark.administrativeArea,placeMark.subLocality);
        }
    }];
顯示位置註解:這裏用硬編碼方式示例

    MKPointAnnotation *pointAnnotation = [[MKPointAnnotation alloc]init];
    pointAnnotation.coordinate = coordinate;
    pointAnnotation.title = @"你好";
    pointAnnotation.subtitle = @"世界";
    [self.mapView addAnnotation:pointAnnotation];
使用MKMapViewDelegate來實現地圖的位置信息以及大頭針的顯示

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    static NSString *indentify = @"indentify";
    MKPinAnnotationView *pinAnnotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:indentify];
    if (nil == pinAnnotationView) {
        pinAnnotationView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:indentify];
    }
    pinAnnotationView.image = [UIImage imageNamed:@"icon_nav_start"];
    
    UIImageView *imageVew = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
    imageVew.image = [UIImage imageNamed:@"Icon"];
    pinAnnotationView.leftCalloutAccessoryView = imageVew;
    
    pinAnnotationView.animatesDrop = YES;
    pinAnnotationView.pinColor = MKPinAnnotationColorPurple;
    pinAnnotationView.canShowCallout = YES;
    
    return pinAnnotationView;
}
當然在這之前還是要遵循CLLocationManagerDelegate,MKMapViewDelegate協議的
設置好後,運行即可看到大頭針的位置顯示視圖了。




發佈了29 篇原創文章 · 獲贊 4 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章