0

我正在尝试做一个脚本来定位用户的当前位置,并在重新加载页面后设置一个随机坐标。我可以初始化地图和随机坐标函数,但地理定位似乎无法定位我的位置,尽管它会在信息窗口中打印出消息?

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

1 回答 1

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;
于 2013-10-21T17:20:19.997 回答