2017-07-03 84 views
2
function initAutocomplete() { 
    var lat=document.getElementById('lat').value; 
    var lng=document.getElementById('lng').value; 
    console.log(lat); 
    console.log(lng); 


    var map = new google.maps.Map(document.getElementById('map'), { 
     center: {lat:lat, lng:lng}, 
     zoom: 13, 
     mapTypeId: 'roadmap' 
    });} 

它給了我下面的錯誤:錯誤:InvalidValueError:setCenter:不是經緯度或LatLngLiteral:物業緯度:不是一個數字

error:InvalidValueError: setCenter: not a LatLng or LatLngLiteral: in property lat: not a number

+1

'.value'返回一個字符串 - > ['parseFloat()'](https://開頭developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat) – Andreas

+0

錯誤已解決,但可以幫助找到位置..實際上位置是如何在地圖上,但如何顯示標記那個位置? .http: – ravneet

+0

將其作爲單獨問題發佈(其中應包括[最小,完整和可驗證的示例](https://stackoverflow.com/help/mcve/)(但不要發佈您的API密鑰;)) – Andreas

回答

8

一個HTMLInputElement.value屬性返回值作爲串。

你必須把它傳遞給地圖API之前解析的lat的內容和lngparseFloat()

function initAutocomplete() { 
    var lat = parseFloat(document.getElementById('lat').value); 
    var lng = parseFloat(document.getElementById('lng').value); 

    var map = new google.maps.Map(document.getElementById('map'), { 
     center: { 
      lat: lat, 
      lng: lng 
     }, 
     zoom: 13, 
     mapTypeId: 'roadmap' 
    }); 
} 
+0

Thanx兄弟的幫助...它解決了:)) – ravneet