0

我正在尝试在两个位置之间绘制路线。为此,我有两个CLLocation (startPoint ,endPoint)。在MKReverseGeocoder这两个位置之后,我像这样出去了。

- (void)findAddress{       
    geoCoder1 = [[MKReverseGeocoder alloc] initWithCoordinate:startPoint.coordinate];  
    geoCoder1.delegate = self;   
    [geoCoder1 start];         
    geoCoder2 = [[MKReverseGeocoder alloc] initWithCoordinate:endPoint.coordinate];      
    geoCoder2.delegate = self;
    [geoCoder2 start];    
}   
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
{
    if(geocoder==geoCoder1){
           NSMutableDictionary *cplace1=[[[NSMutableDictionary alloc]initWithDictionary:[placemark addressDictionary]] autorelease];
          NSLog(@"The MKReverseGeocoder Start Point Address %@", cplace1 );

       }else{
            NSMutableDictionary *cplace2=[[[NSMutableDictionary alloc]initWithDictionary:[placemark addressDictionary]] autorelease];             
           NSLog(@"The MKReverseGeocoder End Point Address %@", cplace2 );
       } 
}

输出:-

The MKReverseGeocoder Start Point Address {
    City = Bangalore;
    Country = India;
    CountryCode = IN;
    FormattedAddressLines =     (
        "Grant Rd, Sampangi Rama Nagar",
        "Bangalore, Karnataka",
        India
    );
    State = Karnataka;
    Street = "Grant Rd";
    SubAdministrativeArea = "Bengaluru Rural";
    SubLocality = "Sampangi Rama Nagar";
    Thoroughfare = "Grant Rd";
}

The MKReverseGeocoder End Point Address {
    Country = India;
    CountryCode = IN;
    FormattedAddressLines =     (
        "NH47 Bi - Rd",
        "Ernakulam, Kerala",
        India
    );
    State = Kerala;
    Street = "NH47 Bi - Rd";
    SubAdministrativeArea = Ernakulam;
    Thoroughfare = "NH47 Bi - Rd";
}

现在我想检索这两个位置之间的路线点,为此我NSURLRequest是这样发送的。

 - (void)updateRoute {
         NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:
@"http://maps.googleapis.com/maps/api/directions/json?origin=%@&destination=%@&mode=%@&sensor=false", startLocation,endLocation,routeMode] ]];

        [[NSURLConnection alloc] initWithRequest:request delegate:self];    
      }  

问题是我想通过startLocationendLocationNSURLRequest。那么我应该分配哪些值startLocation,以及endLocationMKReverseGeocoder委托返回(cplace1 和 cplace2)?

4

1 回答 1

0

看起来您已经将开始和结束位置作为CLLocation对象,因此请使用其中的值coordinate(即 a CLLocationCoordinate2D)传递给 Google。

startPoint 和 endPointCLLocationCoordinate2D都包含类型中的 alatitude和。确实是双精度,因此您可以将其放入格式化的字符串中longitudeCLLocationDegreesCLLocationDegrees%f

NSString *googleURL = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%f,%f&destination=%f,%f&mode=%@&sensor=false", 
    startPoint.coordinate.latitude,startPoint.coordinate.longitude,
    endPoint.coordinate.latitude,endPoint.coordinate.longitude,
    routeMode];

NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:googleURL]];
于 2012-04-27T10:11:46.317 回答