将经度和纬度传递到Google地图初始化函数

时间:2013-02-13 19:42:29

标签: javascript function google-maps-api-3

我正在尝试将经度和纬度传递给Google Maps JavaScript V3 api。我的代码如下所示,应该从交换机中获取googleLatLong变量,然后将其传递给Google initialize()函数。但是目前在initialize()函数中无法访问googleLatLong变量。我如何通过它?

提前致谢。

头脑中的JavaScript:

<script type="text/javascript"> 
//function to get a parameter from the query string
var queryString = function(){
  var s = window.location.search.substr(1),
      p = s.split(/\&/),
      l = p.length, 
      kv, r = {};
  if(l === 0){return false;}
    while(l--){
      kv = p[l].split(/\=/);
      r[kv[0]] = kv[1] || true;
    }
    return r;
}();

//get params from the url
var storeCode = queryString.store;

//switch page params based on the store code
switch(storeCode) {
    case 'POO746':
        var storeName = 'Poole';
        var googleLatLong = '50.736129,-1.988229';
        var trafficURL = 'http://hatrafficinfo.dft.gov.uk/feeds/rss/CurrentAndFutureEvents/South%20West.xml';
        break;
    case 'BRS464':
        var storeName = 'Bognor Regis';
        var googleLatLong = '50.798991,-0.667444';
        var trafficURL = 'http://hatrafficinfo.dft.gov.uk/feeds/rss/CurrentAndFutureEvents/South%20East.xml';
        break;
}    

</script>

身体中的JavaScript:

<script type="text/javascript">
var map

function initialize() {
    var store = new google.maps.LatLng(googleLatLong);
    var myOptions = {
        zoom:12,
        mapTypeId: google.maps.MapTypeId.HYBRID,
        disableDefaultUI: true,
        center: store
    }   
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

    var trafficLayer = new google.maps.TrafficLayer();
    trafficLayer.setMap(map);

}

window.onload = function () {
    initialize();
}
</script>


    <div id="trafficMap">
        <div id="map_canvas"></div>
</div>

3 个答案:

答案 0 :(得分:0)

让它成为页面的全局,所以你将拥有这些全局:

var storeCode = queryString.store;
var googleLatLong;
var map;

答案 1 :(得分:0)

在初始化函数中解析查询字符串。

<script type="text/javascript">
var map

  //function to get a parameter from the query string
  var queryString = function(){
    var s = window.location.search.substr(1),
      p = s.split(/\&/),
      l = p.length, 
      kv, r = {};
    if(l === 0){return false;}
      while(l--){
        kv = p[l].split(/\=/);
        r[kv[0]] = kv[1] || true;
      }
      return r;
  }();

function initialize() {


//get params from the url
var storeCode = queryString.store;

//switch page params based on the store code
switch(storeCode) {
    case 'POO746':
        var storeName = 'Poole';
        var googleLatLong = new google.maps.LatLng(50.736129,-1.988229);
        var trafficURL = 'http://hatrafficinfo.dft.gov.uk/feeds/rss/CurrentAndFutureEvents/South%20West.xml';
        break;
    case 'BRS464':
        var storeName = 'Bognor Regis';
        var googleLatLong = new google.maps.LatLng(50.798991,-0.667444);
        var trafficURL = 'http://hatrafficinfo.dft.gov.uk/feeds/rss/CurrentAndFutureEvents/South%20East.xml';
        break;
}




    var store = googleLatLong;
    var myOptions = {
        zoom:12,
        mapTypeId: google.maps.MapTypeId.HYBRID,
        disableDefaultUI: true,
        center: store
    }   
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

    var trafficLayer = new google.maps.TrafficLayer();
    trafficLayer.setMap(map);

}

window.onload = function () {
    initialize();
}
</script>

答案 2 :(得分:0)

我设法通过分割出经度和格度来解决这个问题。为经度和纬度创建单独的变量。而不是

 var googleLatLong = '50.736129,-1.988229';

我现在有:

 var glat = '50.736129';
 var glong = '-1.988229';

在Google地图初始化功能中,它现在看起来像这样:

 var store = new google.maps.LatLng(glat,glong);

不确定为什么它没有使用经度和纬度变量组合,但是你去了,做这个改变使代码工作。