仅移动标记而不是整个地图

时间:2014-07-08 09:10:30

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

我正在尝试创建一个仅在地图上移动标记的函数,但是在给出新位置时会加载整个地图。我正在使用Geolocator获取当前位置并观看它(我已将getCurrentPosition()中的geolocator.min.js更改为watchPosition()。)

var map;
var map_coordinates = new google.maps.LatLng(60.327821, 13.603855);
var map_options;
var marker;
var place;
var settings_timeout = 10000;

var geolocator_options = { enableHighAccuracy: false, timeout: settings_timeout, maximumAge: 100 };



$(document).ready(function() {

    geolocator.locate(geolocator_success, geolocator_error, null, geolocator_options, 'gm');



    function geolocator_success(location) {

        place = { 'latitude': location.coords.latitude.toFixed(6), 'longitude': location.coords.longitude.toFixed(6) };
        maps_coordinates = new google.maps.LatLng(place['latitude'], place['longitude']);

        marker_solid(map_coordinates);
        marker_move(map_coordinates);

    }


    function geolocator_error(message) {
        alert(message);
    }


});



function marker_solid(coordinates) {
    marker = new google.maps.Marker({
        position: coordinates,
        map: map,
        draggable: false
    });

    map.panTo(coordinates);
}

function marker_move(coordinates) {
    marker.setPosition(coordinates);
    map.panTo(coordinates);
}


function google_maps() {
    map_options = {
        center: map_coordinates,
        zoom: 14,
        minZoom: 2,
        maxZoom: 19,
        streetViewControl: false
    };

    map = new google.maps.Map(document.getElementById('gm'), map_options);
}

Demo at jsFiddle

如何在地图上移动标记,而不是在每个新位置后加载整个地图?

0 个答案:

没有答案