-1

J'ai un div comme suit pour afficher une carte Google:cartes Google API méthode V3 fitBounds() en utilisant prototypejs

#map { 
    width: 300px; 
    height: 300px; 
    border: 1px solid #DDD; 
} 

<div id="map"></div> 

Je veux afficher la carte avec un niveau de zoom qui correspond aux limites de la fenêtre ci-dessus .

Quand je code comme suit cela fonctionne très bien:

var geocoder = new google.maps.Geocoder(); 
    var map = new google.maps.Map($('#map')[0], {zoom: 10}); 

    geocoder.geocode({ 'address': generatedAddress }, function (results, status) { 
     if (status == 'OK') { 
       map.setCenter(results[0].geometry.location); 
       var marker = new google.maps.Marker({ 
        map: map, 
        position: results[0].geometry.location 
       }); 
       if (results[0].geometry.viewport) 
        map.fitBounds(results[0].geometry.viewport); 

     } else { 
      alert('Geocode was not successful for the following reason: ' + status); 
     } 
    }); 

Lorsque j'utilise typeahead-addresspicker.js pour générer la carte, il effectue un zoom avant trop loin?

Je l'ai réduit au code suivant. Lorsque vous appelez la fonction AddressPicker.prototype.updateMap l'option boundsForLocation sur AddressPicker.prototype.initMap fonction doit retourner this.map.fitBounds(response.geometry.viewport); Quand je déboguer je vois qu'il frappe le code suivant dans la fonction AddressPicker.prototype.updateBoundsForPlace comme prévu:

if (response.geometry.viewport) { 
    console.log('test'); 
    return this.map.fitBounds(response.geometry.viewport); 
} 

Ce que je ne comprends pas comment il est renvoyé à google.maps.Map - je ne suis pas familier avec ptototypejs? Donc, en l'exécutant, nous initilisons la carte en appelant initMap, puis nous appelons la fonction updateMap. A l'intérieur fonction updateMap nous appelons l'extrait de code suivant:

if (_this.map) { 
      if ((_ref = _this.mapOptions) != null) { 
       _ref.boundsForLocation(response); 
      } 
    } 

qui est supposé définir les limites en appelant la updateBoundsForPlace mais le google maps options ne marche pas exposer une propriété appelée boundsForLocation?

AddressPicker.prototype.initMap = function() { 
    var markerOptions, _ref, _ref1; 
    if ((_ref = this.options) != null ? (_ref1 = _ref.map) != null ? _ref1.gmap : void 0 : void 0) { 
     this.map = this.options.map.gmap; 
    } else { 
     this.mapOptions = $.extend({ 
     zoom: 3, 
     center: new google.maps.LatLng(0, 0), 
     mapTypeId: google.maps.MapTypeId.ROADMAP, 
     boundsForLocation: this.updateBoundsForPlace 
     }, this.options.map); 
     this.map = new google.maps.Map($(this.mapOptions.id)[0], this.mapOptions); 
    } 
    this.lastResult = null; 
    markerOptions = $.extend({ 
     draggable: true, 
     visible: false, 
     position: this.map.getCenter(), 
     map: this.map 
    }, this.options.marker || {}); 
    this.marker = new google.maps.Marker(markerOptions); 
    if (markerOptions.draggable) { 
     return google.maps.event.addListener(this.marker, 'dragend', this.markerDragged); 
    } 
    }; 


    AddressPicker.prototype.updateMap = function(event, place) { 
    if (this.options.placeDetails) { 
     return this.placeService.getDetails(place, (function(_this) { 
     return function(response) { 
      var _ref; 
      _this.lastResult = new AddressPickerResult(response); 
      if (_this.marker) { 
      _this.marker.setPosition(response.geometry.location); 
      _this.marker.setVisible(true); 
      } 
      if (_this.map) { 
      if ((_ref = _this.mapOptions) != null) { 
       _ref.boundsForLocation(response); 
      } 
      } 
      return $(_this).trigger('addresspicker:selected', _this.lastResult); 
     }; 
     })(this)); 
    } else { 
     return $(this).trigger('addresspicker:selected', place); 
    } 
    }; 

    AddressPicker.prototype.updateBoundsForPlace = function(response) { 
    if (response.geometry.viewport) { 
     return this.map.fitBounds(response.geometry.viewport); 
    } else { 
     this.map.setCenter(response.geometry.location); 
     return this.map.setZoom(this.options.zoomForLocation); 
    } 
    }; 
+1

Est-il possible que vous pouvez faire un violon pour démontrer le problème? Je ne comprends pas vraiment ce que vous essayez de demander. –

+0

Dans initMap, this.map est en cours de définition sur google maps: this.map = this.options.map.gmap; ou this.map = nouveau google.maps.Map ($ (this.mapOptions.id) [0], this.mapOptions); –

+0

@Scott Je comprends cela mais quand vous appelez la fonction updateMap, elle doit définir boundsForLocation. Mais en regardant le https://developers.google.com/maps/documentation/javascript/reference#MapOptions, MapIptions n'a pas de propriété appelée boundsForLocation, alors comment est-il configuré? – adam78

Répondre

0

réussi à résoudre en commentant les lignes suivantes:

//if (response.geometry.viewport) { 
// return this.map.fitBounds(response.geometry.viewport); 
//} else { 
    this.map.setCenter(response.geometry.location); 
    return this.map.setZoom(this.options.zoomForLocation); 
//}