gpt4 book ai didi

javascript - 谷歌地图 javascript api V3 不显示 map

转载 作者:行者123 更新时间:2023-12-03 08:37:07 26 4
gpt4 key购买 nike

我正在将 map 版本 2 api 转换为版本 3。转换后,我没有收到任何语法或 JavaScript 错误,但我的 map 没有显示。该 map 动态生成为字符串并加载到 Delphi Xe2 中的 TWebBrowser 组件中。

为了调试,我在生成 html 后设置了一个断点,这样我就可以将其复制到 html 文件中并使用 chrome 或 IE 浏览它。

非常感谢任何帮助!

这是生成的文件的链接: TestLatestHtml以下是 Delphi .pas 文件的链接: enter link description here

这是生成的代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0    Strict//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
<head>
<title>Google Maps</title>
<meta name="viewport" content="initial-scale=1.0"/>
<meta charset=utf-8"/>
<style type="text/css">
html, body {width: 100%; height: 100px;}
body {margin-top: 0px; margin-right: 0px; margin-left: 0px; margin-bottom: 0px;}
#map {height: 400px; margin: 0; padding: 0;}
</Style>
<script type="text/javascript">
function GetMarker(csid, myLat, MyLong, premise) {
if (premise) {
var marker = new google.maps.Marker({position: {lat:28.3280638, lng:-81.4025985}, label: csid, map: map, icon: icon1});
}
else {
var marker = new google.maps.Marker({position: {lat: 28.3280638, lng: -81.4025985}, label: csid, map: map, icon: icon2});
}
}
function NewMarker(Info, MyLat,MyLong, premise) {
var mk2 = GetMarker(Info, MyLat, MyLong, premise);
};
</script>
</head>

<body>
<div id="map" style="width: 600px; height: 400px"></div>
<script>
function initialize() {
var map; var icon1,icon2;
map = new google.maps.Map(document.getElementById("map"), {center: {lat:28.3280638, lng:-81.4025985}, zoom: 15});
icon1 = new GIcon(G_DEFAULT_ICON, "http://www.unisdr.org/campaign/resilientcities/assets/images/template/google-maps-blue-pin.png");
icon2 = new GIcon(G_DEFAULT_ICON, "http://www.unisdr.org/campaign/resilientcities/assets/images/template/google-maps-red-pin.png");
var mk = NewMarker("<b>Charles~Cassandra</b><br/>Testor<br/> (407)870-0040",28.3280638 ,-81.4025985, true);
};
</script>
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyC39BI6ULNfGzXW_ABqLwuvPx-Sm9mYGcY"&callback=initialize>
</script>
</body>
</html>

这是生成代码的 dephi 函数:

function TGoogleMap.GetMapHTML(MarketHeader:String; MyLat, MyLong : String): String;

function GetSubAddress:String;
var
loop : Integer;
begin
Result := '';
for loop := low(FMarkers) to High(Fmarkers) do begin
if (Fmarkers[loop].Lat <> '' ) then
Result := Result + ' AddMarker("'+ Fmarkers[loop].Info + '",' + Fmarkers[loop].Lat + ' ,' + Fmarkers[loop].Lon +',false); ' + #13#10 ;
// Result := Result + ' map.addOverlay(GetMarker("'+ Fmarkers[loop].Info + '",' + Fmarkers[loop].Lat + ' ,' + Fmarkers[loop].Lon +',false)); ' + #13#10 ;
end;

end;

begin
Result :=
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" ' +
' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> ' +
'<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml"> ' +
' <head> ' +
' <title>Google Maps</title> ' +
' <meta name="viewport" content="initial-scale=1.0"/> ' +
' <meta charset=utf-8"/> ' +

' <style type="text/css">' +
// ' v\:* {behavior:url(#default#VML);}' +
' html, body {width: 100%; height: 100%;}' +
' body {margin-top: 0px; margin-right: 0px; margin-left: 0px; margin-bottom: 0px;}' +
' </Style>' +

' <script type="text/javascript"> ' +

' function GetMarker(csid, myLat, MyLong, premise) { ' +
// ' var point = new GLatLng(MyLat,MyLong); ' +
' if (premise) {' +
' var marker = new google.maps.Marker({' +
' position: {lat:' +myLat+ ', lng:' +MyLong+ '},' +
' label: csid,' +
' map: map,' +
' icon: icon1' +
' });' +
' }' +
' else {' +
' var marker = new google.maps.Marker({' +
' position: {lat: ' +myLat+', lng: ' +MyLong+ '},' +
' label: csid,' +
' map: map,' +
' icon: icon2' +
' });' +
' }' +

