Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript setCenter不';i don’我好像不接受任何东西_Javascript - Fatal编程技术网

Javascript setCenter不';i don’我好像不接受任何东西

Javascript setCenter不';i don’我好像不接受任何东西,javascript,Javascript,我已经为此工作了好几个小时,但似乎找不到适用于此特定场景的setCenter的正确对象提要: var map, currentPositionMarker, mapCenter = new google.maps.LatLng(14.668626, 121.24295) function initializeMap(){ map = new google.maps.Map(_('map'), { zoom:

我已经为此工作了好几个小时,但似乎找不到适用于此特定场景的setCenter的正确对象提要:

    var map,
        currentPositionMarker,
        mapCenter = new google.maps.LatLng(14.668626, 121.24295)

    function initializeMap(){
        map = new google.maps.Map(_('map'), {
           zoom: 18,
           center: mapCenter,
            mapTypeId: 'roadmap',
            mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU}
         });
    }

    function locError(error) { alert("current position not be found");}

    function setCurrentPosition(pos) {

        currentPositionMarker = new google.maps.Marker({
            map: map,
            position: new google.maps.LatLng(
                pos.coords.latitude,
                pos.coords.longitude
            ),
            title: "Current Position"
        });
        map.panTo(new google.maps.LatLng(
                pos.coords.latitude,
                pos.coords.longitude
            ));

    }

    function displayAndWatch(position) {

        setCurrentPosition(position);
        watchCurrentPosition(position);
    }

    function watchCurrentPosition(pos) {

        var positionTimer = navigator.geolocation.watchPosition(
            function (position) {
                setMarkerPosition(
                    currentPositionMarker,
                    position
                );
                **map.setCenter(google.maps.LatLng(pos.coords.latitude,pos.coords.latitude));**
            });
    }

    function setMarkerPosition(marker, position) {
        marker.setPosition(
            new google.maps.LatLng(
                position.coords.latitude,
                position.coords.longitude)
        );
    }

    function initLocationProcedure() {
        initializeMap();
         infoWindow = new google.maps.InfoWindow({  maxWidth: 400 });
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(displayAndWatch, locError);
        } else { alert("Geolocation API not supported");  }
    }
(尤其令人沮丧的是,我不得不开车四处转转,以验证每项更改是否有效!)。
有人知道我在地图上做错了什么吗?设置上面的中心线?

你必须这样设置:

map.setCenter(new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude));

当您执行
setCenter
调用时,在
google.maps.LatLng
前面没有
new
,因此您没有得到一个LatLng对象,因为对LatLng的非构造函数调用不会返回任何内容,所以您得到的是
undefined
。更新的代码现在最初集中在正确的位置,但没有跟踪我的位置。它保持在原始位置的中心,我开车离开屏幕,稍后再回到屏幕上。