簡體   English   中英

如何在 c# 中動態地將多個緯度和經度傳遞給谷歌距離矩陣 api?

[英]how to dynamically pass multiple latitude and longitude to google distance matrix api in c#?

Below i've used hard coded multiple 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&destinations=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&callback=InitializeMap&libraries=places&v=weekly ";

取決於你如何獲取/存儲這些坐標,真的。 一般來說, 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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM