将自定义标记添加到Google街景

时间:2015-06-06 15:23:43

标签: javascript google-maps google-street-view

请帮我添加一个标记到谷歌街景,但当我把它放在任何无效的工作,我得到一个 未捕获的ReferenceError:未定义map

继承标记变量

var cafe = new google.maps.LatLng(34.935196, -107.539546);

下面是设置标记的代码

var cafeMarker = new google.maps.Marker({
  position: cafe,
  map: map,
  icon: 'http://chart.apis.google.com/chart?chst=d_map_pin_icon&chld=cafe|FFFF00',
  title: 'Cafe'
});

继承自定义谷歌街景

<script src="https://maps.googleapis.com/maps/api/js? v=3.exp&signed_in=true"></script>
<script>


function initialize() {
   var fenway = new google.maps.LatLng(34.938447, -107.535998);

   var panoOptions = {
       position: fenway,
       addressControlOptions: {
         position: google.maps.ControlPosition.BOTTOM_CENTER
       },
       linksControl: false,
       panControl: false,
       zoomControlOptions: {
         style: google.maps.ZoomControlStyle.SMALL
       },
       enableCloseButton: false
   };

  var panorama = new google.maps.StreetViewPanorama(
  document.getElementById('map-canvas'), panoOptions);
}
google.maps.event.addDomListener(window, 'load', initialize);

</script>

1 个答案:

答案 0 :(得分:4)

  1. 要在StreetView上查看标记,您必须将其添加到全景图中。 Uncaught ReferenceError: map is not defined告诉您代码中没有定义map变量,需要panorama

    var cafeMarker = new google.maps.Marker({
      position: cafe,
      map: panorama, // your code doesn't have a 'map' variable
      icon: 'http://chart.apis.google.com/chart?chst=d_map_pin_icon&chld=cafe|FFFF00',
      title: 'Cafe'
    });
    
  2. 一旦我这样做,你的标记太遥远了。如果我将全景位置移近(并将其指向标记处),我会看到它。

    function initialize() {
        var fenway = new google.maps.LatLng(34.9355,-107.539254);
    
        var panoOptions = {
            position: fenway,
            addressControlOptions: {
                position: google.maps.ControlPosition.BOTTOM_CENTER
            },
            linksControl: false,
            panControl: false,
            zoomControlOptions: {
            style: google.maps.ZoomControlStyle.SMALL
            },
            enableCloseButton: false
        };
    
        var panorama = new google.maps.StreetViewPanorama(
        document.getElementById('map-canvas'), panoOptions);
    
        var cafe = new google.maps.LatLng(34.935196, -107.539546);
    
        var cafeMarker = new google.maps.Marker({
            position: cafe,
            map: panorama,
            icon: 'http://chart.apis.google.com/chart?chst=d_map_pin_icon&chld=cafe|FFFF00',
            title: 'Cafe'
        });
           var heading = google.maps.geometry.spherical.computeHeading(panorama.getPosition(),cafe);
    
        panorama.setPov({
          heading: heading,
          pitch: 0,
          zoom: 1
        });
    
    }
    
  3. proof of concept fiddle

    代码段

    function initialize() {
      var fenway = new google.maps.LatLng(34.9355, -107.539254);
      // var fenway = new google.maps.LatLng(34.938447, -107.535998);
    
      var panoOptions = {
        position: fenway,
        addressControlOptions: {
          position: google.maps.ControlPosition.BOTTOM_CENTER
        },
        linksControl: false,
        panControl: false,
        zoomControlOptions: {
          style: google.maps.ZoomControlStyle.SMALL
        },
        enableCloseButton: false
      };
    
      var panorama = new google.maps.StreetViewPanorama(
        document.getElementById('map-canvas'), panoOptions);
    
      var cafe = new google.maps.LatLng(34.935196, -107.539546);
    
      var cafeMarker = new google.maps.Marker({
        position: cafe,
        map: panorama,
        icon: 'http://chart.apis.google.com/chart?chst=d_map_pin_icon&chld=cafe|FFFF00',
        title: 'Cafe'
      });
      var heading = google.maps.geometry.spherical.computeHeading(panorama.getPosition(), cafe);
    
      panorama.setPov({
        heading: heading,
        pitch: 0,
        zoom: 1
      });
    
      // Set up the map
      var myOptions = {
        zoom: 15,
        center: cafe
      };
      var map = new google.maps.Map(document.getElementById('map_canvas'),
        myOptions);
      var cafeMarkerMap = new google.maps.Marker({
        position: cafe,
        map: map,
        icon: 'http://chart.apis.google.com/chart?chst=d_map_pin_icon&chld=cafe|FFFF00',
        title: 'Cafe'
      });
      var panoMarker = new google.maps.Marker({
        position: panorama.getPosition(),
        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: 'Pano'
      });
      document.getElementById('info').innerHTML = google.maps.geometry.spherical.computeDistanceBetween(panorama.getPosition(), cafe).toFixed(2) + " meters";
    
    }
    google.maps.event.addDomListener(window, 'load', initialize);
    html,
    body,
    #map-canvas,
    #map_canvas {
      height: 500px;
      width: 500px;
      margin: 0px;
      padding: 0px
    }
    <script src="https://maps.googleapis.com/maps/api/js?libraries=geometry"></script>
    <div id="map-canvas" style="border: 2px solid #3872ac;"></div>
    <div id="map_canvas" style="border: 2px solid #3872ac;"></div>
    <div id="info"></div>