Javascript 谷歌地图API设置中心方法不支持';行不通

Javascript 谷歌地图API设置中心方法不支持';行不通,javascript,api,google-maps,geolocation,Javascript,Api,Google Maps,Geolocation,因此,我必须在我的页面中添加一个“中心”按钮,当单击该按钮时,地图将使用setCenter方法在pin上居中。然而,当我点击我的按钮后,地图变成空白而不是显示中心 这是我的密码 如何解决这个问题? 提前谢谢你 <script src="http://maps.google.com/maps/api/js?sensor=false"></script> <script> function init() { var addButton = docume

因此,我必须在我的页面中添加一个“中心”按钮,当单击该按钮时,地图将使用setCenter方法在pin上居中。然而,当我点击我的按钮后,地图变成空白而不是显示中心

这是我的密码

如何解决这个问题? 提前谢谢你

<script src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script>
  function init() {
    var addButton = document.getElementById("setcenter");
    addButton.onclick = handleSetCenterButtonClicked;
    getMyLocation();
  }


  window.onload = init;

  function getMyLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(displayLocation);
    } else {
        alert("Oops, no geolocation support");
    }
  }

  function displayLocation(position) {
    showMap(position.coords);
    var latitude = position.coords.latitude;
    var longitude = position.coords.longitude;
    var div = document.getElementById("location");
    div.innerHTML = "You are at Latitude: " + latitude + ", Longitude: " + longitude;
  }

  var map;
  function showMap(coords) {
    var googleLatAndLong = new google.maps.LatLng(coords.latitude, coords.longitude);
    var mapOptions = {
        zoom : 18,
        center : googleLatAndLong,
        mapTypeId : google.maps.MapTypeId.SATELLITE
    };
    var mapDiv = document.getElementById("map");
    map = new google.maps.Map(mapDiv, mapOptions);

    addMarker(googleLatAndLong);
  }

  var marker;
  var markerArray = new Array();

  function addMarker(latLong) {
    var markerOptions = {
        position : latLong,
        map : map
    };
    marker = new google.maps.Marker(markerOptions);

    markerArray.push(marker);
  }

  // this is my setCenter method function
  function handleSetCenterButtonClicked(coords) {

    var latLng = new google.maps.LatLng(coords.latitude, coords.lotitude);
    map.setCenter(latLng);
  }

  // this is my setCenter method function
</script>

函数init(){
var addButton=document.getElementById(“setcenter”);
addButton.onclick=HandleSetCenterButtonClick;
getMyLocation();
}
window.onload=init;
函数getMyLocation(){
if(导航器.地理位置){
navigator.geolocation.getCurrentPosition(displayLocation);
}否则{
警报(“Oops,无地理位置支持”);
}
}
功能显示位置(位置){
showMap(position.coords);
变量纬度=位置坐标纬度;
var经度=position.coords.longitude;
var div=document.getElementById(“位置”);
div.innerHTML=“你在纬度:“+纬度+”,经度:“+经度;
}
var映射;
功能显示图(coords){
var googleLatAndLong=新的google.maps.LatLng(坐标系纬度,坐标系经度);
变量映射选项={
缩放:18,
中心:谷歌拉坦朗,
mapTypeId:google.maps.mapTypeId.SATELLITE
};
var mapDiv=document.getElementById(“map”);
map=新的google.maps.map(mapDiv,mapOptions);
addMarker(googleLatAndLong);
}
var标记;
var markerArray=新数组();
函数addMarker(latLong){
变量标记选项={
职位:拉特朗,
地图:地图
};
marker=新的google.maps.marker(markerOptions);
markerArray.push(标记器);
}
//这是我的setCenter方法函数
功能手柄SetCenterButtonClicked(坐标){
var latLng=新的google.maps.latLng(coords.latitude,coords.lotitude);
地图设置中心(latLng);
}
//这是我的setCenter方法函数

我认为代码是错误的:

var latLng = new google.maps.LatLng(coords.latitude, coords.lotitude);
应该是:

var latLng = new google.maps.LatLng(coords.latitude, coords.longitude);

在函数handleSetCenterButtonClicked(coords)中,是“coords.longitude”而不是“coords.lotitude”

函数中存在问题(除了输入错误):

点击
coords
类型为
MouseEvent
,没有任何关于lat/lng的信息。它与谷歌api中的
MouseEvent
不同。不清楚要将中心设置为哪个值。打开您的页面的用户的位置?还是其他价值

如果要在用户拖动后将中心定位到用户位置,可以添加全局变量:

var yourPos;
在函数
showMap()
中将其设置为已知值:

yourPos = googleLatAndLong;
并在单击处理程序中使用它:

  function handleSetCenterButtonClicked(coords) {

    //var latLng = new google.maps.LatLng(coords.lat(), coords.lng());
    //map.setCenter(latLng);

    map.setCenter(yourPos);
  }

除此之外,如果用户不想分享他的立场,则不会处理该案例。

非常感谢,这很有意义。
  function handleSetCenterButtonClicked(coords) {

    //var latLng = new google.maps.LatLng(coords.lat(), coords.lng());
    //map.setCenter(latLng);

    map.setCenter(yourPos);
  }