Setting polyline locations from an array RRS feed

  • Question

  • I would appreciate advice on the following:
     I wish to enter a polyline. All the examples I find are on the following lines:
    var polyline = new Microsoft.Maps.Polyline([new Microsoft.Maps.Location(center.latitude + 0.02, center.longitude - 0.08),
        new Microsoft.Maps.Location(center.latitude + 0.02, center.longitude),
        new Microsoft.Maps.Location(center.latitude - 0.02, center.longitude),
                  new Microsoft.Maps.Location(center.latitude - 0.02, center.longitude + 0.08)], null);
     Using the foregoing code means that each latitude/longitude has to be written in. What I am trying to do is to programmatically enter a series of latitudes and longitudes for the polyline from a javascript array. It seems I should be using the Location class but I cannot work out how to do this despite researching the web for examples.
     I would appreciate someone posting a few lines of sample code showing how this is done. I take it is a simple matter so I apologise in advance,
     Best wishes, John M.
    Thursday, December 20, 2018 9:11 PM


All replies

  • Hi John,

    We have a number of wellknown data provider examples.  Looking at might be the sample that best reflects what you want to do but please do review the other data samples around this one in the interactive SDK to see if they are closer to what you are trying to accomplish.



    Thursday, December 20, 2018 9:27 PM
  • You could parse a coordinates array using parseLatLong(str: string) as documented here:

    For example:

    var coordinates = ["44.084096,-80.417484","44.633935,-77.462162","42.452304, -78.879398"];
    var locs = [];
    for (i=0; i < coordinates.length; i++) { 
    var polyline = new Microsoft.Maps.Polyline(locs, null);

    • Marked as answer by Coedwigwr Friday, December 21, 2018 8:36 PM
    Friday, December 21, 2018 4:38 PM
  • Thanks for the replies.
    I have looked around the SDK but just about all the examples seem to be of the kind I quoted above and it was not self evident as to how I could use those examplea for the construction of a subroutine inputting code from a large js array.
    The good news is that the parselatlong example is what I am looking for and should answer my problem.
    Even better news is that I am just off on my Christmas holiday so will be dealing further with the matter on my return.
    Yes I did read the Location class description but the exact use of the parse method was not clear to me. I have on other occasions  converted string coordinates to real numbers in my own function. I feel the way of using the Polyline and Location classes would be clearer if there were examples given as part of the class definitions.
    Anyway, both replies are  much appreciated.
    With best wishes for Christmas and 2018,
    John M.

    Friday, December 21, 2018 8:40 PM