// ' google.maps.event.addListener(map, "click", function(event) { ' +
// ' marker.openInfoWindowHtml(csid); ' +
// '
// ' }); ' +
// ' return marker; ' +
' } ' +

' function NewMarker(Info, MyLat,MyLong, premise) {' +
' var mk2 = GetMarker(Info, MyLat, MyLong, premise); ' +
// ' map.addOverlay(mk2);'+
' };' +
' </script> ' +

' </head> ' +
// ' <body onload="initialize()" onunload="GUnload()"> ' +
' <body> ' +
' <div id="map" style="width: 600px; height: 300px"></div> ' +
' <script>' +
' function initialize() { ' +
' var map;' +
' var icon1,icon2; ' +

' map = new google.maps.Map(document.getElementById("map"), {center: {lat:' + MyLat +', lng:' + MyLong + '}, zoom: 15}); ' +
' icon1 = new GIcon(G_DEFAULT_ICON, "http://www.unisdr.org/campaign/resilientcities/assets/images/template/google-maps-blue-pin.png");' +
' icon2 = new GIcon(G_DEFAULT_ICON, "http://www.unisdr.org/campaign/resilientcities/assets/images/template/google-maps-red-pin.png");' ;

Result := Result + GetSubAddress; // retrieve the additional markers to display.
Result := Result +

' var mk = NewMarker("' + MarketHeader + '",' + MyLat + ' ,' +MyLong +', true);' +
// ' map.addOverlay(mk); ' +
// ' map.setCenter(new GLatLng(' + Lat + ' ,' +Long +'), ' + IntToStr(FZoomLevel) + '); ' +
// ' map.addControl(new GScaleControl()); ' +
// ' map.addControl(new GOverviewMapControl()); ' +

' }; ' +
' </script>' +
' <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyC39BI6ULNfGzXW_ABqLwuvPx-Sm9mYGcY" &callback=initialize></script>' +
' </body> ' +
'</html> ';

mmWeb.Lines.Add('************************');
mmWeb.Lines.Add(Result);

end;

最佳答案

Google Maps Javascript API v3 不支持 GIcon(这是 Google Maps Javascript API v2 的事情,所有 Gxxxx 类都被 google.maps.xxxx 类替换)

使用 google.maps.Icon改为匿名对象:

icon1 = {
url: "http://www.unisdr.org/campaign/resilientcities/assets/images/template/google-maps-blue-pin.png"
};
icon2 = {
url: "http://www.unisdr.org/campaign/resilientcities/assets/images/template/google-maps-red-pin.png"
};

一些小范围问题( map 、icon1、icon2 是初始化函数的本地问题),它对我有用:

working fiddle

代码片段:

var map;
var icon1, icon2;

function GetMarker(csid, myLat, MyLong, premise) {
if (premise) {
var marker = new google.maps.Marker({
position: {
lat: 28.3280638,
lng: -81.4025985
},
label: csid,
map: map,
icon: icon1
});
} else {
var marker = new google.maps.Marker({
position: {
lat: 28.3280638,
lng: -81.4025985
},
label: csid,
map: map,
icon: icon2
});
}
}

function NewMarker(Info, MyLat, MyLong, premise) {
var mk2 = GetMarker(Info, MyLat, MyLong, premise);
};

function initialize() {
map = new google.maps.Map(document.getElementById("map"), {
center: {
lat: 28.3280638,
lng: -81.4025985
},
zoom: 15
});
icon1 = {
url: "http://www.unisdr.org/campaign/resilientcities/assets/images/template/google-maps-blue-pin.png"
};
icon2 = {
url: "http://www.unisdr.org/campaign/resilientcities/assets/images/template/google-maps-red-pin.png"
};
var mk = NewMarker("<b>Charles~Cassandra</b><br/>Testor<br/> (407)870-0040", 28.3280638, -81.4025985, true);
};
  html,
body {
width: 100%;
height: 100px;
}
body {
margin-top: 0px;
margin-right: 0px;
margin-left: 0px;
margin-bottom: 0px;
}
#map {
height: 400px;
margin: 0;
padding: 0;
}
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?callback=initialize">
</script>
<title>Google Maps</title>
<div id="map" style="width: 600px; height: 400px"></div>

关于javascript - 谷歌地图 javascript api V3 不显示 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33178867/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com