Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
201 views
in Technique[技术] by (71.8m points)

Google Maps GroundOverlay

I am attempting to get a Google maps GroundOverlay working without luck. Searched everywhere, read the Google reference documentation, still no lucK.

What I have attempted can be seen at:

http://www.ryecemetery.com.au/locate.html

Very basic code:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Test</title>
    <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB_SJakjgkzjvWoMu7T-DqsUDWUEbfUtTA"></script>
</head>
<body>
    <div style="width: 1200px; height: 800px" id="map-canvas"></div>
    <script>
    var historicalOverlay;
    var myLatlng = new google.maps.LatLng(-38.374058,144.822763);
    var map = new google.maps.Map(document.getElementById('map-canvas'), {
        zoom: 18,
        center: myLatlng
    });

    var imageBounds = new google.maps.LatLngBounds(
        new google.maps.LatLng(-38.374615, 144.823766),
        new google.maps.LatLng(-38.373628, 144.821631)
    );

    historicalOverlay = new google.maps.GroundOverlay(
        'http://www.ryecemetery.com.au/images/layout-06a.jpg',
        imageBounds);
    historicalOverlay.setMap(map);
  </script>
</body>
</html>

You can see that some sort of overlay is placed on the map. I believe I have the sw, ne latlng correct. If I check on Google maps by right clicking and choosing "whats here" those are the latlng that is given.

I have the overlay in png, gif and jpeg. With the png and gif I get nothing to appear.

Where have I gone wrong?

thanks

Ken

question from:https://stackoverflow.com/questions/65915770/google-map-groundoverlay-image-not-show

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your bounds is wrong for your groundOverlay.

according to the documentation for the constructor for a google.maps.LatLngBounds object:

Constructor
LatLngBounds(sw?:LatLng, ne?:LatLng)    Constructs a rectangle from the points at its south-west and north-east corners.

The arguments should be SW, NE. Your arguments are SE, NW. Your code:

var imageBounds = new google.maps.LatLngBounds(
    new google.maps.LatLng(-38.374615, 144.823766), //SE
    new google.maps.LatLng(-38.373628, 144.821631)  //NW
);

If I swap the longitudes between the two points it works:

var imageBounds = new google.maps.LatLngBounds(
    new google.maps.LatLng(-38.374615, 144.821631), //SW
    new google.maps.LatLng(-38.373628, 144.823766)  //NE
);

proof of concept fiddle

code snippet:

var historicalOverlay;
var myLatlng = new google.maps.LatLng(-38.374058, 144.822763);
var map = new google.maps.Map(document.getElementById('map-canvas'), {
  zoom: 18,
  center: myLatlng
});

var markerNW = new google.maps.Marker({
  position: new google.maps.LatLng(-38.374615, 144.823766),
  map: map,
  icon: {
    url: "https://maps.gstatic.com/intl/en_us/mapfiles/markers2/measle.png",
    size: new google.maps.Size(7, 7),
    anchor: new google.maps.Point(3.5, 3.5)
  },
  title: "SW"
});
var markerSE = new google.maps.Marker({
  position: new google.maps.LatLng(-38.373628, 144.821631),
  map: map,
  icon: {
    url: "https://maps.gstatic.com/intl/en_us/mapfiles/markers2/measle_blue.png",
    size: new google.maps.Size(7, 7),
    anchor: new google.maps.Point(3.5, 3.5)
  },
  title: "NE"
});

var imageBounds = new google.maps.LatLngBounds(
  new google.maps.LatLng(-38.374615, 144.821631),
  new google.maps.LatLng(-38.373628, 144.823766));

historicalOverlay = new google.maps.GroundOverlay(
  'http://www.ryecemetery.com.au/images/layout-06a.jpg',
  imageBounds);
historicalOverlay.setMap(map);
html,
body,
#map-canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map-canvas"></div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...