以下代码在控制台中返回:
无法调用未定义的方法'setCenter'
this.locateUser = function() {
if(navigator && navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var latitude = position.coords.latitude,
longitude = position.coords.longitude;
var coords = google.maps.LatLng(latitude, longitude);
self.map.setCenter(coords);
});
}
看起来似乎有一些错误的坐标
答案 0 :(得分:1)
您的代码块不完整,所以我在这里猜测很多:您正在访问self.map.setCenter
。错误消息显示“无法调用未定义的方法'setCenter',因此在定义self
时,self.map
不是。self
。您的代码不包含self.map
或self
的定义,因此问题可能出在其他地方。
此外,您不应在代码中声明并使用self
作为变量,因为window
永远不会被定义(如果未在本地声明,则引用var that = this;
)。请改用{{1}}。
答案 1 :(得分:0)
试试这样:
map.setCenter(new GLatLng(latitude, -longitude), 13);