2013-10-21 162 views
0

我想做一個腳本,找到用戶的當前位置,並在頁面重新載入後設置一個隨機的座標。我可以啓動地圖和隨機座標功能,但地理位置似乎沒有定位我的位置,所有它雖然在infowindow中打印出消息?地理位置谷歌地圖jquery

var map; 
    var pos; 

    function initialize() { 

    //here is the starting for the map, where it will begin to show 
    var latlng = new google.maps.LatLng(59.2982762, 17.9970823); 

    var myOptions = { 
     zoom: 13, 
     center: latlng, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
     var map = new google.maps.Map(document.getElementById('map-canvas'), myOptions); 

    //geolocation 

    if (navigator.geolocation) { 
     navigator.geolocation.getCurrentPosition(function(position) { 
      initialLocation = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); 

     var infowindow = new google.maps.InfoWindow({ 
      map: map, 
      position: pos, 
      content: "You are here" 
     }); 

      map.setCenter(pos); 
     }, function() { 
      handleNoGeolocation(true); 
     }); 
    } 

     else { 
     handleNoGeolocation(false); 
    } 

     function handleNoGeolocation(errorflag) { 
     if (errorflag) { 
      alert('Geolocation not supported'); 
      initialLocation = stockholm; 
     } 

     else { 
      alert('balblababa'); 
      initialLocation = siberia; 
     }; 

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

    //stop geolocation 

     var flagAreas = [ 

     [59.2967322, 18.0009393], 
     [59.2980245, 17.9971503], 
     [59.2981078, 17.9980875], 
     [59.2982762, 17.9970823], 
     [59.2987638, 17.9917639], 
     [59.2987649, 17.9917824], 
     [59.2987847, 17.9917731], 
     [59.2988498, 17.991684], 
     [59.2988503, 17.9981593], 
     [59.3008233, 18.0041763], 
     [59.3014033, 18.0068793], 
     [59.3016619, 18.0137766] 
     ]; 

     var random = flagAreas.sort(function() { 
     return Math.random() - 0.5 })[0]; 

     var marker = new google.maps.Marker({ 
      position: new google.maps.LatLng(random[0], random[1]), 
      map: map, 
     }); 
    } 

    window.onload = initialize; 

</script> 
+0

您的infowindow'position'設置爲錯誤的變量,將其設置爲'initialLocation'。此外,不知道你是否知道,但你聲明映射兩次(全局和本地'initialize()') - 沒有問題,因爲一切都發生在'initialize()'範圍內,但沒有良好的做法)。 –

+0

作品非常感謝你! – Chilibiff

+0

不錯,不要忘了'map.setCenter(pos);'也應該改成'map.setCenter(initialLocation);' –

回答

0

感謝Suvi Vignarajah。

這是工作代碼!

var map; 
    var pos; 

    function initialize() { 

    //here is the starting for the map, where it will begin to show 
    var latlng = new google.maps.LatLng(59.2982762, 17.9970823); 

    var myOptions = { 
     zoom: 13, 
     center: latlng, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
     var map = new google.maps.Map(document.getElementById('map-canvas'), myOptions); 

    //geolocation 

    if (navigator.geolocation) { 
     navigator.geolocation.getCurrentPosition(function(position) { 
      initialLocation = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); 

     var infowindow = new google.maps.InfoWindow({ 
      map: map, 
      position: initialLocation, 
      content: "You are here" 
     }); 

      map.setCenter(pos); 
     }, function() { 
      handleNoGeolocation(true); 
     }); 
    } 

     else { 
     handleNoGeolocation(false); 
    } 

     function handleNoGeolocation(errorflag) { 
     if (errorflag) { 
      alert('Geolocation not supported'); 
      initialLocation = stockholm; 
     } 

     else { 
      alert('balblababa'); 
      initialLocation = siberia; 
     }; 

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

    //stop geolocation 

     var flagAreas = [ 

     [59.2967322, 18.0009393], 
     [59.2980245, 17.9971503], 
     [59.2981078, 17.9980875], 
     [59.2982762, 17.9970823], 
     [59.2987638, 17.9917639], 
     [59.2987649, 17.9917824], 
     [59.2987847, 17.9917731], 
     [59.2988498, 17.991684], 
     [59.2988503, 17.9981593], 
     [59.3008233, 18.0041763], 
     [59.3014033, 18.0068793], 
     [59.3016619, 18.0137766] 
     ]; 

     var random = flagAreas.sort(function() { 
     return Math.random() - 0.5 })[0]; 

     var marker = new google.maps.Marker({ 
      position: new google.maps.LatLng(random[0], random[1]), 
      map: map, 
     }); 
    } 

    window.onload = initialize;