English 中文(简体)
使用 JavaScript 获取经度和纬度, 并将其设置为 vars
原标题:Get Longitude and Latitude using JavaScript and set these to vars

今天是我使用Google地图API的第一天。

function initializeMap() {

        var opts = {
            center: new google.maps.LatLng( 0, 0), // ignore the zero values for now
            zoom: 10,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(document.getElementById( map ), opts);
        geocoder = new google.maps.Geocoder();
}

我用 window. onload = 初始化Map 调用它。

好吧,我想把地图放在我目前的位置。

navigator.geolocation.getCurrentPosition(displayLocation);

function displayLocation(position) {
    var latitude = position.coords.latitude;
    var longitude = position.coords.longitude; 
 }

现在,我对位置对象如何被推回成功处理器显示位置略感迷茫。 不管怎么说,我怎样才能通过纬度和坡度; 经度到初始化马普() 函数, 像这样...

center: new google.maps.LatLng( latitude, longitude)

目前我正在看谷歌地图 API 的 API 文档,因为我肯定有办法得到这个功能, 但我喜欢使用不同函数的纬度和经度, 所以我想我问我如何让这些变量可以进入所有函数( 如全球功能, 但不是坏全球功能), 而不是只用于 < code> displayLocation 函数 。 抱歉, 如果这个词写得不好... 如果我混淆了人们的话, 请说, 我会重新写这个问题 。

最佳回答

仅声明

var latitude, longitude;

进入母范围,所以该函数是

function displayLocation(position) {
    latitude = position.coords.latitude; /* no var keyword */
    longitude = position.coords.longitude; 
}

当然了,您应该选择定义这些变量的范围,这取决于您。此外,请注意,这些变量将被 unfreed 定义为 ,直到 displayLocation 不被调用

问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签