iOS地图的使用示例代码

- (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view.

//    [self setNavBackgroundColor];

//    [self setLeftButtonAsBack];

//    [self setRightButtonWithTitle:@"查找线路" image:@"barbutton_ina@2x.png"];

    [self setFlatDesignBackButton];

    [self setFlatDesignRightButton:@"导航"];

    NSString *titleName = [shopDict objectForKey:@"shopName"];

    [self setNavTitle:[titleName removeSpecialCharacters:titleName]];

 

    CGRect frame = self.view.frame;

    frame.origin.y = 0.0;

    

    //Create mapView

    mapView = [[MKMapView alloc] initWithFrame:frame];

    mapView.delegate = self;

    mapView.showsUserLocation = YES;

    [self.view addSubview:mapView];

    

    NSString *mapLatitudeValue  = [shopDict objectForKey:@"mapLatitudeValue"]; // 店铺纬度

    NSString *mapLongitudeValue = [shopDict objectForKey:@"mapLongitudeValue"];// 店铺经度

 

    CLLocationCoordinate2D shopCoord = CLLocationCoordinate2DMake([mapLatitudeValue doubleValue], [mapLongitudeValue doubleValue]);

    

    annotation = [[Annotation alloc] init];

    annotation.color = MKPinAnnotationColorGreen;

    annotation.coordinate = shopCoord;

    [annotation setDelegate:self];

//    annotation.title = NSLocalizedString(@"Loading...", nil);

    annotation.shopDict = shopDict;

    NSString *title = [shopDict objectForKey:@"shopName"];

    annotation.title = [title removeSpecialCharacters:title];

    annotation.subtitle = [shopDict objectForKey:@"address"];

    [mapView addAnnotation:annotation];

    

    mapView.region = MKCoordinateRegionMake(shopCoord, MKCoordinateSpanMake(0.001f, 0.001f));

}

 

- (void)viewDidUnload

{

    [super viewDidUnload];

 

    // Release any retained subviews of the main view.

}

 

- (void)viewWillAppear:(BOOL)animated

{

    [super viewWillAppear:animated];

    [pinView setSelected:YES animated:YES];

     pinView.highlighted = YES;

     pinView.selected = YES;

}

 

- (void)viewWillDisappear:(BOOL)animated

{

    mapView.showsUserLocation = NO;

}

 

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    LOG(@"GZMapViewController didReceiveMemoryWarning");

}

 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

    return (interfaceOrientation == UIInterfaceOrientationPortrait);

}

 

#pragma mark - onRightButton

- (void)onRightButton:(id)sender

{

    [self showDetails:sender];

}

 

#pragma mark - showDetails

- (void)showDetails:(id)sender

{

    if([[[UIDevice currentDevice] systemVersion] floatValue] < 6.0) { // ios6以下,调用google map

        NSString *mapLatitudeValue  = [shopDict objectForKey:@"mapLatitudeValue"]; // 店铺纬度

        NSString *mapLongitudeValue = [shopDict objectForKey:@"mapLongitudeValue"];// 店铺经度

        //跳转到外部应用程序

        CLLocationCoordinate2D shopCoord = CLLocationCoordinate2DMake([mapLatitudeValue doubleValue], [mapLongitudeValue doubleValue]);

        NSString *URLString = [NSString stringWithFormat:@"maps://maps.google.com/maps?q=0.1,0.1&daddr=%f,%f&saddr=%f,%f",shopCoord.latitude, shopCoord.longitude, mapView.userLocation.coordinate.latitude, mapView.userLocation.coordinate.longitude];

        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:URLString]];

    } else { // 高德地图

        NSString *mapLatitudeValue  = [shopDict objectForKey:@"mapLatitudeValue"]; // 店铺纬度

        NSString *mapLongitudeValue = [shopDict objectForKey:@"mapLongitudeValue"];// 店铺经度

        CLLocationCoordinate2D to;

        to.latitude  = [mapLatitudeValue doubleValue];

        to.longitude = [mapLongitudeValue doubleValue];

        MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];//当前的位置

        MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc]initWithCoordinate:to addressDictionary:nil]];//要到达的位置

        toLocation.name = [shopDict objectForKey:@"shopName"];

        [MKMapItem openMapsWithItems:[NSArray arrayWithObjects:currentLocation, toLocation, nil] launchOptions:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:MKLaunchOptionsDirectionsModeDriving, [NSNumber numberWithBool:YES], nil] forKeys:[NSArray arrayWithObjects:MKLaunchOptionsDirectionsModeKey, MKLaunchOptionsShowsTrafficKey, nil]]];

    }

}

 

- (void)inspectView:(UIView *)aView level:(NSString *)level

{

    NSArray *array = [aView subviews];

    for (int i = 0; i < [array count]; i++) {

        [self inspectView:[array objectAtIndex:i] level:[NSString stringWithFormat:@"%@/%d", level, i]];

    }

}

 

#pragma mark - MapViewDelegate

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)_annotation

{

    if (![_annotation isKindOfClass:[Annotation class]]) {

        return nil;

    }

    static NSString *identifier = @"annotation";

    pinView = (MKPinAnnotationView *)[theMapView dequeueReusableAnnotationViewWithIdentifier:identifier];

    

    if (pinView == nil){        

        pinView = [[MKPinAnnotationView alloc] initWithAnnotation:_annotation reuseIdentifier:identifier];

    }

    pinView.animatesDrop = NO;

    pinView.canShowCallout = YES;

    pinView.draggable = NO;

    [self inspectView:pinView level:@""];

    pinView.pinColor = annotation.color;

    

    // 放入一个跳转按钮

    UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

    rightButton.tag = [(Annotation *)annotation tag];

    [rightButton addTarget:self

                    action:@selector(showDetails:)

          forControlEvents:UIControlEventTouchUpInside];

    pinView.rightCalloutAccessoryView = rightButton;

    

    return pinView;

}

 

- (void)mapView:(MKMapView *)_mapView didAddAnnotationViews:(NSArray *)views

{

    LOG(@"didAddAnnotationViews");

    for (id<MKAnnotation> currentAnnotation in _mapView.annotations) {

        if ([currentAnnotation isEqual:annotation]) {

            [_mapView selectAnnotation:currentAnnotation animated:YES];

        }

    }

}

 

- (void)mapViewDidFinishLoadingMap:(MKMapView *)_mapView

{

    LOG(@"mapViewDidFinishLoadingMap");

    for (id<MKAnnotation> currentAnnotation in _mapView.annotations) {

        if ([currentAnnotation isEqual:annotation]) {

            [_mapView selectAnnotation:currentAnnotation animated:YES];

        }

    }

}

 

posted @ 2014-07-15 10:30  老皮硕  阅读(255)  评论(0)    收藏  举报