iphoneiosmkmapviewnsurlrequestmkreversegeocoder

After ReverseGeocoder in CLLocation Cordinate ,what Should i give to Google Map Web Service For Exact Location?


I am trying to draw route between two location. For that i have Two CLLocation (startPoint ,endPoint). After MKReverseGeocoder of this two location I got out put like this.

- (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 );
       } 
}

Out put:-

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";
}

Now i want to retrieve route points between this two location, For that i sent NSURLRequest like this.

 - (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];    
      }  

The problem was i want to pass startLocation and endLocation to NSURLRequest. So which values i should assign startLocation, and endLocation from MKReverseGeocoder delegate return(cplace1 and cplace2)?


Solution

  • It looks like you already have the start and end locations as CLLocation objects, so use the values for coordinate (which is a CLLocationCoordinate2D) in those to pass to Google.

    The startPoint and endPoint's CLLocationCoordinate2D both contain a latitude and longitude, which are in CLLocationDegrees types. CLLocationDegrees is really a double, so you can put it into a formatted string with %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]];