如何在MKMapView之间绘制我当前位置到所需位置之间的路线? [英] How to draw a route between my current location to desired location on MKMapView?

查看:237
本文介绍了如何在MKMapView之间绘制我当前位置到所需位置之间的路线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在当前位置和所需位置之间显示路线。我能够做到这一点,但是当源点和目的地点到目前为止,它将不会显示路由并给出内存警告。您可以建议任何示例代码或任何方式来执行此操作吗?

解决方案

以下代码为当前位置到欲望之间的绘制路径位置

   - (void)viewDidLoad {
[super viewDidLoad];

mapView = [[[MapView alloc] initWithFrame:
CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height)] autorelease ]。

[self.view addSubview:mapView];
[self performSelector:@selector(LoadMaps)withObject:self afterDelay:1.0];
}

- (void)LoadMaps {
MapWithRoutesAppDelegate * app =(MapWithRoutesAppDelegate *)[[UIApplication sharedApplication] delegate];
CLLocation * location = app.userlocation;
//配置来自位置的信息的新事件
CLLocationCoordinate2D coordinates = [位置坐标];

NSString * latitude = [NSString stringWithFormat:@%f,coordinate.latitude];
NSString * longitude = [NSString stringWithFormat:@%f,coordinate.longitude];

NSLog(@dLatitude:%@,纬度);
NSLog(@dLongitude:%@,longitude);


Place * home = [[[Place alloc] init] autorelease];
home.name = @Home;
home.description = @甜蜜的家;
home.latitude = coordinate.latitude;
home.longitude = coordinate.longitude;

Place * office = [[[Place alloc] init] autorelease];
office.name = @Office;
office.description = @坏办公室;
office.latitude = 23.329404;
office.longitude = 72.0039299;

[mapView showRouteFrom:home to:office];
NSLog(@latitide:%。7f,longitude:%.7f,coordinate.latitude,coordinate.longitude);
}

App Applegate方法包含



(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:$ {code} (CLLocation *)oldLocation {
MapWithRoutesAppDelegate * app =(MapWithRoutesAppDelegate *)[[UIApplication sharedApplication] delegate];
app.userlocation = newLocation;
NSLog(@latitide:%。7f,longitude:%.7f,app.userlocation.coordinate.latitude,app.userlocation.coordinate.longitude);
}

您可以从这里下载完整的源代码。



可以帮助很多。


I want to show a route between my current location and desired location. I am able to do this but when the source point and destination point so far then it will not show a route and gave the memory warning. Can you suggest any sample code or any way to do this?

解决方案

Following Code for the draw path between the current location to desire location

- (void)viewDidLoad {
    [super viewDidLoad];

    mapView = [[[MapView alloc] initWithFrame:
                         CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)] autorelease];

    [self.view addSubview:mapView];
    [self performSelector:@selector(LoadMaps) withObject:self afterDelay:1.0];
}

-(void)LoadMaps{
    MapWithRoutesAppDelegate *app = (MapWithRoutesAppDelegate *)[[UIApplication sharedApplication]delegate];
    CLLocation *location = app.userlocation;
    // Configure the new event with information from the location
    CLLocationCoordinate2D coordinate = [location coordinate];

    NSString *latitude = [NSString stringWithFormat:@"%f", coordinate.latitude];
    NSString *longitude = [NSString stringWithFormat:@"%f", coordinate.longitude];

    NSLog(@"dLatitude : %@", latitude);
    NSLog(@"dLongitude : %@",longitude);


    Place* home = [[[Place alloc] init] autorelease];
    home.name = @"Home";
    home.description = @"Sweet home";
    home.latitude = coordinate.latitude;
    home.longitude = coordinate.longitude;

    Place* office = [[[Place alloc] init] autorelease];
    office.name = @"Office";
    office.description = @"Bad office";
    office.latitude = 23.329404;
    office.longitude = 72.0039299;

    [mapView showRouteFrom:home to:office];
    NSLog(@"latitide:%.7f, longitude: %.7f ", coordinate.latitude, coordinate.longitude);
}

In Appdelegate Method contain

#pragma mark- CLLocationManager delegate
- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation{
    MapWithRoutesAppDelegate *app = (MapWithRoutesAppDelegate*)[[UIApplication sharedApplication] delegate];
    app.userlocation = newLocation;
    NSLog(@"latitide:%.7f, longitude: %.7f ", app.userlocation.coordinate.latitude, app.userlocation.coordinate.longitude);
}

You can download the complete source code from here.

May this help lot.

这篇关于如何在MKMapView之间绘制我当前位置到所需位置之间的路线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