简体   繁体   中英

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

Depends on how you obtain/store these coordinates, really. In general, string.Join should help:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM