iphone - 绘制多个位置之间的 map 路径

标签 iphone ipad ios6.1

我是 iPhone 开发新手,我想在 5 个位置之间绘制路径,如何在谷歌地图中绘制路径。 我想要从location1到location2、location2到location3、location3到location4以及location4到location5的路径。

这是我的代码

- (void)viewDidLoad {
[super viewDidLoad];

NSDictionary *d1,*d2,*d3,*d4,*d5;

d1=[NSDictionary dictionaryWithObjectsAndKeys:@"location1",@"comments",@"1",@"id",@"23.02941395",@"latitude",@"72.54620655",@"longitude",nil];
d2=[NSDictionary dictionaryWithObjectsAndKeys:@"location2",@"comments",@"2",@"id",@"23.028359049999995",@"latitude",@"72.54537318333334",@"longitude",nil];
d3=[NSDictionary dictionaryWithObjectsAndKeys:@"location3",@"comments",@"3",@"id",@"23.029545",@"latitude",@"72.546036",@"longitude",nil];
d4=[NSDictionary dictionaryWithObjectsAndKeys:@"location4",@"comments",@"4",@"id",@"23.030050",@"latitude",@"72.546226",@"longitude",nil];
d5=[NSDictionary dictionaryWithObjectsAndKeys:@"location5",@"comments",@"5",@"id",@"23.030050",@"latitude",@"72.546022",@"longitude",nil];


self.reports=[NSArray arrayWithObjects:d1,d2,d3,d4,d5,nil];


for (NSDictionary *d in self.reports) {
    float latitude=[[d valueForKey:@"latitude"] floatValue];
    float longitude=[[d valueForKey:@"longitude"] floatValue];

    Place* home = [[[Place alloc] init] autorelease];
    home.name = [d valueForKey:@"comments"];
    home.latitude = latitude;
    home.longitude = longitude;

    PlaceMark *from = [[[PlaceMark alloc] initWithPlace:home] autorelease];             
    [mapView addAnnotation:from];

}


[self centerMap];

   }

   -(void) centerMap 

   {
MKCoordinateRegion region;
CLLocationDegrees maxLat = -90;
CLLocationDegrees maxLon = -180;
CLLocationDegrees minLat = 120;
CLLocationDegrees minLon = 150;
NSMutableArray *temp=[NSArray arrayWithArray:self.reports];
NSLog(@"%@",temp);
for (int i=0; i<[temp count];i++) {
    Place* home = [[[Place alloc] init] autorelease];
    home.latitude = [[[temp objectAtIndex:i] valueForKey:@"latitude"]floatValue];
    home.longitude =[[[temp objectAtIndex:i] valueForKey:@"longitude"]floatValue];

    PlaceMark* from = [[[PlaceMark alloc] initWithPlace:home] autorelease];     

    CLLocation* currentLocation = (CLLocation*)from ;
    if(currentLocation.coordinate.latitude > maxLat)
        maxLat = currentLocation.coordinate.latitude;
    if(currentLocation.coordinate.latitude < minLat)
        minLat = currentLocation.coordinate.latitude;
    if(currentLocation.coordinate.longitude > maxLon)
        maxLon = currentLocation.coordinate.longitude;
    if(currentLocation.coordinate.longitude < minLon)
        minLon = currentLocation.coordinate.longitude;

    region.center.latitude     = (maxLat + minLat) / 2;
    region.center.longitude    = (maxLon + minLon) / 2;
    region.span.latitudeDelta  =  maxLat - minLat;
    region.span.longitudeDelta = maxLon - minLon;
}
[mapView setRegion:region animated:YES];

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

if (annotation == mapView.userLocation) 
    return nil;

MKPinAnnotationView *pin = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier: @"asdf"];

if (pin == nil)
    pin = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier: @"asdf"] autorelease];
else
    pin.annotation = annotation;
pin.userInteractionEnabled = YES;
UIButton *disclosureButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
[disclosureButton setFrame:CGRectMake(0, 0, 30, 30)];

pin.rightCalloutAccessoryView = disclosureButton;
pin.pinColor = MKPinAnnotationColorRed;
//pin.animatesDrop = YES;
[pin setEnabled:YES];
[pin setCanShowCallout:YES];
return pin;

   }

    - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view  calloutAccessoryControlTapped:(UIControl *)control
    {
NSString *strTitle = [NSString stringWithFormat:@"%@",[view.annotation title]];
NSMutableDictionary *d;
NSMutableArray *temp=[NSArray arrayWithArray:self.reports];


for (int i = 0; i<[temp count]; i++)
{
    d = (NSMutableDictionary*)[temp objectAtIndex:i];
    NSString *strAddress = [NSString stringWithFormat:@"%@",[d valueForKey:@"comments"]];

    if([strAddress isEqualToString:strTitle]) {
        [self presentModalViewController:self.nxtDetailsVCtr animated:YES];
        [self.nxtDetailsVCtr.lblDetail setText:strAddress];     
        break;
    }


}
    }

最佳答案

您可以使用NVPlolineAnnotation在 map 内绘制线条。这是Source Code .

关于iphone - 绘制多个位置之间的 map 路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17568827/

相关文章:

ios - UITableView 在单元格上滚动时不滚动

iphone - 有多个 View 时如何禁用多点触控?

ios - 如何在 iOS 6.1 上重现网站崩溃

ios - 在 IOS 中存储应用程序数据的正确位置

iphone - Xcode 4.2 , 将 iPhone xib 改成 iPad xib

iphone - 关闭从 UIWebView 打开的 UIActionSheet

iphone - 有人可以向我解释 SpriteBatch 节点和 Sprite 表是如何工作的吗?

ios - iPad 总是横向启动

ios - 无法停止设备旋转

ios - 如何在 ios 6.1 xcode 4.6 中获取 json 数据