Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

in my rails project some of the following code I have in a Google map is:

function initialize_google_maps() {
    var currentlatlng = new google.maps.LatLng(<%= @user.lat %>, <%= @user.lng %>);
    var zoom = <%= @kms_range %> > 9 ? 9 : 10;
    var myOptions = {
        zoom: zoom,

etc, etc...

The problem is those:

(<%= @user.lat %>, <%= @user.lng %>);

How would I convert <%= @user.lat %> and <%= @user.lng %> into a string, so javascript or jquery can understand it?

For ajax reasons, I can't put this code between script tags. I need to make it into a jquery function, assigning the values <%= @user.lat %> and <%= @user.lng %> into something that jquery can understand.

<script type="text/javascript">
  var map;
  var markers = [];

  function initialize_google_maps() {
    var currentlatlng = new google.maps.LatLng(<%= @user.lat %>, <%= @user.lng %>);
    var zoom = <%= @kms_range %> > 9 ? 9 : 10;
    var myOptions = {
        zoom: zoom,
        center: currentlatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP, // ROADMAP, SATELLITE, HYBRID
        streetViewControl: false
    };
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

    var marker = new google.maps.Marker({map: map, position: currentlatlng, icon:{oppacity:0}});
    map.setCenter(currentlatlng);
    map.setZoom(zoom);

    var circle = new google.maps.Circle({
        map: map,
        fillOpacity: 0,
        strokeWeight: 2,
        strokeOpacity: 0.7,
        radius: <%= @kms_range %>*1000,
    });
    circle.bindTo('center', marker, 'position');

  }

  function show_markers() {
    if (markers)
      for(i in markers) {
        markers[i].setMap(map);
      }
  }

  function add_marker(location) {
    marker = new google.maps.Marker({
      position: location,
      map: map
    });
    markers.push(marker);
    // markers.setVisible(false);
  }

  function initialize_markers() {
    <% (@reviews || []).each do |r| %>
      <% next unless r.lat && r.lng %>
      position = new google.maps.LatLng(<%= r.lat %>, <%= r.lng %>);
      add_marker(position);
    <% end %>
  }

  $(function() {
    initialize_google_maps();
    initialize_markers();
    show_markers();
  });

</script>
share|improve this question
    
wrap them inside "" –  Mohammad Adil Apr 22 '13 at 19:19
    
Tried that, but it's still ruby code, just with quotes around it. –  Christophe Harris Apr 22 '13 at 19:30

1 Answer 1

up vote 1 down vote accepted

If they are a double or int, you don't need to, you can just render them verbatim. If it really is a string and you need a string in js, just put quotation marks in it:

var currentlatlng = new google.maps.LatLng("<%= @user.lat %>", "<%= @user.lng %>");

Edit: according to comments:

var map_latitude = <%= at_user.lat %>;
var map_longitude = <%= at_user.lng %>;
share|improve this answer
    
Not sure what you mean. I tried <div id = "user_lat"> var currentlatlng = new google.maps.LatLng("<%= at_user.lat %>", "<%= at_user.lng %>") currentlatlng </div> and I got returned in my browser: var currentlatlng = new google.maps.LatLng("48.8051741", "2.1219587") currentlatlng –  Christophe Harris Apr 22 '13 at 19:29
1  
Pleaes show your whole code (in the question). I think you are mixing html with javascript. var currentlatlng = ... is javascript and needs to be in <script> tags. –  Femaref Apr 22 '13 at 19:30
    
Ah! There's the problem. I'm doing stuff with ajax, where stuff between scripts is not executed on a call back. So I have to make a function on the ajax side - basically displaying the map I wish I could show between my script tags. So I'm trying to convert that ruby code to something jquery can understand. i'll edit my question, with the script code. Thanks. –  Christophe Harris Apr 22 '13 at 19:38
    
So you want to display the value of currentlatlng in the user_lat div? –  Femaref Apr 22 '13 at 19:56
    
Well, ideally if I could assign <%= at_user.lat %> and <%= at_user.lng %> as decimal variables called map_latitude and map_longitude. That way, I shouldn't run into trouble with these maps - I've realised they're decimal values, which might cause problems if they're strings. Thanks. –  Christophe Harris Apr 22 '13 at 20:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.