谷歌地图javascript api V3没有显示地图

时间:2015-10-16 20:16:07

标签: javascript google-maps delphi google-maps-api-3

我正在将地图版本2 api转换为版本3。转换后,我没有语法或javascript错误,但我的地图没有显示。地图动态生成为字符串并加载到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;

2 个答案:

答案 0 :(得分:1)

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

代码段

&#13;
&#13;
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);
};
&#13;
  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;
  }
&#13;
<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>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

像魅力一样工作!谢谢geocodezip!此外,还链接到您的回答有关&#39;无法读取属性&#39; offsetWidth&#39;为null。我得到了一个未被捕获的引用错误:当我添加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>