IOS 地圖控制元件 : mapkit
第一步
顯示地圖
- - (void)viewDidLoad
- {
- self.mapView=[[[MKMapView alloc] initWithFrame:self.view.bounds] autorelease];
- mapView.delegate=self;
- mapView.autoresizingMask= (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin);
- [self.view addSubview:mapView];
- [self.mapView setZoomEnabled:YES];
- [self.mapView setScrollEnabled:YES];
- [super viewDidLoad];
- // Do any additional setup after loading the view from its nib.
- }
- (void)viewDidLoad
{
self.mapView=[[[MKMapView alloc] initWithFrame:self.view.bounds] autorelease];
mapView.delegate=self;
mapView.autoresizingMask= (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin);
[self.view addSubview:mapView];
[self.mapView setZoomEnabled:YES];
[self.mapView setScrollEnabled:YES];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
也可以直接託控制元件。 直接執行,OK . 沒問題。
第二步:
啟動定位服務,標註自己當前位置
- if (lm) {
- lm.delegate=nil;
- [lm release];
- lm=nil;
- }
- lm=[[CLLocationManager alloc] init];
- lm.delegate=self;
- lm.desiredAccuracy= kCLLocationAccuracyNearestTenMeters;
- lm.distanceFilter =1000.0f;
- [lm startUpdatingLocation];
if (lm) {
lm.delegate=nil;
[lm release];
lm=nil;
}
lm=[[CLLocationManager alloc] init];
lm.delegate=self;
lm.desiredAccuracy= kCLLocationAccuracyNearestTenMeters;
lm.distanceFilter =1000.0f;
[lm startUpdatingLocation];
如果定位成功 :
- -(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
- {
- if (!newLocation) {
- [self locationManager:manager didFailWithError:(NSError *)NULL];
- }
- if (signbit(newLocation.horizontalAccuracy)) {
- [self locationManager:manager didFailWithError:(NSError *)NULL];
- return;
- }
- [manager stopUpdatingLocation];
- CLLocationCoordinate2D _coordination = [newLocation coordinate];
- now_lat = _coordination.latitude;
- now_lng =_coordination.longitude;
- mapView.showsUserLocation =YES;
- [lm stopUpdatingLocation];
- ///
- [NSThread detachNewThreadSelector:@selector(getNear) toTarget:self withObject:nil];
- }
-(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
if (!newLocation) {
[self locationManager:manager didFailWithError:(NSError *)NULL];
}
if (signbit(newLocation.horizontalAccuracy)) {
[self locationManager:manager didFailWithError:(NSError *)NULL];
return;
}
[manager stopUpdatingLocation];
CLLocationCoordinate2D _coordination = [newLocation coordinate];
now_lat = _coordination.latitude;
now_lng =_coordination.longitude;
mapView.showsUserLocation =YES;
[lm stopUpdatingLocation];
///
[NSThread detachNewThreadSelector:@selector(getNear) toTarget:self withObject:nil];
}
這樣便可以定自己當前位置。
/***
執行緒是根據當前經緯度 從網路上獲取附近 。
返回JSON 字串, 然後解析json 。
得到每個物件經緯度
**/
第三步:
在地圖上畫圈
首先在 標頭檔案定義 :
- @property(nonatomic,retain)MKCircle *circle;
@property(nonatomic,retain)MKCircle *circle;
圈的顏色,屬性
- -(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
- {
- MKOverlayView *view=nil;
- if ([overlay isKindOfClass:[MKCircle class]]) {
- MKCircleView *cirView =[[MKCircleView alloc] initWithCircle:overlay];
- cirView.fillColor=[UIColor redColor];
- cirView.strokeColor=[UIColor redColor];
- cirView.alpha=0.1;
- cirView.lineWidth=4.0;
- view=[cirView autorelease];
- }
- return view;
- }
-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
{
MKOverlayView *view=nil;
if ([overlay isKindOfClass:[MKCircle class]]) {
MKCircleView *cirView =[[MKCircleView alloc] initWithCircle:overlay];
cirView.fillColor=[UIColor redColor];
cirView.strokeColor=[UIColor redColor];
cirView.alpha=0.1;
cirView.lineWidth=4.0;
view=[cirView autorelease];
}
return view;
}
第四步:
顯示自己位置和為地圖新增標註
- @interface POI : NSObject<MKAnnotation>
- {
- CLLocationCoordinate2D coordinate;
- NSString *subtitle;
- NSString *title;
- NSString *cofeId ;
- NSString *doroname;
- }
- @property (nonatomic,readonly) CLLocationCoordinate2D coordinate;
- @property (nonatomic,copy) NSString *subtitle;
- @property (nonatomic,copy) NSString *title;
- @property (nonatomic,copy) NSString *xId ;
- @property (nonatomic,copy) NSString *name; ;
- -(id) initWithCoords:(CLLocationCoordinate2D) coords;
@interface POI : NSObject<MKAnnotation>
{
CLLocationCoordinate2D coordinate;
NSString *subtitle;
NSString *title;
NSString *cofeId ;
NSString *doroname;
}
@property (nonatomic,readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic,copy) NSString *subtitle;
@property (nonatomic,copy) NSString *title;
@property (nonatomic,copy) NSString *xId ;
@property (nonatomic,copy) NSString *name; ;
-(id) initWithCoords:(CLLocationCoordinate2D) coords;
- @implementation POI
- @synthesize coordinate,subtitle,title;
- @synthesize xId;
- @synthesize doroname;
- - (id) initWithCoords:(CLLocationCoordinate2D) coords{
- self = [super init];
- if (self) {
- coordinate = coords;
- }
- return self;
- }
- - (void) dealloc
- {
- [title release];
- [subtitle release];
- [xId release];
- [doroname release];
- [super dealloc];
- }
- @end
@implementation POI
@synthesize coordinate,subtitle,title;
@synthesize xId;
@synthesize doroname;
- (id) initWithCoords:(CLLocationCoordinate2D) coords{
self = [super init];
if (self) {
coordinate = coords;
}
return self;
}
- (void) dealloc
{
[title release];
[subtitle release];
[xId release];
[doroname release];
[super dealloc];
}
@end
這個網上很多, 可以根據需求自己適當修改 。
- doors=[jsonDic objectForKey:@"roomshops"];
- for (NSDictionary *dic in doors) {
- CLLocationCoordinate2D p1;
- p1.latitude= [[dic objectForKey:@"roomLng"] doubleValue];
- p1.longitude=[[dic objectForKey:@"roomLat"] doubleValue];
- POI *poi = [[[POI alloc] initWithCoords:p1] autorelease];
- poi.title=[dic objectForKey:@"roomName"];
- poi.subtitle=[dic objectForKey:@"roomAddress"];
- poi.xId= [dic objectForKey:@"roomid"];
- poi.doroname=[dic objectForKey:@"room"];
- [mapView addAnnotation:poi];
- }
doors=[jsonDic objectForKey:@"roomshops"];
for (NSDictionary *dic in doors) {
CLLocationCoordinate2D p1;
p1.latitude= [[dic objectForKey:@"roomLng"] doubleValue];
p1.longitude=[[dic objectForKey:@"roomLat"] doubleValue];
POI *poi = [[[POI alloc] initWithCoords:p1] autorelease];
poi.title=[dic objectForKey:@"roomName"];
poi.subtitle=[dic objectForKey:@"roomAddress"];
poi.xId= [dic objectForKey:@"roomid"];
poi.doroname=[dic objectForKey:@"room"];
[mapView addAnnotation:poi];
}
解析json。
這裡要說明以下, google地圖先緯度,再經度。 而一般我們都是先經度,再緯度。
地圖示註 :
- - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
- {
- /// 判斷是否是自己
- if ([annotation isKindOfClass:[POI class]]) {
- MKAnnotationView *view = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:[annotation title] ];
- if (view==nil) {
- view= [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:[annotation title]] autorelease];
- }
- else
- {
- view.annotation=annotation;
- }
- POI *pview= annotation;
- if ([pview.doroname isEqual:@"你大爺"]) {
- [view setImage:[UIImage imageNamed:@"poi.png"]];
- view.canShowCallout=YES;
- UIButton *btn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
- view.rightCalloutAccessoryView=btn;
- }
- else
- {
- [view setImage:[UIImage imageNamed:@"大小_選中.png"]];
- view.canShowCallout=YES;
- UIButton *btn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
- view.rightCalloutAccessoryView=btn;
- }
- return view;
- }
- else
- {
- POI *Mapannotation = annotation;
- Mapannotation.title=@"當前位置";
- return nil;
- }
- }
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
/// 判斷是否是自己
if ([annotation isKindOfClass:[POI class]]) {
MKAnnotationView *view = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:[annotation title] ];
if (view==nil) {
view= [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:[annotation title]] autorelease];
}
else
{
view.annotation=annotation;
}
POI *pview= annotation;
if ([pview.doroname isEqual:@"你大爺"]) {
[view setImage:[UIImage imageNamed:@"poi.png"]];
view.canShowCallout=YES;
UIButton *btn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
view.rightCalloutAccessoryView=btn;
}
else
{
[view setImage:[UIImage imageNamed:@"大小_選中.png"]];
view.canShowCallout=YES;
UIButton *btn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
view.rightCalloutAccessoryView=btn;
}
return view;
}
else
{
POI *Mapannotation = annotation;
Mapannotation.title=@"當前位置";
return nil;
}
}
截圖說明,因為我圈是加在中間的, 所以會這樣。