【问题标题】:What are the differences for polylines between iOs maps and android maps?iOs地图和android地图之间的折线有什么区别?
【发布时间】:2019-10-23 14:50:06
【问题描述】:

我正在用 c# 转换一个 iOs 应用程序,以使用 xamarin.forms 创建一个多平台版本。该应用程序显示了一个地图,其中有很多折线,这些折线是从不同的 json 点(纬度和经度)创建的。当一条折线“完成”另一条无缝开始时,即使因为折线的最后一个点是下一个的第一个。当我使用相同的 json 在 c# 中创建折线时,它们是不连续的,会产生不想要的效果。我该如何解决这个问题?我得到了该应用程序的本机 android 版本,它与多平台版本有相同的问题。我不是 iOs 代码的作者。感谢您的帮助!

这是objective-c代码:

id resp =[NSJSONSerialization JSONObjectWithData:tratta.polilinea options:NSJSONReadingMutableContainers error:nil];
        //NSLog(@"%@",resp);
        if([resp isKindOfClass:[NSArray class]])
        {
            NSArray *listatratte = (NSArray*)resp;
            CLLocationCoordinate2D* coords = malloc([listatratte count] * sizeof(CLLocationCoordinate2D));

            float diskkm = tratta.ini_km;

            for(int i = 0; i < [listatratte count]; i++)
            {
                id cordinate = [listatratte objectAtIndex:i];

                //Aggiungo offset
                float lineangle = atan2(tratta.ini_lat - tratta.end_lat, tratta.end_lon - tratta.ini_lon);
                //float halfGap = 0.0060;
                float halfGap = [self getOffsetStrade];
                float radians = 90/M_PI;

                float angleTop = 90/radians + lineangle;
                float topoffsetx = cos(angleTop)*halfGap;
                float topoffsety = sin(angleTop)*halfGap;

                float angleBottom = -90/radians + lineangle;
                float botoffsetx = cos(angleBottom)*halfGap;
                float botoffsety = sin(angleBottom)*halfGap;

                if([tratta.direzione isEqualToString:@"D"])
                {
                    CLLocationCoordinate2D punto = CLLocationCoordinate2DMake([[cordinate objectForKey:@"lat"]doubleValue]+topoffsetx, [[cordinate objectForKey:@"lon"]doubleValue]+topoffsety);
                    coords[i] = punto;
                }
                else
                {
                    CLLocationCoordinate2D punto = CLLocationCoordinate2DMake([[cordinate objectForKey:@"lat"]doubleValue]+botoffsetx, [[cordinate objectForKey:@"lon"]doubleValue]+botoffsety);
                    coords[i] = punto;
                }
            }
            MKPolyline *poly = [MKPolyline polylineWithCoordinates:coords count:[listatratte count]];
            if([tratta.direzione isEqualToString:@"D"])
            {
                poly.title = @"D";
            }
            else
            {
                poly.title = @"S";
            }
            free(coords);

这是 C# 代码:

 private async Task DrawPolyline(List<Tuple<int,List<Model.Position>>> ListOfListPolylinePoints)
        {
            //IsIntoFiPiLi = false;

            if (Map.Polylines.Count > 0) RemovePolylines();

            foreach (var listOfPoints in ListOfListPolylinePoints)
            {
                var tratta = await RepositoryHelper.Instance.TratteRepository.GetItemAsync(listOfPoints.Item1);
                var polyline_to_draw = new Polyline();
                var points_polilyne = listOfPoints.Item2;
                for (var i = 0; i < points_polilyne.Count - 1; i++)
                {
                    var lineAngle = Math.Atan2(points_polilyne[i].lat - points_polilyne[i + 1].lat,
                        points_polilyne[i + 1].lon - points_polilyne[i].lon);
                    var halfGap = GetOffsetStreet();
                    var radians = 90 / Math.PI;
                    var angleTop = 90 / radians + lineAngle;
                    var topoffsetx = Math.Cos(angleTop) * halfGap;
                    var topoffsety = Math.Sin(angleTop) * halfGap;
                    var angleBottom = -90 / radians + lineAngle;
                    var botoffsetx = Math.Cos(angleBottom) * halfGap;
                    var botoffsety = Math.Sin(angleBottom) * halfGap;

                    if (tratta.direzione == "D")
                        polyline_to_draw.Positions.Add(new Position(points_polilyne[i].lat + topoffsetx,
                            points_polilyne[i].lon + topoffsety));
                    else
                        polyline_to_draw.Positions.Add(new Position(points_polilyne[i].lat + botoffsetx,
                            points_polilyne[i].lon + botoffsety));
                }


                polyline_to_draw.StrokeColor = GetTrafficColor(tratta);
                polyline_to_draw.StrokeWidth = strokeWidth;

                Map.Polylines.Add(polyline_to_draw);
            }
        }

【问题讨论】:

  • 我不太理解你的问题。 iOS和Android的两张截图看起来一样。我无法弄清楚差异,也没有看到任何不连续的折线。另外,您能否分享一个最小的、可重现的示例来重现该问题?类似于tratta.polilinea 中的那些点。
  • 嗨@JackHua-MSFT,如果您在android版本的地图“Lastra a Signa”的标签下看得更清楚,您可以看到两条折线之间存在不连续性。更好地分析代码,我发现如果我采用折线的点并且我很难输入,它会按预期工作。所以,我看到当我从包含点的 jsons 中反序列化点时,会有一些小数的四舍五入,我想这是真正的问题。那么如何避免 Newtonsoft.Json 舍入取自 json 的值?
  • 好的,我看到了不连续的折线。你可以试试下面的代码:var settings = new JsonSerializerSettings(); settings.FloatParseHandling = FloatParseHandling.Decimal; var data = JsonConvert.DeserializeObject(json, settings); 让我知道它是否有效。
  • 谢谢,现在它可以正常工作了。
  • 我在评论中发布了一个带有代码的答案,您可以标记它以便我们帮助更多有同样问题的人吗?

标签: c# android objective-c google-maps xamarin.forms


【解决方案1】:

那么我怎样才能避免 Newtonsoft.Json 舍入取自 json?

您可以在JsonSerializerSettings中将FloatParseHandling设置为Decimal,代码示例如下:

var settings = new JsonSerializerSettings(); 
settings.FloatParseHandling = FloatParseHandling.Decimal;
var data = JsonConvert.DeserializeObject(json, settings);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-22
    • 2023-03-25
    • 1970-01-01
    • 2013-11-02
    • 1970-01-01
    • 1970-01-01
    • 2011-07-12
    相关资源
    最近更新 更多