2017-03-03 61 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

相關問題