Google Map api V3, setPosition/setCenter is not working

ε祈祈猫儿з 提交于 2019-12-23 05:42:32

问题


There are two places where i need to show google maps. I'm using same code for both maps(I mean I'm reusing same code for both maps). Below are links where you can find screen shots of those two maps.

https://drive.google.com/file/d/0B2JnvvXsAALUdlVzemRVMzNMajF4OFB1V0JXc0RuN1p4eWFV/view - map1

https://drive.google.com/file/d/0B2JnvvXsAALUVGRhNm1HSldhQnpZT3k4S2I2R1YyQkp4OWZz/view - map2

I'm showing second map(map2) in bootstarp modal

first map(map1) is working fine. But in second map(map2), though I have used setCenter method, marker is not showing at center of the map(instead it is showing at top left corner). what should i do to place marker at center of the map(in second map)?

below is my code..

initialize: function(options){
  //initializing the geocoder
  this.geocoder = new google.maps.Geocoder();
  
  this.map;				
},

//on show of the view
//onShow is a marionette method, which will be triggered when view is shown
onShow: function(){
  var address = this.model.get("address");
  //render the map with dummy latLang
  this.renderMap();
  
  //render the Map with Proper address
  if(address!== ""){
    this.renderMapWithProperAddress(address);
  }
},
  
renderMap: function(){
  var mapCanvas = document.getElementById("mapCanvas"), self = this,
  
  mapOptions = {
    center: new google.maps.LatLng(64.855, -147.833),//dummy latLang
    zoom: 15,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    zoomControl: false,
    mapTypeControl: false,
    streetViewControl:false
  };
  
  //initializing google map
  self.map = new google.maps.Map(mapCanvas, mapOptions);
  
  $("#myModal").on("shown.bs.modal",function(){
    google.maps.event.trigger(self.map, "resize");
  });  
},
  
  
renderMapWithProperAddress: function(address){
  var self = this,marker;
  
  self.geocoder.geocode( { "address": address}, function(results, status) {
	if (status == google.maps.GeocoderStatus.OK) {
		
        self.map.setCenter(results[0].geometry.location);
        
        marker = new google.maps.Marker({
          map: self.map,
          position: results[0].geometry.location,
          title: address
        });
				      
    } else {
				        
      alert("Geocode was not successful for the following reason: " + status);
				      
    }
	
  });
}

回答1:


My guess, without seeing your code, is that you need to trigger a map resize event after the bootstrap modal has been shown.

Bootstrap modal

Maps events

$('#myModal').on('shown.bs.modal', function () {
    google.maps.event.trigger(map, 'resize');
});

Edit:

Here is a complete and working example. As per my comment, you should 1) Trigger a map resize and 2) set the map center to your marker coordinates.

var center = new google.maps.LatLng(59.76522, 18.35002);

function initialize() {

    var mapOptions = {
        zoom: 7,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        center: center
    };

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

    var marker = new google.maps.Marker({
        map: map,
        position: center
    });
}

$('.launch-map').on('click', function () {

    $('#modal').modal({
        backdrop: 'static',
        keyboard: false
    }).on('shown.bs.modal', function () {
        google.maps.event.trigger(map, 'resize');
        map.setCenter(center);
    });
});

initialize();

JSFiddle demo



来源:https://stackoverflow.com/questions/29875472/google-map-api-v3-setposition-setcenter-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!