2013-02-04 29 views
4

我正在研究一個iPhone應用程序,該應用程序需要在UIWebView中顯示從點A到另一點B的步行路線,該UIWebView使用相關參數加載Google Maps URL 。如在下面的鏈接圖示的圖像谷歌地圖在iPhone上的地圖指示

[self.myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://maps.google.com/maps?saddr=55.720923,12.513428&daddr=55.745666,12.546387"]]]; 

我需要的UIWebView到指示顯示作爲地圖(在地圖上以行)::

我加載URL的

實施例

How I want the directions to be displayed (directions as a map)

然而,當該URL被裝載在iPhone上的方向,而不是顯示爲列表(見下面的鏈接)。

How the directions are displayed (directions as a list)

用戶需要按下才能看到的指示作爲地圖在頂欄地圖按鈕。

如何設置Google地圖網址參數,以便在我的iPhone應用程序中直接顯示作爲地圖(不是列表!)

+0

我有完全相同的問題。你有沒有想出解決這個問題的方法? – hoya21

回答

1

如果您使用github的MKMapView.check nvpolyline項目,這會容易得多。它將幫助您在點a到點b之間創建一條線,從而解決您的第一個問題。

而對於第二個問題,在您的項目

NSString* apiUrlStr = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%@&destination=%@&sensor=false", saddr, daddr]; 
    //&alternatives=true 

    NSURL* apiUrl = [NSURL URLWithString:apiUrlStr]; 

    NSString *apiResponse = [NSString stringWithContentsOfURL:apiUrl]; 

    SBJsonParser *jsonParser = [SBJsonParser new]; 
    NSDictionary *jsonData = (NSDictionary *) [jsonParser objectWithString:apiResponse error:nil]; 

    NSLog(@"apiResponse is : %@",jsonData); 

    //to display route in drop down 

    NSArray *routeArray = [[NSArray alloc]init]; 
    routeArray= [jsonData objectForKey:@"routes"]; 

    for(int i=0;i<[routeArray count];i++) 
    { 
     NSDictionary *tempDictionary = [routeArray objectAtIndex:i]; 
     if([tempDictionary objectForKey:@"overview_polyline"]!=nil) 
     { 
      NSDictionary *secTempDictionary = [tempDictionary objectForKey:@"overview_polyline"]; 
      if([secTempDictionary objectForKey:@"points"]!=nil) 
      { 
       NSString * routePoint =[secTempDictionary objectForKey:@"points"]; 

       [routeSetAry addObject:routePoint]; 

       encodedPoints = [secTempDictionary objectForKey:@"points"]; 
      } 
      // NSLog(@"secTempDictionary is: %@", secTempDictionary); 
     } 
     if([tempDictionary objectForKey:@"legs"]!=nil) 
     { 
      NSArray *lagArray = [[NSArray alloc]init]; 
      lagArray= [tempDictionary objectForKey:@"legs"]; 

      for(int i=0;i<[lagArray count];i++) 
      { 
       NSDictionary *thirdTempDictionary = [lagArray objectAtIndex:i]; 
       if([thirdTempDictionary objectForKey:@"steps"]!=nil) 
       { 
        NSArray *stepsArray = [[NSArray alloc]init]; 
        stepsArray= [thirdTempDictionary objectForKey:@"steps"]; 

        for(int i=0;i<[stepsArray count];i++) 
        { 
         NSDictionary *forthTempDictionary = [stepsArray objectAtIndex:i]; 

         if([forthTempDictionary objectForKey:@"html_instructions"]!=nil) 
         { 
          NSString * directionStr =[forthTempDictionary objectForKey:@"html_instructions"]; 

          NSRange range; 
          while ((range = [directionStr rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound){ 
           directionStr=[directionStr stringByReplacingCharactersInRange:range withString:@""]; 
          } 
          [directionStrAry addObject:directionStr]; 
         } 

         NSDictionary *fifthTempDictionary = [forthTempDictionary objectForKey:@"polyline"]; 
         if([fifthTempDictionary objectForKey:@"points"]!=nil) 
         { 
          NSString * routePoint =[fifthTempDictionary objectForKey:@"points"]; 

          [polylineSetAry addObject:routePoint]; 

          // encodedPoints = [fifthTempDictionary objectForKey:@"points"]; 
         } 
         NSDictionary *sixthTempDictionary =[forthTempDictionary objectForKey:@"distance"]; 
         if([sixthTempDictionary objectForKey:@"text"]!=nil) 
         { 
          NSString * distanceStr =[sixthTempDictionary objectForKey:@"text"]; 

          [distanceStrAry addObject:distanceStr]; 

          // encodedPoints = [fifthTempDictionary objectForKey:@"points"]; 
         } 
        } 
       } 
      } 
     } 
    } 

    NSLog(@"routeSetAry is :%@",routeSetAry); 
NSLog(@"polylineSetAry is : %i",polylineSetAry.count); 

添加以下代碼這將讓你的方向字符串數組,你可以使用這個字符串在項目中的任何地方diplay。

在我的項目中,我創建了一個顯示在地圖頂部的小視圖,並且在該視圖中我逐一顯示了方向。

+0

謝謝,但我想使用谷歌地圖。 – steak2002

+0

Mapview將加載谷歌地圖.... – Dilip

+0

它會加載iOS 5.0.1之前的谷歌地圖。最新的iOS版本使用Apple地圖數據。 –

0

我不知道,但你應該參考This鏈接 谷歌已經很好地解釋它「谷歌地圖URL方案」

您可以使用

「comgooglemaps:// SADDR = Google + Inc,+ 8th + Avenue,+ New + York,+ NY & daddr = John + F. + Kennedy + International + Airport,+ Van + Wyck + Expressway,+ Jamaica,+ New + York & directionsmode = transit「

它有不同的參數,如 SADDRDADDRdirectionsmode

希望這有助於!