Martijn
Martijn

Reputation: 24809

How to convert longitude and latitude string to double?

I'm getting my longitude and lattitude from google:

http://maps.google.com/maps/geo?q=Sliedrecht,%20Netherlands&output=csv&oe=utf8&sensor=false

I want to add a marker to my map using this results:

        if (coordinates.Length == 4 && coordinates[0] == "200")
        {
            var overlay = new GMapOverlay(mapexplr, "overlayTwo");

            overlay.Markers.Add(new GMapMarkerGoogleGreen(new PointLatLng(Convert.ToDouble(coordinates[2]), Convert.ToDouble(coordinates[3]))));
            mapexplr.Overlays.Add(overlay);
        }

The problem is that my marker is not displayed. I think it is because the doubles aren't provided in the correct format. The result of the above link is this:

200,4,51.8248681,4.7731624

When I convert the value 4.7731624 to a double I get 47731624, without the dot.

So my question is, how do I convert the string to a double with the dot in the right place?

Upvotes: 2

Views: 13380

Answers (2)

dmarges
dmarges

Reputation: 361

just use Double.Parse() and pass the string containing the lat/long coord

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1503220

Chances are it's due to your culture - it sounds like in this case you want the invariant culture:

double longitude = double.Parse(text, CultureInfo.InvariantCulture);

(You may well want to use double.TryParse to gracefully handle the possibility of it not being a valid value.)

Upvotes: 24

Related Questions