如何在MapView ios9中绘制PolyLine

时间:2016-10-15 08:32:03

标签: ios objective-c iphone google-maps google-maps-api-3

我在MapView中创建了一个PolyLine。我尝试了很多次,但没画画。一个绘制polyLine,但这种情况只运行模拟器,我在设备中运行并崩溃我的应用程序。我已经发布了我的问题here

我尝试了很多次并使用了很多教程,请建议任何其他教程。

我想使用google direction URL在MapView中创建PolyLine。起点是我的设备位置,终点是任何lat长。所以中心的两个点都绘制了一条polyLine。这意味着创建根映射起点到终点绘制PolyLine。

请帮助,谢谢

2 个答案:

答案 0 :(得分:1)

您可以使用google api进行此操作

http://maps.googleapis.com/maps/api/directions/json?origin

在这个api中,您必须传递您的位置的lat日志,如此方法

  -(NSArray*) calculateRoutes
    {


            NSString *baseUrl = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%f,%f&destination=%f,%f&sensor=true",currentlatitude,currentlongitude,destination lat,destination log];

            NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:baseUrl]];
            if (data) {

                NSDictionary *direction_dictionary=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
                if (direction_dictionary) {
                    if ([[direction_dictionary valueForKey:@"status"] isEqualToString:@"OK"]) {
                        NSArray *routes_array=[direction_dictionary valueForKey:@"routes"];
                        if (routes_array) {
                            NSString *poinstEncoded_String=[[[routes_array firstObject] valueForKey:@"overview_polyline"] valueForKey:@"points"];
                            routes = [self decodePolyLine:[poinstEncoded_String mutableCopy]];
                            NSInteger numberOfSteps = routes.count;

                            CLLocationCoordinate2D coordinates[numberOfSteps];
                            for (NSInteger index = 0; index < numberOfSteps; index++)
                            {
                                CLLocation *location = [routes objectAtIndex:index];
                                CLLocationCoordinate2D coordinate = location.coordinate;
                                coordinates[index] = coordinate;
                            }
                            MKPolyline *polyLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfSteps];

                            dispatch_async(dispatch_get_main_queue(), ^{
                                [self.MapVw addOverlay:polyLine];

                                // add polyline here to your mapview
                            });

                        }
                    }
                }




            }


    }

-(NSMutableArray *)decodePolyLine: (NSMutableString *)encoded
{

        [encoded replaceOccurrencesOfString:@"\\\\" withString:@"\\" options:NSLiteralSearch range:NSMakeRange(0, [encoded length])];
        NSInteger len = [encoded length];
        NSInteger index = 0;
        NSMutableArray *array = [[NSMutableArray alloc] init];
        NSInteger lat=0;
        NSInteger 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 *loc = [[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]];
            [array addObject:loc];
        }
        return array;


}

答案 1 :(得分:0)

获得回复后(完整示例http://way2ios.com/development/ios-development-2/iphone-2/draw-polygon-mkmapview-stylus/):

   – (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id )overlay

 {

MKPolygonView *polygonView = [[MKPolygonView alloc] initWithPolygon:overlay];

polygonView.lineWidth = 5;

polygonView.strokeColor = [UIColor redColor];

polygonView.fillColor = [UIColor colorWithRed:0 green:191 blue:255 alpha:0.5];

 mapView.userInteractionEnabled = YES;

 return polygonView;

 }

还检查这些: https://github.com/kadirpekel/MapWithRouteshttps://github.com/versluis/MapViewLines