0

I am trying to pass a longitude and latitude to Google Maps JavaScript V3 api. My code as below should take the googleLatLong variable from the switch and then pass it to the Google initialize() function. However at the moment the googleLatLong variable is not accessible in the initialize() function. How do I pass it through?

Thanks in advance.

JavaScript in the head:

<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 in the body:

<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>
4

3 回答 3

0

使其成为页面的全局变量,因此您将拥有这些全局变量:

var storeCode = queryString.store;
var googleLatLong;
var map;
于 2013-02-13T19:52:59.220 回答
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>
于 2013-02-13T20:01:43.577 回答
0

我最终通过拆分经度和纬度来解决这个问题。为经度和纬度创建单独的变量。代替

 var googleLatLong = '50.736129,-1.988229';

我现在有:

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

在谷歌地图初始化函数中,它现在看起来像这样:

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

不太确定为什么它不能与组合的经度和纬度变量一起使用,但是你去了,使这个改变使代码工作。

于 2013-02-14T01:17:27.177 回答