2017-02-25 67 views
12

我正在繪製折線使用iOS的最新谷歌地圖API。我逐點構造多段線,但它不能正確渲染,因爲當我從地圖中縮小折線消失(不按照字面意義)時,並且當我放大它時,只顯示線。谷歌地圖折線不完美渲染

Zoomed in view 這是

Zoomed out view 放大時折線的顯示這是縮小時

這是我繪製折線

RCPolyline *polyline = [[RCPolyline alloc] init]; 
[polyline drawPolylineFromPoint:self.selectedEmployee.location toPoint:location]; 

功能我已經覆蓋init:它如何出現對於RCPolyline是這樣的

- (instancetype)init { 
self = [super init]; 
if (self) { 
    self.strokeWidth = 5.0f; 
    self.strokeColor = UIColor.redColor; 
    self.geodesic = YES; 
    self.map = [RCMapView sharedMapView]; 
} 
return self;} 

drawPolylineFromPoint:toPoint:做到這一點

- (void)drawPolylineFromPoint:(CLLocation *)pointX toPoint:(CLLocation *)pointY { 
     GMSMutablePath *path = [GMSMutablePath path]; 
     [path addCoordinate:pointX.coordinate]; 
     [path addCoordinate:pointY.coordinate]; 
     self.path = path;} 
+0

我想你的多段線路徑數據太多了。 – wf9a5m75

+0

@ wf9a5m75以及這與此有關? –

+0

路徑的繪圖點使用內存。很多分數意味着適用於iOS的Google Maps SDK使用大量內存。我想這就是原因。 爲了減少你的分數,你可以編碼你的路徑。 看看這裏http://stackoverflow.com/questions/30393067/google-maps-ios-sdk-level-of-detail-polylines – wf9a5m75

回答

4

我找到了故障,我正在RCPolyline類的本地實例,並要求從構建折線的方法就是我想要的是有RCPolyline一個全局對象實例和更新GMSPathRCPolyline類實例

是這樣的:

- (instancetype)initWithMap:(GMSMapView *)mapView { 
    self = [super init]; 
    if (self) { 
     self.strokeWidth = 4.0f; 
     self.strokeColor = [UIColor redColor]; 
     self.geodesic = YES; 
     self.map = mapView; 
     self.mutablePath = [GMSMutablePath path]; 
    } 
     return self;} 

現在我從同一個實例調用這個方法。

- (void)appendPolylineWithCoordinate:(CLLocation *)location { 
    [self.mutablePath addCoordinate:location.coordinate]; 
    self.path = self.mutablePath;} 

PS:RCPolylineGMSPolyline

0

子試試這個代碼。

- (void)fetchPolylineWithOrigin:(CLLocation *)origin destination:(CLLocation *)destination { 

    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:lat longitude:longg zoom:12]; 
    GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera]; 
    mapView.myLocationEnabled = YES; 
    self.view = mapView; 
    GMSMarker *marker = [[GMSMarker alloc] init]; 
    marker.position = CLLocationCoordinate2DMake(-33.86, 151.20); 
    marker.map = mapView; 
    NSString *originString = [NSString stringWithFormat:@"%f,%f", origin.coordinate.latitude, origin.coordinate.longitude]; 
    NSString *destinationString = [NSString stringWithFormat:@"%f,%f", destination.coordinate.latitude, destination.coordinate.longitude]; 
    NSString *directionsAPI = @"https://maps.googleapis.com/maps/api/directions/json?"; 
    NSString *directionsUrlString = [NSString stringWithFormat:@"%@&origin=%@&destination=%@&mode=driving&key=%@&alternatives=true", directionsAPI, originString, destinationString,@"YOUR API KEY"]; 
    NSURL *directionsUrl = [NSURL URLWithString:directionsUrlString]; 
    NSURLSessionDataTask *fetchDirectionsTask = [[NSURLSession sharedSession] dataTaskWithURL:directionsUrl completionHandler: 
               ^(NSData *data, NSURLResponse *response, NSError *error) 
               { 
                NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 
                if(error) 
                { 
                 return; 
                } 
                NSArray *routesArray = [json objectForKey:@"routes"]; 
                GMSPolyline *polyline = nil; 
                int i=1; 
                for (id route in routesArray) 
                { 
                 NSDictionary *routeDict = [route valueForKey:@"overview_polyline"]; 
                 NSString *points = [routeDict objectForKey:@"points"]; 
                 GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] init]; 
                 GMSPath *path = [GMSPath pathFromEncodedPath:points]; 
                 polyline = [GMSPolyline polylineWithPath:path]; 
                 polyline.strokeWidth = 3; 
                 if(i==1) 
                 { 
                  polyline.strokeColor = [UIColor greenColor]; 

                 }else if(i==2) 
                 { 
                  polyline.strokeColor = [UIColor redColor]; 
                 }else{ 
                  polyline.strokeColor = [UIColor blueColor]; 
                 } 
                 i = i+1; 

                 bounds = [bounds includingCoordinate:marker.position]; 
                 polyline.map=mapView; 
                } 
               }]; 
    [fetchDirectionsTask resume]; 
} 
+0

我沒有使用API​​獲取兩個座標之間的路線我正在記錄位置數據,因爲用戶正在移動並繪製它 –