2016-08-14 92 views

回答

0

對於lblRightAddress也是一樣。

// NSString *location = lblLeftAddress.text; I hust checked with your address it worked. 
NSString *location = @"296 Broadway Blvd Santa Monica CA 90016"; 
     CLGeocoder *geocoder = [[CLGeocoder alloc] init]; 
     [geocoder geocodeAddressString:location 
        completionHandler:^(NSArray* placemarks, NSError* error){ 
         if (placemarks && placemarks.count > 0) { 
          CLPlacemark *topResult = [placemarks objectAtIndex:0]; 
          MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult]; 

          MKCoordinateRegion region = self.mapView.region; 
          region.span.longitudeDelta /= 8.0; 
          region.span.latitudeDelta /= 8.0; 

          [self.mapView setRegion:region animated:YES]; 
          [self.mapView addAnnotation:placemark]; 
         } 
        } 
     ]; 

output

+0

我把這個代碼在viewDidLoad中,但我仍然得到一個空白的地圖,沒有註釋。它應該放在其他地方嗎? – kh1090

+0

給出斷點並檢查您是否在地標中獲得某些值。 – Nilesh

+0

我檢查了這個(NSString *位置= @「296 Broadway Blvd Santa Monica CA 90016」;)它的工作..我附上了屏幕截圖。 – Nilesh

0

這裏只是改變源和目的地的座標。

#pragma mark - Show Route Direction 

-(void)loadRoute 
{ 
    MKPlacemark *source = [[MKPlacemark alloc]initWithCoordinate:CLLocationCoordinate2DMake(34.0207504,-118.69193) addressDictionary:[NSDictionary dictionaryWithObjectsAndKeys:@"",@"", nil] ]; 

    MKMapItem *srcMapItem = [[MKMapItem alloc]initWithPlacemark:source]; 
    [srcMapItem setName:@""]; 

    MKPlacemark *destination = [[MKPlacemark alloc]initWithCoordinate:CLLocationCoordinate2DMake(37.757815,-122.5076406) addressDictionary:[NSDictionary dictionaryWithObjectsAndKeys:@"",@"", nil] ]; 

    MKMapItem *distMapItem = [[MKMapItem alloc]initWithPlacemark:destination]; 
    [distMapItem setName:@""]; 

    MKDirectionsRequest *request = [[MKDirectionsRequest alloc]init]; 
    [request setSource:srcMapItem]; 
    [request setDestination:distMapItem]; 
    [request setTransportType:MKDirectionsTransportTypeAutomobile]; 

    MKDirections *direction = [[MKDirections alloc]initWithRequest:request]; 

    [direction calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) { 

     NSLog(@"response = %@",response); 
     NSArray *arrRoutes = [response routes]; 
     [arrRoutes enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { 

      MKRoute *rout = obj; 

      MKPolyline *line = [rout polyline]; 
      [self.mapView addOverlay:line]; 
      NSLog(@"Rout Name : %@",rout.name); 
      NSLog(@"Total Distance (in Meters) :%f",rout.distance); 

      NSArray *steps = [rout steps]; 

      NSLog(@"Total Steps : %lu",(unsigned long)[steps count]); 

      [steps enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { 
       NSLog(@"Rout Instruction : %@",[obj instructions]); 

      }]; 
     }]; 
    }]; 

} 

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay 
{ 

    if ([overlay isKindOfClass:[MKPolyline class]]) { 
     MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithOverlay:overlay]; 
     [renderer setStrokeColor:[UIColor blueColor]]; 
     [renderer setLineWidth:3.0]; 
     return renderer; 
    } 
    return nil; 
} 

更多的參考,你可以訪問這個here