1

i did this code File Real.cs

public ActionResult Index() {
    IList<Eventdata> liste = DATA2.Eventdata.GetEventdata();
    int a = liste.Count;

    float lat = liste[a-2].Latitude;
    float longi = liste[a-2].Longitude;
    IList<float> model = new List<float>();
    model.Add(lat);
    model.Add(longi);
    return View(model);
}

i mapped the table EventData : it contains two attributs floats ( Latitude,Longitude) and i want to know its last values.

File Index.CShtml

@model IList<float> @{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Lay3.cshtml";  
}

    <body  onload="initialize(@Model[0],@Model[1])">
    <div id="map_canvas" style="width: 800px; height: 500px;">
    </div>
    <script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript">
    </script>
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false">
    </script>
    <script type="text/javascript">

    function initialize(a, b) {
        alert(a);
        alert(b);
        var myLatLng = new google.maps.LatLng(a,b);

        var myOptions = {
            zoom: 30,
            center: myLatLng,
            mapTypeId: google.maps.MapTypeId.TERRAIN
        };

        var map = new google.maps.Map(document.getElementById("map_canvas"),
        myOptions);
        var flightPlanCoordinates = [
        new google.maps.LatLng(a,b)
        ];

        var flightmarker = new google.maps.Marker({
   position: new google.maps.LatLng(a,b),
  map: map, title: " denden"
        });
    }    

    </script>
    </body>

and i have the last point like that : Latitude=34.75064 and the longitude=10.761744 . but the messages are : 34 and 75064. Why? And how can i correct it.

1
  • It's a shot in the dark, but try changing float to double and check again if it works ? Commented Sep 15, 2012 at 11:53

2 Answers 2

1

If your server or application uses culture other than "en-US" then I believe the problem is in line with

onload="initialize(@Model[0],@Model[1])"

Here you implicitly use C# .ToString method on float type and pass it to javascript. However, C# takes into account culture information, and javascript deals only with en culture.

You should explicitly set culture in conversion, something like:

  @model IList<float>
    @{
        ViewBag.Title = "Index";
        Layout = "~/Views/Shared/_Lay3.cshtml";
        CultureInfo culture = CultureInfo.GetCultureInfo("en-US");
        string latitude = Model[0].ToString(culture);
        string longitude = Model[1].ToString(culture);
    }
    <body onload="initialize(@latitude,@longitude)">
edit: GetCultureInfo
Sign up to request clarification or add additional context in comments.

Comments

0

Try using( or set culture with '.' decimal digit separator)

<body  onload="initialize('@Model[0]','@Model[1]')"> // This will pass your value as
//two parameters(instead of four) but it can be used only as a string because
//javascript can parse decimal values only with '.' separator

Instead of

<body  onload="initialize(@Model[0],@Model[1])">

If you call initialize method with 34,75064 and 10,761744 you are actually passing four parameters. Because of that a = 34 and b = 75064.

You need to check culture when passing parameters from server to client because when culture have ',' insead of '.' for decimal digit separator your code wont work as expected.

In your case with map you need decimal value, so you can't use

<body  onload="initialize('@Model[0]','@Model[1]')">

You need to covert your decimal to appropriate culture like this

var culture = CultureInfo.GetCultureInfo("en-US");
...
<body  onload="initialize(@Model[0].ToString(culture), @Model[1].ToString(culture))">

5 Comments

if i do <body onload="initialize('@Model[0]','@Model[1]')"> the map become empty!!
Map is empty because you are actually passing '34,75064' and '10,761744'. These values cant be parsed as decimal because separator is ',' instead of '.'. For example if you try '1,5' * 2 you will get NaN, and if you try '1.5' * 2 you will get result 3
the soltuion is to use ParsetoFloat(a) and ParsetoFloat(b)
If you use javascript parseFloat function, then when you say parseFloat('1.25') you will get 1.25, but when you say parseFloat('1,25') you will get 1. So i don't think this is what you want to get for you location? So, i suggest you to use value.ToString(CultureInfo.GetCultureInfo("en-US")) on the server side. If you set some culture where decimal separator is ',' you will have same problem. So, set culture to en-US.You may only edit a comment every 5 seconds.(click on this box to dismiss)
i did like you say but it gives me two values and it repeat for example i have 5 objects EventData but the code returns the first two values corrects but it repeat the same values in the rest!!!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.