2012-04-15 69 views
4

我目前正在使用Google Place API升級我的網站,並且我還有一個問題要解決,直到我開始工作,不明白爲什麼我會收到此錯誤消息:Uncaught TypeError: Cannot read property 'value' of null。我整個Places Library,甚至「掃描」整個example file,但我的代碼是完全一樣的。在Google Places API上獲取「Uncaught TypeError:無法讀取null值的屬性」

這裏是我的代碼:

var geocoder; 
var map; 
var myplace = '<?php echo str_replace(" ", "-", utf8_encode($_COOKIE["wrn-myplace"])); ?>'; 
var myplace_clean = '<?php echo $_COOKIE["wrn-myplace"]; ?>'; 

geocoder = new google.maps.Geocoder(); 
geocoder.geocode({'address' : myplace_clean}, function(results, status) { 
    if(status == google.maps.GeocoderStatus.OK) { 
     map.setCenter(results[0].geometry.location); 
     var myplace_center = results[0].geometry.location; 

     var marker = new google.maps.Marker({ 
      map: map, 
      position: results[0].geometry.location 
     }); 

     var myplaceControlDiv = document.createElement('div'); 
     var myplaceControl = new MyPlaceControl(myplaceControlDiv, map, myplace_center); 

     myplaceControlDiv.index = 2; 
     map.controls[google.maps.ControlPosition.TOP_RIGHT].push(myplaceControlDiv); 
    } else { 
     alert('Funktionen "geocoder" kunde inte användas på grund av följande fel: ' + status); 
    } 
}); 

var mapOptions = { 
    streetViewControl: false, 

    mapTypeControl: true, 
    navigationControlOptions: { 
     style: google.maps.NavigationControlStyle.SMALL 
    }, 

    zoomControl: true, 
    zoomControlOptions: { 
     style: google.maps.ZoomControlStyle.SMALL, 
     position: google.maps.ControlPosition.TOP_LEFT 
    }, 

    mapTypeId: google.maps.MapTypeId.ROADMAP 
}; 



map = new google.maps.Map(
    document.getElementById('weather-map'), mapOptions 
); 


var input = document.getElementById('google-search'); 
var autocomplete = new google.maps.places.Autocomplete(input); 

autocomplete.bindTo('bounds', map); 
google.maps.event.addListener(autocomplete, 'place_changed', function() { 
    var place = autocomplete.getPlace(); 
    if(place.geometry.viewport) { 
     map.fitBounds(place.geometry.viewport); 
    } else { 
     var marker = new google.maps.Marker({ 
      map: map, 
      position: place.geometry.location 
     }); 

     map.setCenter(place.geometry.location); 
     map.setZoom(17); 
    } 
}); 


var cloudControlDiv = document.createElement('div'); 
var cloudControl = new CloudControl(cloudControlDiv, map); 

cloudControlDiv.index = 1; 
map.controls[google.maps.ControlPosition.TOP_RIGHT].push(cloudControlDiv); 


var fullscreenControlDiv = document.createElement('div'); 
var fullscreenControl = new FullscreenControl(fullscreenControlDiv, map); 

fullscreenControlDiv.index = 3; 
map.controls[google.maps.ControlPosition.TOP_RIGHT].push(fullscreenControlDiv); 


var boxText = document.createElement("div"); 
boxText.style.cssText = 'background-color: #000000; border: 1px solid #ffffff; color: #eaeaea; font-family: Arial; font-size: 11px; padding: 5px 10px 5px 10px;'; 
boxText.innerHTML = 'Du är här'; 

var myPopup = { 
     content: boxText, 
     disableAutoPan: false, 
     maxWidth: 300, 
     pixelOffset: new google.maps.Size(-100, -100), 
     zIndex: null, 
     boxStyle: { 
      opacity: 0.80, 
      width: '200px' 
     }, 
     closeBoxMargin: '3px', 
     closeBoxURL: 'http://p.yusukekamiyamane.com/icons/search/fugue/icons/cross-octagon-frame.png', 
     infoBoxClearance: new google.maps.Size(1, 1), 
     isHidden: false, 
     enableEventPropagation: true 
}; 


var cloudLayer = new google.maps.weather.CloudLayer(); 
cloudLayer.setMap(map); 

elevator = new google.maps.ElevationService(); 
google.maps.event.addListener(map, 'click', getElevation); 

map.setZoom(10); 

$('#weather-data').load('jquery-fetch/fetch-weatherdata.php?place=' + myplace); 

show_position是所謂的initialize功能,這是在「說明」爲我的錯誤信息:

kK 
tK.(anonymous function).dl 
(anonymous function) 
(anonymous function)%7Bmain,places,weather%7D.js:9 
bf.(anonymous function).Hc.c%7Bmain,places,weather%7D.js:24 
O%7Bmain,places,weather%7D.js:9 
df%7Bmain,places,weather%7D.js:24 
hf%7Bmain,places,weather%7D.js:24 
(anonymous function) 
ef.controls 
(anonymous function)%7Bmain,places,weather%7D.js:24 
b%7Bmain,places,weather%7D.js:10 
bf.(anonymous function).Hc%7Bmain,places,weather%7D.js:24 
O%7Bmain,places,weather%7D.js:9 
bf.(anonymous function).Hc%7Bmain,places,weather%7D.js:24 
af.(anonymous function).ke%7Bmain,places,weather%7D.js:23 
bf.(anonymous function).Hc%7Bmain,places,weather%7D.js:24 
ff%7Bmain,places,weather%7D.js:24 
(anonymous function) 

我打電話從JS文件谷歌是這樣的:

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key={my API key}&sensor=false&libraries=weather,places"></script> 

我該如何解決我的小問題問題?

在此先感謝!

回答

8

現在問題已解決。顯然,在DIV標籤內部必須有文本字段。

+0

你能否爲他人利益更新你的答案 – Prakhar 2016-12-10 22:30:52

5

得到了同樣的錯誤。發現其ID爲Places的元素不存在。檢查錯誤的元素標識,或複製/粘貼的代碼。

相關問題