【问题标题】:how to dynamically pass multiple latitude and longitude to google distance matrix api in c#?如何在c#中动态地将多个纬度和经度传递给谷歌距离矩阵api?
【发布时间】:2021-05-22 13:52:50
【问题描述】:

下面我使用了硬编码的多个 lat long VAR URL = @“https://maps.googleapis.com/maps/api/distancematrix/xml?origins=24.74712999956982,46.5368152409792|24.5576972961426,46.8022346496582|24.9216365814209,46.7802574375|24.7977771759033,46.6214179992676|24.6342353820801,46.5405044555664|24.869083404541,46.6235580444336|24.796724319458 ,46.7717247009277&目的地= 24.7471295792982,46.5368152409792 | 24.5576972961426,46.8022346496582 | 24.9216365814209,46.7802734375 | 24.7977771759033,46.6214179992676 | 24.6342353820801,46.5405044555664 | 24.869083404541,46.6235580444336 | 24.796724319458,46.7717247009277&DEPARTURE_TIME = 1621784369&回调= InitializeMap&库=地方&v =每周“;

【问题讨论】:

    标签: javascript c# .net


    【解决方案1】:

    实际上取决于您如何获取/存储这些坐标。一般来说,string.Join 应该会有所帮助:

    var origins = new double[][]
    {
        new [] {24.74712999956982, 46.5368152409792},
        new [] {24.5576972961426, 46.8022346496582},
        new [] {24.9216365814209, 46.7802574375},
        new [] {24.7977771759033, 46.6214179992676},
        new [] {24.6342353820801, 46.5405044555664},
        new [] {24.869083404541, 46.6235580444336},
        new [] {24.796724319458, 46.7717247009277},
    };
                
    var destinations = new double[][]
    {
        new [] {24.7471295792982, 46.5368152409792},
        new [] {24.5576972961426, 46.8022346496582},
        new [] {24.9216365814209, 46.7802734375},
        new [] {24.7977771759033, 46.6214179992676},
        new [] {24.6342353820801, 46.5405044555664},
        new [] {24.869083404541, 46.6235580444336},
        new [] {24.796724319458, 46.7717247009277},
    };
    
    string format(double[][] cords) => string.Join('|', cords.Select(c => string.Join(',', c)));
    
    var output = $"https://maps.googleapis.com/maps/api/distancematrix/xml" +
        $"?origins={format(origins)}" +
        $"&destinations={format(destinations)}" +
        $"&departure_time=1621784369" +
        $"&callback=InitializeMap" +
        $"&libraries=places" +
        $"&v=weekly";
    
    Console.WriteLine(output);
    

    【讨论】:

    • @umberto-pertov - 上面的代码会在 c=>string.join(',',c) 处给出转换错误
    • 我测试了它,它对我有用.. 你能复制粘贴那个“转换错误”的整个文本,以便我们了解失败的原因吗?
    猜你喜欢
    • 2011-05-18
    • 2016-07-29
    • 1970-01-01
    • 2014-07-30
    • 1970-01-01
    • 1970-01-01
    • 2019-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多