Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Matthew 93 posts 153 karma points
    Jun 27, 2012 @ 07:31
    Matthew
    0

    var from razor not working in javascript

    This is a followup form my previous post. I tried Dan Diplo's suggestion, 'replace the lat/long and zoom values (from the 'hello world' gmap sample) with variables from your razor script' to learn how to get google maps working for my site but the value doesn't make it through.  This is my razor script:

    @{ var myLatLon = @Model.LatLon;
    }
    <script type="text/javascript"
    src="http://maps.googleapis.com/maps/api/js?sensor=false">
    </script>
    <script type="text/javascript">
    var myJavaLatLon = '@myLatLon';
    function initialize() {
    var myOptions = {
    center: new google.maps.LatLng('myJavaLatLon'),
    zoom: 4,
    mapTypeId: google.maps.MapTypeId.SATELLITE
    };
    var map = new google.maps.Map(document.getElementById("topimg"),
    myOptions);
    }
    </script>

    I think viewing the source shows the value is getting into the script var but not the function:

         var myJavaLatLon = '41.771312,-102.304687';
          function initialize() {
            var myOptions = {
              center: new google.maps.LatLng('myJavaLatLon'),

    but the page just shows the div with the Google Maps corner controls but no actual map.

    I was reading some posts that said you can't access a razor variable in javascript but it sure looks like it's getting in there in the source, at least to where I try to use it to define the js var.

    Can someone please straighten me out? Thanks.

  • Jonas Eriksson 930 posts 1825 karma points
    Jun 27, 2012 @ 07:57
    Jonas Eriksson
    0

    Hi, you have enclosed the var in quotes, I think this is the way you should do it:

    center:new google.maps.LatLng(myJavaLatLon),
  • Matthew 93 posts 153 karma points
    Jun 27, 2012 @ 08:32
    Matthew
    0

    Thank you for the reply, Jonas.  I tried it that way too and it produced the same result.

  • Jonas Eriksson 930 posts 1825 karma points
    Jun 27, 2012 @ 08:48
    Jonas Eriksson
    0

    Can you make it work if you enter the long and lat manually in the code? I don't see any call to Initialize() ? Should it be within $(document).ready(function(){...}); ?

    Another thing : you don't necessarily  need to add an extra razor variable, you can remove the inititial var myLatLon and use the model directly in javascript:

    var myJavaLatLon ='@Model.LatLon';


  • Matthew 93 posts 153 karma points
    Jun 27, 2012 @ 09:38
    Matthew
    0

    Yes, it works with the numbers entered manually, without any quotes around them.

    I have   <body onload="initialize()"> in the template, which I assumed was ok since it works with the manually entered numbers.

    And thanks for the tip on the redundant variable, I've removed it and it still works with manually entered numbers, but not with the var.

  • Jonas Eriksson 930 posts 1825 karma points
    Jun 27, 2012 @ 09:56
    Jonas Eriksson
    0

    Ok, so it does work with manually numbers, is the format exactly the same as when you look in the source. Try move the variable declaration to inside the initialize()

  • Matthew 93 posts 153 karma points
    Jun 27, 2012 @ 09:57
    Matthew
    0

    Ouch, I got it working by removing the other var too and just putting the @Model.LatLon directly in the google.maps:

    center: new google.maps.LatLng(@Model.LatLon),


    I will swear on a stack of Batman comics that that was the first thing I tried and it didn't work and that's why I added in all the extra junk.  Man, is my face red.  Thanks for helping me work through it though and my apologies to all for consuming world wide resources.

  • Jonas Eriksson 930 posts 1825 karma points
    Jun 27, 2012 @ 10:00
    Jonas Eriksson
    0

    Ah, it requires two variables, not one string

     center:new google.maps.LatLng(-34.397,150.644),

     

    so you'll need to split the string into two values

    something like this 

    @{

    var splitLatLong = Model.LatLng.ToString().Split(',');

    var lat =  splitLatLng [0];

    var lng =  splitLatLng [1];

    }

    center:new google.maps.LatLng(@lat,@lng),

  • Jonas Eriksson 930 posts 1825 karma points
    Jun 27, 2012 @ 10:02
    Jonas Eriksson
    0

    On a stack of Batman comics and all. :)

    NP. Glad you made it work. Cheers 

Please Sign in or register to post replies

Write your reply to:

Draft