【问题标题】:How to draw PolyLine in MapView ios9如何在 MapView ios9 中绘制折线
【发布时间】:2017-02-24 16:38:48
【问题描述】:

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

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

我想使用 google 方向 URL 在 MapView 中创建折线。起点是我的设备位置,终点是任何纬度。所以中心的两个点都画了一条折线。这意味着创建一个根地图起点到终点绘制一条折线。

请帮忙,谢谢

【问题讨论】:

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


【解决方案1】:

您可以为此使用 google api

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

在这个 api 中你必须像这个方法一样传递你的位置的纬度日志

  -(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;


}

【讨论】:

  • routes 是一个全局 NSMutableArray 。
  • 并使用未声明的标识符“SSGlobleFuntions”。在这一行中,routes = [SSGlobleFuntions decodePolyLine:[pointenstEncoded_String mutableCopy]];
  • @DivankKumawat 我已经提到路由是一个数组对象。在你的 h 文件中声明它。
  • decodePolyLine 有些问题请稍候。发送我的项目网址,请检查我的错误。
  • @DivankKumawat 什么网址?
【解决方案2】:

得到回复后(完整示例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

【讨论】:

  • 您复制的代码丢失了很多间距。例如。 [MKPolygonViewalloc] 应该是 [MKPolygonView alloc]。等等。另外,他使用的是折线,而不是多边形,所以你真的想要MKPolylineView,或者更好的是MKPolylineRenderer。你不想要fillColor。此外,viewForOverlay 已替换为 rendererForOverlay...
  • 这个项目不工作请检查github.com/kadirpekel/MapWithRoutes
  • @Rob,我已经展示了我从中获取的链接。感谢您的关心。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-12
  • 1970-01-01
  • 1970-01-01
  • 2018-01-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多