2016-11-02 13 views
-1

私はこの質問が以前に尋ねられていることを知っています。私が得た最も近いが、この1this.setValuesは関数ではありませんGoogleマップAPI 3

Map Markers Not Displaying (JavaScript/Google Maps API V3)

だったが、ここで私は彼がやろうとしていると、それにもかかわらず、私は私の場合は違うと思われるものを知らないし、それは私のために動作しませんでした。 だれでも勇気があれば教えてください。

function initialize() { 
 
      var mapOptions = { 
 
       zoom: 16, 
 
       center: new google.maps.LatLng(52.5498783, 13.425209099999961), 
 
       mapTypeId: google.maps.MapTypeId.SATELLITE 
 
      }; 
 

 
      map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); 
 
      
 
      overlay = new DraggableOverlay(map, 
 
              map.getCenter(), 
 
              '<div class="overlay">drag me!</div>' 
 
        ); 
 

 

 
     } 
 
     
 
     DraggableOverlay.prototype = new google.maps.OverlayView(); 
 

 
     DraggableOverlay.prototype.onAdd = function() { 
 
      var container = document.createElement('div'), 
 
       that = this; 
 

 
      if (typeof this.get('content').nodeName !== 'undefined') { 
 
       container.appendChild(this.get('content')); 
 
      } 
 
      else { 
 
       if (typeof this.get('content') === 'string') { 
 
        container.innerHTML = this.get('content'); 
 
       } 
 
       else { 
 
        return; 
 
       } 
 
      } 
 
      container.style.position = 'absolute'; 
 
      container.draggable = true; 
 
      google.maps.event.addDomListener(this.get('map').getDiv(), 
 
              'mouseleave', 
 
               function() { 
 
                google.maps.event.trigger(container, 'mouseup'); 
 
               } 
 
      ); 
 

 

 
      google.maps.event.addDomListener(container, 
 
              'mousedown', 
 
             function (e) { 
 
              this.style.cursor = 'move'; 
 
              that.map.set('draggable', false); 
 
              that.set('origin', e); 
 

 
              that.moveHandler = google.maps.event.addDomListener(that.get('map').getDiv(), 
 
                            'mousemove', 
 
                            function (e) { 
 
                             var origin = that.get('origin'), 
 
                              left = origin.clientX - e.clientX, 
 
                              top = origin.clientY - e.clientY, 
 
                              pos = that.getProjection() 
 
                                .fromLatLngToDivPixel(that.get('position')), 
 
                              latLng = that.getProjection() 
 
                                .fromDivPixelToLatLng(new google.maps.Point(pos.x - left, 
 
                                           pos.y - top)); 
 
                             that.set('origin', e); 
 
                             that.set('position', latLng); 
 
                             that.draw(); 
 
                            }); 
 

 

 
             } 
 
      ); 
 

 
      google.maps.event.addDomListener(container, 'mouseup', function() { 
 
       that.map.set('draggable', true); 
 
       this.style.cursor = 'default'; 
 
       google.maps.event.removeListener(that.moveHandler); 
 
      }); 
 

 

 
      this.set('container', container) 
 
      this.getPanes().floatPane.appendChild(container); 
 
     }; 
 

 
     function DraggableOverlay(map, position, content) { 
 
      if (typeof draw === 'function') { 
 
       this.draw = draw; 
 
      } 
 
      this.setValues({ 
 
       position: position, 
 
       container: null, 
 
       content: content, 
 
       map: map 
 
      }); 
 
     } 
 

 

 

 
     DraggableOverlay.prototype.draw = function() { 
 
      var pos = this.getProjection().fromLatLngToDivPixel(this.get('position')); 
 
      this.get('container').style.left = pos.x + 'px'; 
 
      this.get('container').style.top = pos.y + 'px'; 
 
     }; 
 

 
     DraggableOverlay.prototype.onRemove = function() { 
 
      this.get('container').parentNode.removeChild(this.get('container')); 
 
      this.set('container', null) 
 
     }; 
 

 

 
     google.maps.event.addDomListener(window, 'load', initialize);
html, body, #map-canvas { 
 
     height: 100%; 
 
     margin: 0px; 
 
     padding: 0px 
 
     } 
 
     .overlay{ 
 
     background:yellow; 
 
     padding:30px; 
 
     border:4px double black; 
 
     }
<div id="map-canvas"></div> 
 
    <script async defer 
 
    src="https://maps.googleapis.com/maps/api/js?key=AIzaSyD7FTNE22Wl6S6DTQF83sTZTqbFFPzEkmU&libraries=drawing,places,geometry&callback=initialize"> 
 
    
 
    </script>

