折线在ios中的谷歌地图上并不完美

时间:2014-05-20 12:00:08

标签: google-maps path google-maps-sdk-ios google-polyline google-direction

我正在尝试在ios7中的谷歌地图上绘制路径。为此,我使用谷歌地图提供的json数据。

  1. 我正在使用谷歌方向api,我能够在两个地方之间绘制路径。
  2. 当我在近处使用路径时,它会很好,但是当我使用长距离路径时,路径不会随谷歌地图转动。
  3. 请在这些图片中查看更清晰(缩放前)。enter image description here
  4. 并且在缩放后,它无法正确转动。enter image description here
  5. enter image description here

    1. 为此,我使用了以下代码。
    2. NSError *错误;

      NSData *jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://maps.googleapis.com/maps/api/directions/json?origin=chennai&destination=porur&sensor=false"]];
      
      NSDictionary *jsonDict  = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];
      
      //NSLog(@"the json data is %@",jsonDict);
      
      
      //--  json path --
      
      NSString *polyString = [[[[ jsonDict objectForKey:@"routes"] objectAtIndex:0] objectForKey:@"overview_polyline"] objectForKey:@"points"];
      
      NSString *TotalEstimatedTime = [[[[[[jsonDict objectForKey:@"routes"]objectAtIndex:0] objectForKey:@"legs"] objectAtIndex:0] objectForKey:@"duration"] objectForKey:@"text"];
      
      NSLog(@" %@ ", polyString);
      NSLog(@" %@ ", TotalEstimatedTime);
      
      
      //===================== drawing path ===============================
      
      NSMutableArray *decodepolyString = [self decodePolyLine:polyString];
      
      GMSMutablePath *path = [GMSMutablePath path];
      
      for (int i= 0; i< [decodepolyString count]; i++)
      {
          CLLocation *location = [decodepolyString objectAtIndex:i];
          CLLocationCoordinate2D coordinate = location.coordinate;
          [path addCoordinate:coordinate];
      }
      
      
      GMSPolyline *overviewpolyline = [GMSPolyline polylineWithPath:path];
      
      overviewpolyline.strokeColor = [UIColor blueColor];
      
      overviewpolyline.strokeWidth = 4.f;
      overviewpolyline.geodesic = YES;
      overviewpolyline.map = self.GMSMapView1;
      

      }

      - (NSMutableArray *)decodePolyLine:(NSString *)encodedStr {     NSMutableString * encoded = [[NSMutableString alloc] initWithCapacity:[encodedStr length]];

      [encoded appendString:encodedStr];
      [encoded replaceOccurrencesOfString:@"\\\\" withString:@"\\"
                                  options:NSLiteralSearch
                                    range:NSMakeRange(0, [encoded length])];
      float len = [encoded length];
      float index = 0;
      NSMutableArray *array = [[NSMutableArray alloc] init];
      float lat=0;
      float lng=0;
      while (index < len) {
          NSInteger b;
          NSInteger shift = 0;
          NSInteger result = 0;
          do {
              b = [encoded characterAtIndex:index++] - 63;
              result |= (b & 0x1f) << shift;
              shift += 5;
          } while (b >= 0x20);
          NSInteger dlat = ((result & 1) ? ~(result >> 1) : (result >> 1));
          lat += dlat;
          shift = 0;
          result = 0;
          do {
              b = [encoded characterAtIndex:index++] - 63;
              result |= (b & 0x1f) << shift;
              shift += 5;
          } while (b >= 0x20);
          NSInteger dlng = ((result & 1) ? ~(result >> 1) : (result >> 1));
          lng += dlng;
          NSNumber *latitude = [[NSNumber alloc] initWithFloat:lat * 1e-5];
          NSNumber *longitude = [[NSNumber alloc] initWithFloat:lng * 1e-5];
      
          CLLocation *location = [[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]];
          [array addObject:location];
      }
      
      return array;
      

      }

      • 请告诉它为什么会发生这种情况,解决方法是什么?
      • 如果你在iOS(目标c)帮助我,它对我有好处。

0 个答案:

没有答案