【问题标题】:Possible to add ground overlay to google map with stored image?可以使用存储的图像将地面叠加层添加到谷歌地图吗?
【发布时间】:2015-06-25 20:43:15
【问题描述】:

我在尝试将图像作为地面叠加层添加到我正在创建的谷歌地图时遇到了困难。在我看到的所有帮助文件中,图像已作为 url 传递给地面覆盖函数。是否可以传入存储在我机器上的图像,而不是在线图像?

这是我用于地图和叠加层的代码:

    function initialize() {
        var mapOptions = {
            center: {lat: 45.3469, lng: -75.7594},
            zoom: 10,
            disableDefaultUI: true,
            mapTypeId: google.maps.MapTypeId.ROADMAP
            };
        var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

        var imageBounds = new google.maps.LatLngBounds(
        new google.maps.LatLng(45.2118075, -75.4455767),
        new google.maps.LatLng(45.2043559, -75.4545309));

        var campusOverlay = new google.maps.GroundOverlay('Campus.JPG', imageBounds);
            campusOverlay.setMap(map);
    }

我希望将我创建的“Campus.jpg”图像添加到地图中。我也尝试过传递整个路径及其所有目录,但地图上仍然没有出现任何内容。如果没有办法像这样传递图像,有没有办法将图像放到网上并以这种方式使用它?

谢谢

【问题讨论】:

  • 我认为您的问题是由于图像必须由有效的 url 识别,而不是来自对文件的本地引用
  • 我同意这可能是问题所在,关于如何将本地引用转换为 url 的任何建议?
  • 最简单的方法就是将图片保存在互联网服务器上。今天有许多免费的托管服务,您可以在其中使用 FTP 放置您需要的文件

标签: google-maps jpeg offline gmsgroundoverlay


【解决方案1】:

将完整的 url 作为GroundOverlay 的第一个参数传递给图像,并确保您的边界是正确的。这可能有点棘手。您的叠加层应覆盖北纬和南纬/东和 西经,所以你基本上有某种矩形区域。 这是一个简单的例子。相信你会明白的。

var overlay;

function initialize() {

  var center = new google.maps.LatLng(45.3469, -75.7594);
  var imageBounds = new google.maps.LatLngBounds(
    new google.maps.LatLng(45.2, -76.1),
    new google.maps.LatLng(45.5, -75.38)
  );

  var mapOptions = {
    zoom: 10,
    center: center,
    disableDefaultUI: true
  };

  var map = new google.maps.Map(document.getElementById('map-canvas'),
                                mapOptions);

  overlay = new google.maps.GroundOverlay(
    'https://upload.wikimedia.org/wikipedia/commons/e/ee/Dowarian%2C_Neelam_Valley%2C_AJK_Pakistan.JPG',
    imageBounds);
  overlay.setMap(map);
}

google.maps.event.addDomListener(window, 'load', initialize);
html, body, #map-canvas {
  height: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true"></script>
<div id="map-canvas"></div>

希望对您有所帮助。

【讨论】:

  • 感谢您的帮助,原来我的坐标比我想象的更远,但现在效果很好。
  • 这根本不能回答问题
猜你喜欢
  • 2022-10-13
  • 2018-12-08
  • 2010-09-15
  • 2012-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多