-8

I have some point coordinates like below:

-123.118069008,49.2761419674,0 -123.116802056,49.2752350159,0 -123.115385004,49.2743520328,0 -123.114912944,49.2738039982,.............. -123.118069008,49.2761419674,0

Can you please let me know how I can use C# to create something like this:

new google.maps.LatLng(-123.118069008,49.2761419674),
new google.maps.LatLng(-123.116802056,49.2752350159),
.....
new google.maps.LatLng(-123.118069008,49.2761419674)];

As you can see I need to:

  1. Add the "new google.maps.LatLng(),"
  2. Remove the 0 from -123.118069008,49.2761419674,0
  3. Parse it until the last line which terminate with ;

Thanks

DigitalNomad
  • 428
  • 1
  • 3
  • 18
Suffii
  • 5,694
  • 15
  • 55
  • 92
  • 7
    Have you tried anything? – Saggio Mar 07 '13 at 21:51
  • A regular expression would be your easiest solution. [This question has you halfway there.][1] [1]: http://stackoverflow.com/questions/3518504/regular-expression-for-matching-latitude-longitude-coordinates – DigitalNomad Mar 07 '13 at 21:57

2 Answers2

1

Here's the simplest way to do it that I know of...

        var txt = "-123.118069008,49.2761419674,0 -123.116802056,49.2752350159,0 -123.115385004,49.2743520328,0";
        var output = new StringBuilder();
        foreach (var group in txt.Split(' '))
        {
            var parts = group.Split(',');
            var lat = double.Parse(parts[0]);
            var lng = double.Parse(parts[1]);
            if (output.Length > 0)
                output.AppendLine(",");
            output.Append("new google.maps.LatLng("+lat+","+lng+")");
        }
        MessageBox.Show("["+output+"]");

The result is...

[new google.maps.LatLng(-123.118069008,49.2761419674),

new google.maps.LatLng(-123.116802056,49.2752350159),

new google.maps.LatLng(-123.115385004,49.2743520328)]

Gabe Halsmer
  • 808
  • 1
  • 9
  • 18
0

Untested and not compiled, but this should get you started

var items = data.Split(',');
for(int i = 0; i < items.Length; i = i + 2)
{
    var lat = int.Parse(items[i].Replace("0-","-"));
    var lng = int.Parse(items[i+1]);
}

You'll want to do array bounds checking, etc, hopefully this still helps.

Nate
  • 30,286
  • 23
  • 113
  • 184