2013-02-04 40 views
0

我試圖在地圖初始化後在地圖上繪製標記。不過,我需要知道中心點。Google Maps v3 jquery mobile獲取中心的緯度和經度

我試過使用這兩個但我得到「未捕獲RangeError:最大調用堆棧大小超過」在鉻js;

$('#map_canvas').gmap('get', 'map').getCenter()); and also 
$('#map_canvas').gmap('option', 'center'); 

<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script> 
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script> 
<script type="text/javascript" src="jquery-ui-map-3.0-rc/ui/jquery.ui.map.js"></script> 
<script type="text/javascript" src="jquery-ui-map-3.0-rc/ui/jquery.ui.map.extensions.js"></script> 
<script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script> 
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" /> 

<script> 
$('#map_page').live("pageshow", function() { 
    function add_markers() { 
     var location = $('#map_canvas').gmap('option', 'center'); 
    $.getJSON('http://localhost/motherly/android/index.php/api/venues?lat=' + location.lat() + '&lng=' + location.lng(), function(data) { 

     console.log(data['response']); 

     $.each(data.response, function(i, marker) { 
      // console.log(marker.lat, marker.lng); 
      $('#map_canvas').gmap('addMarker', { 
       'position': new google.maps.LatLng(marker.lat, marker.lng), 
       'bounds': true 
      }).click(function() { 
       $('#map_canvas').gmap('openInfoWindow', { 'content': marker.name }, this); 
      }); 
     }); 
    }); 
    } 

    function initialize(lat,lng) { 
     $('#map_canvas').gmap({'center' : new google.maps.LatLng(lat, lng), 'callback': function(){ 
      add_markers(); 
     }}); 
    } 

    if(navigator.geolocation) { 
     navigator.geolocation.getCurrentPosition(function(position){ 
      console.log(position); 
      initialize(position.coords.latitude,position.coords.longitude); 
      // google.maps.event 
     }); 
    } 
}); 

回答

0

道歉傢伙我認識的問題是,我打電話裏面抽籤方法爲這是有道理的,地圖對象未初始化地圖的構造。

因此,要重述的問題是add_markers()方法在此構造函數內(將add_markers()移動到此行下方);

$('#map_canvas').gmap({'center' : new google.maps.LatLng(lat, lng), 'callback': function(){ 

}}); 
相關問題