MKMapView에 경로 그리기 (BreadCrumb 활용)

2012. 11. 27. 09:49아이폰 개발

Apple에서 샘플로 제공하는 BreadCrumb 예제를 참고하면 쉽게 구현할 수 있다.

http://developer.apple.com/library/ios/#samplecode/Breadcrumb/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010048-Intro-DontLinkElementID_2


전국 광역시도 위도, 경도값은 아래 사이트를 참고하자.

http://blueamor.tistory.com/918


    CLLocation *oldLocation = [[CLLocation alloc] initWithLatitude:37.566531 longitude:126.977961];

    CLLocation *new1Location = [[CLLocation alloc] initWithLatitude:37.566535 longitude:126.977969];

    CLLocation *new2Location = [[CLLocation alloc] initWithLatitude:35.198362 longitude:129.053922];

    

    [self addLocation:new1Location fromLocation:oldLocation];

    [self addLocation:new2Location fromLocation:new1Location];


서울과 부산의 위도/경도를 이용하여 제대로 경로가 그려지는지 확인을 해보니 아래와 같이 잘 작동되는 것을 확인 했다.


-(void)addLocation:(CLLocation *)newLocation

           fromLocation:(CLLocation *)oldLocation

{

    if (newLocation)

    {

        if ([self.toggleAudioButton isOn])

{

[self setSessionActiveWithMixing:YES]; // YES == duck if other audio is playing

[self playSound];

}

// make sure the old and new coordinates are different

        if ((oldLocation.coordinate.latitude != newLocation.coordinate.latitude) &&

            (oldLocation.coordinate.longitude != newLocation.coordinate.longitude))

        {

            if (!crumbs)

            {

                // This is the first time we're getting a location update, so create

                // the CrumbPath and add it to the map.

                //

                crumbs = [[CrumbPath alloc] initWithCenterCoordinate:newLocation.coordinate];

                [map addOverlay:crumbs];

                

                // On the first location update only, zoom map to user location

                MKCoordinateRegion region =

                MKCoordinateRegionMakeWithDistance(newLocation.coordinate, 20000, 20000);

                [map setRegion:region animated:YES];

            }

            else

            {

                // This is a subsequent location update.

                // If the crumbs MKOverlay model object determines that the current location has moved

                // far enough from the previous location, use the returned updateRect to redraw just

                // the changed area.

                //

                // note: iPhone 3G will locate you using the triangulation of the cell towers.

                // so you may experience spikes in location data (in small time intervals)

                // due to 3G tower triangulation.

                //

                MKMapRect updateRect = [crumbs addCoordinate:newLocation.coordinate];

                

                if (!MKMapRectIsNull(updateRect))

                {

                    // There is a non null update rect.

                    // Compute the currently visible map zoom scale

                    MKZoomScale currentZoomScale = (CGFloat)(map.bounds.size.width / map.visibleMapRect.size.width);

                    // Find out the line width at this zoom scale and outset the updateRect by that amount

                    CGFloat lineWidth = MKRoadWidthAtZoomScale(currentZoomScale);

                    updateRect = MKMapRectInset(updateRect, -lineWidth, -lineWidth);

                    // Ask the overlay view to update just the changed area.

                    [crumbView setNeedsDisplayInMapRect:updateRect];

                }

            }

        }

    }

}


대신 locationManager의 didUpdateToLocation은 막아두어야 한다. 아니면 에뮬레이터의 경우 마지막 경로를 미국 현위치로 그리기 때문이다.


- (void)locationManager:(CLLocationManager *)manager

    didUpdateToLocation:(CLLocation *)newLocation

           fromLocation:(CLLocation *)oldLocation