سؤال

I use the following razor code to generate some javascript to produce markers on a Google map.

@foreach(Point point in Model.Points.Take(3))
{
    String longitude = point.Longitude.ToString(CultureInfo.InvariantCulture);
    String latitude = point.Latitude.ToString(CultureInfo.InvariantCulture);
    <text>
        var location = new google.maps.LatLng(@(longitude), @(latitude));
        bounds.extend(location);
        var marker = new google.maps.Marker({
            position: location,
            map: map
        });
    </text>
}

In development, this correctly becomes:

var location = new google.maps.LatLng(52.2124273, 5.9545532);
bounds.extend(location);
var marker = new google.maps.Marker({
    position: location,
    map: map
});

However, on our production server, it becomes:

var location = new google.maps.LatLng(522124273000, 59545532000);
bounds.extend(location);
var marker = new google.maps.Marker({
    position: location,
    map: map
});

Which is producing just a grey Google map. What is causing this strange ToString behavior?

edit

The Point class is a custom class, not from a library. Here are the relevant parts:

public class Point
{
    private Double latitude;
    private Double longitude;

    public Double Latitude
    {
        get
        {
            return latitude;
        }
    }

    public Double Longitude
    {
        get
        {
            return longitude;
        }
    }
}
هل كانت مفيدة؟

المحلول

JK and AlexC were right, see their comments on my question. The data was coming in wrong from the external API. Setting the global culture to en-US solved the issue.

I only do not understand one thing: I'm using a library which uses JSON.net, everywhere in the documentation it says the parsing is done with InvariantCulture. So I would think the global culture would not make a difference.

نصائح أخرى

1) Have you noticed that you have lat and lng the wrong way round in:

var location = new google.maps.LatLng(@(longitude), @(latitude));

2) I would check on production server that the data is correct - emit point.Longitude etc directly to the output as text somewhere to see if it is actually the ToString that is at fault and try (23.456).ToString() or similar too.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top