だからここに私はグーグルが

  • this.setValuesが定義されていない二つの問題

    • を取得しています見ることができるよう

    関数ではありませんだから誰でもこの問題に直面したら私に知らせてください。

  • 答えて

    0

    google is not defined:あなたは非同期APIをロードしている場合は、コールバック関数(あなたのケースでinitialize)内部APIに依存すべてコードを配置する必要があります。 APIに依存するものは、APIがロードされるまで機能しません。 DraggableOverlayの定義をinitializeに移動するか、APIを同期して読み込む必要があります。

    APIの非同期ロードすることなく以下の実施例(等the example in the documentationDr.Molle's answer that it looks like the DraggableOverlay came from: Can we make custom overlays draggable on google maps V3

    function initialize() { 
     
        var mapOptions = { 
     
        zoom: 16, 
     
        center: new google.maps.LatLng(52.5498783, 13.425209099999961), 
     
        mapTypeId: google.maps.MapTypeId.SATELLITE 
     
        }; 
     
    
     
        map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); 
     
    
     
        overlay = new DraggableOverlay(map, 
     
        map.getCenter(), 
     
        '<div class="overlay">drag me!</div>' 
     
    ); 
     
    } 
     
    
     
    DraggableOverlay.prototype = new google.maps.OverlayView(); 
     
    DraggableOverlay.prototype.onAdd = function() { 
     
        var container = document.createElement('div'), 
     
        that = this; 
     
    
     
        if (typeof this.get('content').nodeName !== 'undefined') { 
     
        container.appendChild(this.get('content')); 
     
        } else { 
     
        if (typeof this.get('content') === 'string') { 
     
         container.innerHTML = this.get('content'); 
     
        } else { 
     
         return; 
     
        } 
     
        } 
     
        container.style.position = 'absolute'; 
     
        container.draggable = true; 
     
        google.maps.event.addDomListener(this.get('map').getDiv(), 
     
        'mouseleave', 
     
        function() { 
     
         google.maps.event.trigger(container, 'mouseup'); 
     
        } 
     
    ); 
     
    
     
        google.maps.event.addDomListener(container, 
     
        'mousedown', 
     
        function(e) { 
     
         this.style.cursor = 'move'; 
     
         that.map.set('draggable', false); 
     
         that.set('origin', e); 
     
    
     
         that.moveHandler = google.maps.event.addDomListener(that.get('map').getDiv(), 
     
         'mousemove', 
     
         function(e) { 
     
          var origin = that.get('origin'), 
     
          left = origin.clientX - e.clientX, 
     
          top = origin.clientY - e.clientY, 
     
          pos = that.getProjection() 
     
          .fromLatLngToDivPixel(that.get('position')), 
     
          latLng = that.getProjection() 
     
          .fromDivPixelToLatLng(new google.maps.Point(pos.x - left, 
     
           pos.y - top)); 
     
          that.set('origin', e); 
     
          that.set('position', latLng); 
     
          that.draw(); 
     
         }); 
     
        } 
     
    ); 
     
        google.maps.event.addDomListener(container, 'mouseup', function() { 
     
        that.map.set('draggable', true); 
     
        this.style.cursor = 'default'; 
     
        google.maps.event.removeListener(that.moveHandler); 
     
        }); 
     
        this.set('container', container) 
     
        this.getPanes().floatPane.appendChild(container); 
     
    }; 
     
    
     
    function DraggableOverlay(map, position, content) { 
     
        if (typeof draw === 'function') { 
     
        this.draw = draw; 
     
        } 
     
        this.setValues({ 
     
        position: position, 
     
        container: null, 
     
        content: content, 
     
        map: map 
     
        }); 
     
    }; 
     
    
     
    
     
    DraggableOverlay.prototype.draw = function() { 
     
        var pos = this.getProjection().fromLatLngToDivPixel(this.get('position')); 
     
        this.get('container').style.left = pos.x + 'px'; 
     
        this.get('container').style.top = pos.y + 'px'; 
     
    }; 
     
    
     
    DraggableOverlay.prototype.onRemove = function() { 
     
        this.get('container').parentNode.removeChild(this.get('container')); 
     
        this.set('container', null) 
     
    }; 
     
    
     
    google.maps.event.addDomListener(window, 'load', initialize);
    html, 
     
    body, 
     
    #map-canvas { 
     
        height: 100%; 
     
        margin: 0px; 
     
        padding: 0px 
     
    } 
     
    .overlay { 
     
        background: yellow; 
     
        padding: 30px; 
     
        border: 4px double black; 
     
    }
    <div id="map-canvas"></div> 
     
    <script src="https://maps.googleapis.com/maps/api/js?libraries=drawing,places,geometry"> 
     
    </script>

    関連する問題