地图开发常用代码



1//更新地图可见区域,让所有公交站点在地图可见区域


- (void)updateMapviewVisibleRegion {

    

    MKMapRect zoomRect = MKMapRectNull;

    for (id <MKAnnotation> annotation in self.mapView.annotations) {

        MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);

        MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0);

        if (MKMapRectIsNull(zoomRect)) {

            zoomRect = pointRect;

        } else {

            zoomRect = MKMapRectUnion(zoomRect, pointRect);

        }

    }

    zoomRect = [self.mapView mapRectThatFits:zoomRect];

    [self.mapView setVisibleMapRect:zoomRect animated:YES];

    

}



- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id < MKAnnotation >)annotation {

    

    //判断是否是自身位置

    if ([annotation isKindOfClass:[MKUserLocation class]]) {

        

        return nil;

    }

    

    

    //验证参数

    if (![annotation isKindOfClass:[ZPMKAnnotation class]]) {

        

        return nil;

        

    }

    

    static NSString *strIdentifier = @"MapViewRouteAnnotationViewIdentifier";

    MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:strIdentifier];

    if (nil == pinView) {

        

        pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:strIdentifier] autorelease]; 

        pinView.tag = ((ZPMKAnnotation *)annotation).stationID;

        pinView.pinColor = MKPinAnnotationColorGreen;

        pinView.canShowCallout = YES;

        pinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

        pinView.draggable = YES;

        

    }else {

        

        pinView.annotation = annotation;

    }

    

    

    

    return pinView;

}




2、在地图上画线



//更新地图可见区域,让所有公交站点在地图可见区域

- (void)updateMapviewVisibleRegion {

    

    MKMapRect zoomRect = MKMapRectNull;

    for (id <MKAnnotation> annotation in self.mapView.annotations) {

        MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);

        MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0);

        if (MKMapRectIsNull(zoomRect)) {

            zoomRect = pointRect;

        } else {

            zoomRect = MKMapRectUnion(zoomRect, pointRect);

        }

    }

    zoomRect = [self.mapView mapRectThatFits:zoomRect];

    [self.mapView setVisibleMapRect:zoomRect animated:YES];

    

}


- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay  {

    

    if(overlay == self.routeLine) {  

    

        self.routeLineView = [[[MKPolylineView alloc] initWithPolyline:self.routeLine] autorelease];  

        self.routeLineView.fillColor = [UIColor blueColor];  

        self.routeLineView.strokeColor = [UIColor blueColor];  

        self.routeLineView.lineWidth = 5;  

        

        return self.routeLineView;  

    }  

    return nil;  

}  



你可能感兴趣的:(iPhone开发有关)