如何集中我目前的位置?(谷歌地图)

Jac*_*ack 3 javascript jquery google-maps-api-3

我正试图将我当前的位置放在谷歌地图中心.这将使具有特定纬度和经度变量的位置居中.

var coords = new google.maps.LatLng(62.39081100, 17.30692700);
Run Code Online (Sandbox Code Playgroud)

但我尝试使用此功能获取用户位置:

function grabMyPosition() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(centerMe);
    } else {
        alert("You don't support this");
    }
}
function centerMe(center) {
    var me_location = {
        lat: position.coords.latitude,
        lon: position.coords.longitude
    };
}
Run Code Online (Sandbox Code Playgroud)

然后这样做:

var coords = new google.maps.LatLng(me_location.lat, me_location.lon);
Run Code Online (Sandbox Code Playgroud)

但后来我明白了:

Uncaught TypeError: Cannot read property 'lat' of undefined 
Run Code Online (Sandbox Code Playgroud)

那是因为我在函数中存储并填充了这个"me_location"变量.我怎样才能使它"全局",以便它可以在我的功能之外使用?

fer*_*vio 8

function grabMyPosition() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(centerMe);
    } else {
        alert("You don't support this");
    }
}
function centerMe(position) {
    var coords = new google.maps.LatLng(
        position.coords.latitude,
        position.coords.longitude
    );

    map.setCenter(coords);
    // or
    map.panTo(coords);
}
Run Code Online (Sandbox Code Playgroud)

假设你的map变量是全局的..