开发者

Plotting a route between user's location and specified location on Google MapKit in Xcode

开发者 https://www.devze.com 2023-02-19 01:11 出处:网络
Hey guy, On my iPhone project, I built a Google Map using MapKit, and specified a location of my company on the map. Now I want to add a little feature where my google map will plot a route between a

Hey guy, On my iPhone project, I built a Google Map using MapKit, and specified a location of my company on the map. Now I want to add a little feature where my google map will plot a route between a user's location to my company's location. So far this is the code I used to specify the location of my company

#import "GoogleMap.h"
#import "MyAnnotation.h"

@implementation GoogleMap

    - (void)viewDidLoad {
    [super viewDidLoad];

    variable1 = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] 
                                                         pathForResource:@"NewYorkAreas" 
                                                         ofType:@"plist"]];

    double minLat = [[variable1 valueForKeyPath:@"@min.latitude"] doubleValue];
    double maxLat = [[variable1 valueForKeyPath:@"@max.latitude"] doubleValue];
    double minLon = [[variable1 valueForKeyPath:@"@min.longitude"] doubleValue];
    double maxLon = [[variable1 valueForKeyPath:@"@max.longitude"] doubleValue];

    MKCoordinateRegion region;
    region.center.latitude = (maxLat + minLat) / 2.0;
    region.center.longitude = (maxLon + minLon) / 2.0;
    region.span.latitudeDelta = (maxLat - minLat) * 1.05;
    region.span.longitudeDelta = (maxLon - minLon) * 1.05;
    map.region = region;

    for (NSDictionary *newYorkAreasDict in variable1){
        MyAnnotation *annotation = [[MyAnnotation alloc] initWithDictionary:newYorkAreasDict];
        [map addAnnotation:annotation];
        [annotation release];
    }
}

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

    if (map.userLocation == annotation){
        return nil;
    }

    NSString *identifier = @"MY_IDENTIFIER";

    MKAnnotationView *annotationView = [map dequeueReusableAnnotationViewWithIdentifier:identifier];
    if (annotationView == nil){
        annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation 
                                                       reuseIdentifier:identifier] 
                          autorelease];
        annotationView.image = [UIImage imageNam开发者_高级运维ed:@"logo.png"];
        annotationView.canShowCallout = YES;

        annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    }
    return annotationView;
}

So now, I want to implement the user's location and plot a route between the user's location and my company, but I have no clue how to code it, so I hope someone can help me out, thanks!


One way to do it would be you would need to get the GPS coordinates for the route, then you could use an MKOverlayView.

check this out for more info

0

精彩评论

暂无评论...
验证码 换一张
取 消