地理位置无法正常工作

时间:2013-01-16 13:56:46

标签: javascript geolocation

以下代码在控制台中返回:

无法调用未定义的方法'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);
            });
    }

看起来似乎有一些错误的坐标

2 个答案:

答案 0 :(得分:1)

您的代码块不完整,所以我在这里猜测很多:您正在访问self.map.setCenter。错误消息显示“无法调用未定义的方法'setCenter',因此在定义self时,self.map不是。self。您的代码不包含self.mapself的定义,因此问题可能出在其他地方。

此外,您不应在代码中声明并使用self作为变量,因为window永远不会被定义(如果未在本地声明,则引用var that = this;)。请改用{{1}}。

答案 1 :(得分:0)

试试这样:

 map.setCenter(new GLatLng(latitude, -longitude), 13);
相关问题