[英]Google Maps API showing blank map
我確定這是一個基本問題,但我現在已經把頭撞到牆上太多次了,所以希望有人會同情我!
我有以下示例,但它所做的只是顯示一個灰色框,根本沒有地圖。 誰能告訴我為什么?
我已經檢查過我實際上返回了一個結果,它似乎工作正常。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
html, body, #map-canvas {margin: 0;padding: 0;height: 100%;}
</style>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
var geocoder;
var map;
function initialize()
{
geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': "England"}, function(results, status)
{
if (status == google.maps.GeocoderStatus.OK)
{
var mapOptions = {
zoom: 8,
center: new google.maps.LatLng(results[0].geometry.location),
mapTypeId: google.maps.MapTypeId.ROADMAP
}
// Let's draw the map
map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
}
else
{
alert("Geocode was not successful for the following reason: " + status);
}
});
}
initialize();
</script>
</head>
<body onload="">
<div id="map-canvas" style="width: 320px; height: 480px;"></div>
</body>
</html>
嘗試調整瀏覽器窗口的大小,搖動瀏覽器/使用光標從瀏覽器選項卡拖動它,您將看到地圖出現。
由於 MVC 部分視圖谷歌地圖中的一些奇怪原因,谷歌地圖顯示為空白,您的地圖正在工作,只需調整大小即可。
用光標搖動瀏覽器窗口聽起來很有趣,但它有效,我不知道如何最好地描述它。
謝謝,阿努拉格
================================================== ======================
我的最終工作代碼如下:
`
<script type="text/javascript">
$(document).ready(function () {
(function () {
var options = {
zoom: 6,
center: new google.maps.LatLng(-2.633333, 37.233334),
mapTypeId: google.maps.MapTypeId.TERRAIN,
mapTypeControl: false
};
// init map
var map = new google.maps.Map(document.getElementById('map_canvas'), options);
var arrLocation = [];
$("#markerDiv").find("div").each(function () {
var Lat = $(this).find("input[id='Latitude']").val();
var Lon = $(this).find("input[id='Longitude']").val();
var Id = $(this).find("input[id='Id']").val();
var AssessmentDet = $(this).find("input[id='AssessmentDateTime']").val();
var LocAcc = $(this).find("input[id='LocationAccuracy']").val();
var assessorName = $(this).find("input[id='AssessorName']").val();
var partnerName = $(this).find("input[id='PartnerName']").val();
arrLocation.push({
Id: Id,
Latitude: Lat,
Longitude: Lon,
AssessmentDate: AssessmentDet,
LocationAccuracy: LocAcc,
AssessorDetail: assessorName,
PartnerName: partnerName
});
});
var allMarkers = [];
for (var i = 0; i < arrLocation.length; i++) {
//final position for marker, could be updated if another marker already exists in same position
var latlng = new google.maps.LatLng(arrLocation[i].Latitude, arrLocation[i].Longitude);
var finalLatLng = latlng;
var comparelatlng = "(" + arrLocation[i].Latitude + "," + arrLocation[i].Longitude + ")";
var copyMarker = arrLocation[i];
var marker = new google.maps.Marker({
position: new google.maps.LatLng(arrLocation[i].Latitude, arrLocation[i].Longitude),
map: map,
title: 'Equine # ' + arrLocation[i].Id,
icon:"abc.png"
});
var markerInfo = "Reference # : <b>" + arrLocation[i].Id + "</b><br/>";
markerInfo = markerInfo + "Assessor : <b>" + arrLocation[i].AssessorDetail + "</b><br/>";
markerInfo = markerInfo + "Date : <b>" + arrLocation[i].AssessmentDate + "</b><br/>";
markerInfo = markerInfo + "Partner : <b>" + arrLocation[i].PartnerName + "</b>";(function (marker, i) {
bindInfoWindow(marker, map, new google.maps.InfoWindow(), markerInfo);
})(marker, i);
}
})();
});
function bindInfoWindow(marker, map, infowindow, html) {
google.maps.event.addListener(marker, 'click', function () {
infowindow.setContent(html);
infowindow.open(map, marker);
});
}
</script>
`
results[0].geometry.location
已經是一個 latLng 對象,所以你可以說:
center: results[0].geometry.location
在這里找到工作小提琴: http : //jsfiddle.net/87z9K/
這是因為提供了“google.maps.LatLng”。 提供測試坐標,它將起作用。 更換線
center: new google.maps.LatLng(results[0].geometry.location),
和
center: new google.maps.LatLng(-34.397, 150.644)
獲取英格蘭坐標
這不完全是你的問題,但密切相關。
我發現我必須使用有效的centre
設置 mapOptions ,如下所示:
new google.maps.Map(mapCanvas, {
center: new google.maps.LatLng(-34.397, 150.644)
});
如果我沒有輸入地圖選項,或者如果我輸入了但它沒有有效的center
集,我會得到一張沒有加載圖塊的空白地圖。
如果地圖的高度/寬度為 0,也會發生這種情況。
我試圖設置地圖的 MapTypeId 並且它幫助了 Anurag 提出的建議:
map.setMapTypeId(google.maps.MapTypeId.TERRAIN);
我可以看到您的代碼存在一般的 javascript 問題。
您的腳本可能會嘗試在加載 HTML 之前將地圖嵌入到頁面中。
像這樣調用函數(還有其他方法)。
<body onload="initialize()">
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.