将bing maps页面加载到Chromium浏览器中(在Delphi中)

时间:2017-08-25 19:19:10

标签: bing-maps chromium

此代码创建一个HTML页面,用于加载特定地址(由程序传递)的bing映射,然后将该页面加载到Chromium浏览器中。我已经测试了它产生的HTML文件,如果我双击该文件,我的默认浏览器将打开并正确加载页面。但是,当我尝试将此页面加载到程序中的Chromium浏览器窗口(过去曾为其他页面工作)时,只显示标题(来自 div id =“TitleDiv”的材料)但地图从未显示过。我试图找出为什么在手动加载时正确加载时以编程方式加载到浏览器中时不加载HTML。

我在使用和不使用 ChromWebPage.Browser.Reload; 行时尝试过此操作。

这是我的精确Delphi代码,除了我在这里省略了我们的Bing地图键。 (使用Delphi获取文本的最简单方法,我知道是否使用TStringList,这就是代码格式化的原因。)

  mMap := TStringList.Create;
  with mMap do begin
    Add('<!DOCTYPE html> ' );
    Add('<html> ' );
    Add('<head>');
    Add('<title>' + sCaption + '</title> ');
    Add('    <meta charset="utf-8" /> ');
    Add('    <script type="text/javascript"> ');
    Add('    var map, searchManager; ');
    Add('    function GetMap() { ');
    Add('        map = new Microsoft.Maps.Map("#myMap", { ');
    Add('            credentials: "KEY-HERE" ');
    Add('        }); ');
    //Make a request to geocode passed address
    Add('        geocodeQuery("' + sAddress + '"); ');
    Add('    } ');
    Add('    function geocodeQuery(query) { ');
    //If search manager is not defined, load the search module.
    Add('        if (!searchManager) { ');
    // Create an instance of the search manager and call the geocodeQuery function again.
    Add('            Microsoft.Maps.loadModule("Microsoft.Maps.Search", function () { ');
    Add('                searchManager = new Microsoft.Maps.Search.SearchManager(map); ');
    Add('                geocodeQuery(query); ');
    Add('            }); ');
    Add('        } else { ');
    Add('            var searchRequest = { ');
    Add('                where: query, ');
    Add('                callback: function (r) { ');
    // Add the first result to the map and zoom into it.
    Add('                    if (r && r.results && r.results.length > 0) { ');
    Add('                        var pin = new Microsoft.Maps.Pushpin(r.results[0].location); ');
    Add('                        map.entities.push(pin); ');
    Add('                        map.setView({ bounds: r.results[0].bestView }); ');
    Add('                    } ');
    Add('                }, ');
    Add('                errorCallback: function (e) { ');
    // If there is an error, alert the user about it.
    Add('                    alert("No results found."); ');
    Add('                } ');
    Add('            }; ');
    //Make the geocode request.
    Add('            searchManager.geocode(searchRequest); ');
    Add('        } ');
    Add('    } ');
    Add('    </script> ');
    Add('    <script type="text/javascript" src="/BingMapsCredentials.js"></script> ');
    Add('    <script type="text/javascript" src="https://www.bing.com/api/maps/mapcontrol?callback=GetMap" async defer></script> ');
    Add('</head> ');
    Add('<body style="width:100%;height:100%"> ');
    Add('    <div id="TitleDiv" style="padding-bottom: 15px; ');
    Add('                             padding-top: 5px; ');
    Add('                             font-family: Arial, Helvetica, sans-serif; ');
    Add('                             font-size:   14px; ');
    Add('                              font-weight: bold; ');
    Add('                             position:relative;">' + sCaption + '</div> ');
    Add('    <div id="myMap" style="width:100vw;height:95vh;position:relative;"></div> ');
    Add('</body> ');
    Add('</html> ');
    sTempFile := GetLocalAppDir + 'mymap.html';
    if fileExists(sTempFile) then DeleteFile(sTempFile);
    savetofile(sTempFile);
    mMap.free;
  end;
  ChromWebPage.DefaultUrl := sTempFile;
  ChromWebPage.Browser.MainFrame.LoadUrl(sTempFile);
  ChromWebPage.Browser.Reload;

1 个答案:

答案 0 :(得分:0)

升级到最新版本的DCEF3解决了这个问题。怀疑脚本与旧版DCEF3不兼容。