2013-06-12 66 views
1

看到下面的代碼,我是業餘的JavaScript非常抱歉,如果它真的很糟糕。 基本上我的功能someFunc()是通過按下一個按鈕後激活的,它通過一些形式和其他功能得到了eventlocationcurrentlocation。 我假設NaN是一個錯誤,我認爲這必須是一個相當簡單的修復,我只是有一個線索。ComputeDistanceBetween返回NaN而不是距離

兩個全局變量globaleventglobalcurrent呼應的someFunc()使用文檔設定值正確的座標,但計算距離是行不通的。當我'發佈'coords並將它們作爲php變量返回時,它計算得很好。

更新:以下作品 @geocodezip讓我在正確的方向。

var map; 
var pos; 
var geocoder; 

function initialize() { 
    geocoder = new google.maps.Geocoder(); 
    var latlng = new google.maps.LatLng(-34.397, 150.644); 
    var mapOptions = { 
zoom: 3, 
center: latlng, 
mapTypeId: google.maps.MapTypeId.ROADMAP 
} 
map = new google.maps.Map(document.getElementById('map-canvas'), 
    mapOptions); 
    } 

function getLocation(){ 
if(navigator.geolocation) { 
navigator.geolocation.getCurrentPosition(function(position) { 
    pos = new google.maps.LatLng(position.coords.latitude, 
            position.coords.longitude); 
document.getElementById('distance').value = pos; 
}, function() { 
    handleNoGeolocation(true); 
}); 
} else { 
// Browser doesn't support Geolocation 
handleNoGeolocation(false); 
} 
} 


function handleNoGeolocation(errorFlag) { 
if (errorFlag) { 
var content = 'Error: The Geolocation service failed.'; 
} else { 
var content = 'Error: Your browser doesn\'t support geolocation.'; 
} 

var options = { 
map: map, 
position: new google.maps.LatLng(60, 105), 
content: content 
}; 

var infowindow = new google.maps.InfoWindow(options); 
map.setCenter(options.position); 
} 

function createLine() 
{ 

geocoder = new google.maps.Geocoder(); 
var address = document.getElementById('distance').value; 
var address2 = document.getElementById('address').value; 

var temp, temp2; 

geocoder.geocode({ 'address': address }, function (results, status) { 
temp = results[0].geometry.location; 
geocoder.geocode({ 'address': address2 }, function (results, status) { 
    temp2 = results[0].geometry.location; 

var route = [ 
    temp, 
    temp2 
]; 

var polyline = new google.maps.Polyline({ 
    path: route, 
    strokeColor: "#ff0000", 
    strokeOpacity: 0.6, 
    strokeWeight: 5 
}); 

var lengthInMeters = google.maps.geometry.spherical.computeLength(polyline.getPath()); 
var distance = lengthInMeters/1000*0.621371192; 

document.getElementById('distance2').value = distance; 

polyline.setMap(map); 
}); 
}); 
} 

google.maps.event.addDomListener(window, 'load', initialize); 
+0

發佈的代碼使用的是過時的Google Maps Javascript API v2。 – geocodezip

+0

Offhand,它看起來像globalevent和globalcurrent都是字符串,而不是數字,並且都包含非數​​字字符。 – Terry

+0

as @geocodezip指出,您使用API​​ v2代碼,而且您的'someFunc'函數使用API​​ v3代碼 - 這兩種語法不兼容 – duncan

回答

1

有一個在谷歌地圖JavaScript API第2無computeDistanceBetween方法,在該版本的谷歌地圖API的Javascript都使用GLatLng.distanceFrom

from the documentation

Note: The Google Maps JavaScript API Version 2 was officially deprecated on May 19, 2010. The original deprecation period has been extended from May 19, 2013 until November 19, 2013. As of this date, all applications requesting v2 will be served a special, wrapped version of the v3 API instead. We expect this wrapped version of the API will work for most simple maps, but we strongly encourage you to migrate your code to version 3 of the Maps JavaScript API before this date.

這就產生了一個字符串:

globalcurrent = position.coords.latitude + 「」 + position.coords.longitude;

此創建的GLatLng與這些座標:

爲V2:

globalcurrent =新的GLatLng(position.coords.latitude,position.coords.longitude);

爲V3:

globalcurrent =新google.maps.LatLng(position.coords.latitude,position.coords.longitude);

+0

感謝您的迴應,那麼如何讓他們的數字,然後因爲如果我分別得到經度和緯度值我會有4個變量放在computeDistanceBetween是這麼簡單嗎? – Purgatory

+0

看到我更新的答案。 – geocodezip

+0

謝謝,好吧,我已經將我的代碼轉換爲V3,所以試圖實現您的更新,但無濟於事,我相信我接近。看到我把它放在底部的原始帖子。 – Purgatory