简体   繁体   English

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

[英]Passing longitude and latitude into Google maps initialize function

I am trying to pass a longitude and latitude to Google Maps JavaScript V3 api. 我正在尝试将经度和纬度传递给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. 我的如下代码应从开关中获取googleLatLong变量,然后将其传递给Google initialize()函数。 However at the moment the googleLatLong variable is not accessible in the initialize() function. 但是,目前无法在initialize()函数中访问googleLatLong变量。 How do I pass it through? 如何通过?

Thanks in advance. 提前致谢。

JavaScript in the head: 头中的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 in the body: 体内的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>

Make it global to the page, so you'll have these globals: 将其设置为页面的全局变量,这样您将拥有以下全局变量:

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

Parse the query string in your initialize function. 在初始化函数中解析查询字符串。

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

I managed to fix this in the end by splitting out the longitude and lattitude. 我设法通过拆分经度和纬度来解决此问题。 Creating seperate vars for longitude and latitude. 为经度和纬度创建单独的变量。 Instead of 代替

 var googleLatLong = '50.736129,-1.988229';

I now have: 我现在有:

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

and in the Google Maps initialize function it now looks like this: 现在在Google Maps的初始化函数中,它看起来像这样:

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

Not really sure why it wasn't working with the combined longitude and latitude variable but there you go, making this change made the code work. 不太确定为什么它不能与经度和纬度组合在一起使用,但是您去了,进行此更改使代码正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM