我使用了来自this Stack Overflow Thread的代码,非常感谢你。我使用了Google Javascript Maps API中的Streetview代码。但是,我一无所获。警报(纬度)工作和警报(经度)工作。但是当我尝试显示街景时,它会说:“未捕获的ReferenceError:未定义纬度”。我尝试将警报中返回的值粘贴到StreetView代码中,然后DIV变为灰色。我很难过。
<div id="map" style="border-width:medium;border-style:solid; border-color:Red;width:200px;height:150px;"></div>
<script type="text/javascript">
function initMap(){
var geocoder = new google.maps.Geocoder();
var address = "new york";
geocoder.geocode({ 'address': address }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
alert(latitude);
alert(longitude);
}
});
var panorama;
panorama = new google.maps.StreetViewPanorama(
document.getElementById('map'),
{
position: { lat: latitude, lng: longitude },
pov: { heading: 165, pitch: 0 },
zoom: 1
});
}
</script>
答案 0 :(得分:0)
试试这个。我没有经过测试就写了它,但它应该给你一个起点。
<div id="map" style="border-width:medium;border-style:solid; border-color:Red;width:200px;height:150px;"></div>
<script type="text/javascript">
function initMap() {
var geocoder = new google.maps.Geocoder(),
address = "new york",
// this function will get called inside geocode's callback
// when we have received the async data
renderStreetView = function(lat, lng) {
var panorama,
map = document.getElementById('map');
panorama = new google.maps.StreetViewPanorama(map, {
position: { lat: lat, lng: lng },
pov: { heading: 165, pitch: 0 },
zoom: 1
});
};
geocoder.geocode({ 'address': address }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latitude = results[0].geometry.location.lat(),
longitude = results[0].geometry.location.lng();
alert(latitude);
alert(longitude);
renderStreetView(latitude, longitude);
}
});
}
</script>
答案 1 :(得分:0)
显然,40.712784,-74.005941没有街景数据(地理编码的结果&#34;纽约&#34;)。如果我把它改成&#34;时代广场,纽约&#34; (并在地理编码器回调函数中创建全景图)我得到一张全景图(在40.759011,-73.984472)。
代码段
function initMap() {
var geocoder = new google.maps.Geocoder();
var address = "times square, new york"; // "new york"; //
geocoder.geocode({
'address': address
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
document.getElementById('coord').innerHTML += results[0].geometry.location.toUrlValue(6) + "</br>";
var panorama;
panorama = new google.maps.StreetViewPanorama(
document.getElementById('map'), {
position: results[0].geometry.location,
pov: {
heading: 165,
pitch: 0
},
zoom: 1,
visible: true
});
}
});
}
google.maps.event.addDomListener(window, 'load', initMap);
&#13;
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="coord"></div>
<div id="map" style="border-width:medium;border-style:solid; border-color:Red;width:400px;height:300px;"></div>
&#13;