Google не отображает полилинию
Я рисую полилинию, используя новейшие API карт google для iOS. Я строю полилинию по пунктам, но она не отображается должным образом, так как при уменьшении масштаба полилиния исчезает (не в буквальном выражении) с карты, и когда я увеличиваю ее масштаб, она просто показывает строку.
Так появляется полилиния при увеличении
Вот как это выглядит при увеличении
вот моя функция для рисования полилинии
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;}
Ответы
Ответ 1
Я нашел сбой, я делал локальный экземпляр класса RCPolyline
и вызывал метод построения полилинии, из которого я хотел был иметь глобальный объект для экземпляра RCPolyline и обновлять GMSPath
для RCPolyline
экземпляр класса
что-то вроде этого:
- (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: RCPolyline
является подклассом GMSPolyline
Ответ 2
Попробуйте этот код.
- (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];
}