繁体   English   中英

谷歌地图javascript API V3未显示地图

[英]google maps javascript api V3 not showing map

我正在将Maps Version2 api转换为Version3。 转换后,没有任何语法或JavaScript错误,但我的地图没有显示。 该映射将动态生成为字符串,并加载到Delphi Xe2中的TWebBrowser组件中。

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

任何帮助是极大的赞赏 !

这是到生成的文件的链接: TestLatestHtml这是到Delphi .pas文件的链接在此处输入链接描述

这是生成的代码:

<!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对于initialize函数而言是本地的),它对我有用:

工作小提琴

代码段:

 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> 

像魅力一样运作! 感谢geocodezip! 还可以通过以下链接链接到有关“无法读取null的属性'offsetWidth'”的答案。 我收到一个未捕获的引用错误:添加addDOMListener时未定义google ...我将其放置在您的示例中与offsetWidth为null相同的位置。

<script type="text/javascript">
 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);
 };
 google.maps.event.addDomListener(window, "load", initialize); 
</script>
</head>
<body>
 <script type="text/javascript" 
    src="https://maps.googleapis.com/maps/api/js?callback=initialize">
 </script>
 <title>Google Maps</title>
 <div id="map" style="width: 100%; height: 100%"></div>  
</body> 
</html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM