1

I am trying to set the latlng of my map from a data tag using jQuery. For some reason it won't ever work. If I hard code the value its fine, but if I try and call it from the data tag it just fails but has no js errors.

Any help gratefully appreciated

http://jsfiddle.net/4jNTs/

$(function (){

    var co = $("#map").data("co");
    var map;
    var myLatlng = new google.maps.LatLng(co);
    var mapOptions = {
        center: myLatlng,
        zoom: 4
    }; 
    map = new google.maps.Map(document.getElementById("map"), mapOptions);
    var marker = new google.maps.Marker({
        position: myLatlng,
        map: map,
        title: 'title',
        icon: 'http://www.richardgrantham.com/img/marker.png'
    });
});
user989952
  • 661
  • 1
  • 12
  • 23
  • 1
    Similiar http://stackoverflow.com/questions/18117079/google-maps-api-showing-blank-map-when-given-latlng-cords-from-html5-geolocation, http://stackoverflow.com/questions/15315722/convert-52-43242-4-43242-to-google-latlng – Musa Sep 17 '13 at 23:42
  • possible duplicate of [Having problems in changing Google Maps location](http://stackoverflow.com/questions/13493271/having-problems-in-changing-google-maps-location) – geocodezip Sep 18 '13 at 00:01

1 Answers1

0

change <div id="map" data-co="-41.286713, 174.773063"></div>

to <div id="map" data-co="[-41.286713, 174.773063]"></div>

and var myLatlng = new google.maps.LatLng(co);

to var myLatlng = new google.maps.LatLng(co[0],co[1]);

The latlng constructor expects two numbers, you give it this string "-41.286713, 174.773063"