2017-03-03 59 views
0

我一直在这几个小时,但我似乎无法找到正确的对象进到setCenter这个特定的场景: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行中做错了什么?

+0

你不必'new'在'google.maps.LatLng'面前的时候,你正在做的'setCenter'通话,让你AREN得到一个LatLng对象,你会得到'undefined',因为对LatLng的非构造函数调用不会返回任何东西。 –

回答

0

你必须将其设置是这样的:

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

添加新的使它在俄罗斯某处中心... – user2067101

+0

@ user2067101尝试更新的代码 –

+0

更新的代码现在最初位于正确的位置,但不跟踪我的位置。它始终集中在原来的位置,然后我开车离开屏幕,然后再回到屏幕上。 – user2067101

相关问题