2013-03-04 76 views
0

我得到这个错误我的代码。我不确定为什么我会得到它,尽管我没有太多的地理定位经验。我认为问题可能在于获得经纬度的位置,但我并不积极。如果任何人有关于如何修改我的代码/想法的建议,为什么这不起作用,将不胜感激。无效值属性<map>错误

function callGeo() { 
console.log("you called findLocation"); 
if(navigator.geolocation) { 
    navigator.geolocation.getCurrentPosition(getLocation, locationError); 
    } 
else { console.log("no geolocation support"); 
     return; 
     } 
} 

function getLocation(position) { 
    var latitude = position.coords.latitude; 
    var longitude = position.coords.longitude; 
    if (!map) { 
     showMap(latitude, longitude); 
     } 
    addMarker(latitude, longitude); 
} 

function showMap(lat, long) { 
    var googleLatLong = new google.maps.LatLng(lat, long); 
    var mapOptions = { 
     zoom: 12, 
     center: googleLatLong, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
     }; 
    var mapDiv = document.getElementById("map"); 
    map = new google.maps.Map(mapDiv, mapOptions); 
    map.panTo(googleLatLong); 
    } 

function addMarker(lat, long) { 
    var googleLatLong = new google.maps.LatLng(lat, long); 
    var markerOptions = { 
    position: googleLatLong, 
    map: map, 
    title: "Where I'm thinking today" 
    } 
    var marker = new google.maps.Marker(markerOptions); 
    } 

function locationError(error) { 
    var errorTypes = { 
     0: "Unknown error", 
     1: "Permission denied by user", 
     2: "Position not available", 
     3: "Request timed out" 
    }; 
    var errorMessage = errorTypes[error.code]; 
    if (error.code == 0 || error.code == 2) { 
     errorMessage += " " + error.message; 
    } 
    console.log(errorMessage); 
    alert(errorMessage); 
} 
+0

提供从浏览器控制台抛出详细的错误......在主题提到的错误没有什么意义 – charlietfl 2013-03-04 04:51:05

回答

0

对不起,我只是想通了。我正在检查是否(!地图)。我将它改为if(map),现在它可以工作。

相关问题