谷歌地图上的中心选项无效

时间:2014-09-08 19:23:56

标签: javascript google-maps google-maps-api-3

也许我忽略了什么?中心不工作。我知道有一种通往汽车中心的方式,但它将亚洲减少到两个。我希望美国在左边,而不是在中间,因为它显示了汽车中心。提前谢谢!

Demo Here

google.maps.event.addDomListener(window, 'load', initialize);
var infos = [];
var locations = [
  ['loan 1', 23.2646555,-106.4603982, 'address 1'],
  ['loan 1', 39.9388838,116.3974589, 'address 1'],
  ['loan 1', 38.8993487,-77.0145665, 'address 1']
];
function initialize() {
var myOptions = {
        zoom: 2,
  center: new google.maps.LatLng(39.9388838,116.3974589),
styles: [{"featureType":"landscape","stylers":[{"hue":"#FFAD00"},{"saturation":50.2},{"lightness":-34.8},{"gamma":1}]},{"featureType":"road.highway","stylers":[{"hue":"#FFAD00"},{"saturation":-19.8},{"lightness":-1.8},{"gamma":1}]},{"featureType":"road.arterial","stylers":[{"hue":"#FFAD00"},{"saturation":72.4},{"lightness":-32.6},{"gamma":1}]},{"featureType":"road.local","stylers":[{"hue":"#FFAD00"},{"saturation":74.4},{"lightness":-18},{"gamma":1}]},{"featureType":"water","stylers":[{"hue":"#00FFA6"},{"saturation":-63.2},{"lightness":38},{"gamma":1}]},{"featureType":"poi","stylers":[{"hue":"#FFC300"},{"saturation":54.2},{"lightness":-14.4},{"gamma":1}]}] 

};
var map = new google.maps.Map(document.getElementById("map"),
    myOptions);

setMarkers(map,locations)
 }
function setMarkers(map,locations){

  var marker, i
for (i = 0; i < locations.length; i++)
 {  
var loan = locations[i][0]
var lat = locations[i][1]
var long = locations[i][2]
var add =  locations[i][3]
latlngset = new google.maps.LatLng(lat, long);

var marker = new google.maps.Marker({  
      map: map, title: loan , position: latlngset  
    });
    map.setCenter(marker.getPosition())


    var content = "Loan Number: " + loan +  '</h3>' + "Address: " + add     
var infowindow = new google.maps.InfoWindow()

  google.maps.event.addListener(marker,'click', (function(marker,content,infowindow){ 
    return function() {

    /* close the previous info-window */
   closeInfos();

       infowindow.setContent(content);
       infowindow.open(map,marker);

    /* keep the handle, in order to close it on next click event */
infos[0]=infowindow;

    };
})(marker,content,infowindow)); }} 
function closeInfos(){
if(infos.length > 0){


  /* detach the info-window from the marker ... undocumented in the API docs */
  infos[0].set("marker", null);

  /* and close it */
  infos[0].close();

  /* blank the array */
  infos.length = 0; 
}}

2 个答案:

答案 0 :(得分:5)

如果在init函数末尾添加map.setCenter(new google.maps.LatLng(39.9388838,116.3974589)),它似乎可以正常工作

function initialize() {
  var myOptions = {
    zoom: 2,
    center: new google.maps.LatLng(39.9388838,116.3974589),
    styles: ...] 
  };
  var map = new google.maps.Map(document.getElementById("map"), myOptions);

  setMarkers(map,locations)
  map.setCenter(new google.maps.LatLng(39.9388838,116.3974589))
}

答案 1 :(得分:0)

您可以随时尝试设置自己的中心点。如果您希望以某种方式定位,您可以尝试找到所需的坐标,并将其用于您的中心。

center:new google.maps.LatLng(24.6251938,-27.7752902,15z)