【问题标题】:GoogleMaps API watchPositionGoogleMaps API watchPosition
【发布时间】:2013-04-09 21:48:00
【问题描述】:

我有代码:

var watchID;
        var geoLoc;

        var geoService = navigator.geolocation;
        if (geoService) {
            geoService.getCurrentPosition(showCurrentLocation,errorHandler,{enableHighAccuracy: true});
        } else {
            alert("Your Browser does not support GeoLocation.");
        }

        function showCurrentLocation(position){
            var latLng = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);

            //Google Map options
            var myOptions = {zoom: 16,
                             center: latLng,
                             mapTypeId: google.maps.MapTypeId.ROADMAP
                            };

            var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

            var usermarker = new google.maps.Marker({position: latLng, map: map, title: "You are here!"});

            map.setCenter(latLng);
            usermarker.setPosition(latLng);
            getLocationUpdate(usermarker, latLng)
        }

        function errorHandler(error){
              alert("Error while retrieving current position. Error code: " + error.code + ",Message: " + error.message);
        }

        function getLocationUpdate(usermarker, latLng){
           if(navigator.geolocation){
              var options = {timeout:60000};
              geoLoc = navigator.geolocation;
              watchID = geoLoc.watchPosition(showLocation, 
                                             errorHandler,
                                             options);
           }else{
              alert("Sorry, browser does not support geolocation!");
           }
        }

        function showLocation(position){
            op = document.getElementById("output");
            var latitude = position.coords.latitude;
            var longitude = position.coords.longitude;

            op.innerHTML = "Latitude : " + latitude + "<br />Longitude: " + longitude;
        }

设置地图并获取我当前的位置。我正在尝试使地图居中并移动标记。 getLocationUpdate 当前触发 watchLocation,但我不知道如何更新标记和地图中心。

任何帮助表示赞赏。

【问题讨论】:

  • 您找到解决方案了吗?

标签: google-maps


【解决方案1】:

试试这个:

map.panTo(new google.maps.LatLng(position.coords.latitude, position.coords.longitude));

它以“移动地图”为中心...

【讨论】:

    【解决方案2】:

    你设置标记的位置不对,试试:

    position: new google.maps.LatLng(latLng)
    

    你可以更新标记的位置:

    newLatlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
    marker.setPosition(newLatlng);
    

    在 showLocation(position) 内部调用

    希望对你有帮助...

    【讨论】:

      猜你喜欢
      • 2022-06-20
      • 1970-01-01
      • 2011-09-01
      • 2014-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-11
      相关资源
      最近更新 更多