最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - re initialize gmap3 map - Stack Overflow

programmeradmin1浏览0评论

Is it possible to re initialize a gmap3 map to its default settings (lat, long, zoom etc)?

I tried destroy on the map and then re run the initialise but it is not working.

See my jsfiddle here - /

Is it possible to re initialize a gmap3 map to its default settings (lat, long, zoom etc)?

I tried destroy on the map and then re run the initialise but it is not working.

See my jsfiddle here - http://jsfiddle/vlowe/z9dDE/1/

Share edited Sep 29, 2012 at 11:51 Eli 14.8k5 gold badges61 silver badges77 bronze badges asked Jul 8, 2012 at 15:18 Vince LoweVince Lowe 3,6307 gold badges39 silver badges63 bronze badges 2
  • Just remove the destroy code. it will work as expected. – Kundan Singh Chouhan Commented Jul 8, 2012 at 15:31
  • it tried that first, it does not – Vince Lowe Commented Jul 8, 2012 at 15:39
Add a ment  | 

2 Answers 2

Reset to default 4

Wrap the map element in a container div, and then when you want to reset the map, just remove the whole map div and recreate it. This should solve the problem.

According to the gmap3 documentation, you still need to call destroy on the map before removing the dom element.

HTML code

<div>
    <div id="map"></div>
</div>

<a href="#" onclick="restore();">restore</a>​

JavaScript

var dlat = 53.9783997; // default map latitude
var dlng = -1.5666347; // default map longitude
var dzoom = 6; // default map zoom
var dmaptype = "roadmap"; // default map type
// create gmap
$('#map').gmap3({
    action: 'init',
    options: {
        center: [dlat, dlng],
        zoom: dzoom,
        mapTypeId: dmaptype,
        mapTypeControl: true,
        mapTypeControlOptions: {
            style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
        },
        navigationControl: true,
        scrollwheel: true,
        streetViewControl: true
    }
});


function restore() {

    $('#map').gmap3({
        action: 'destroy'
    });

    var container = $('#map').parent();
    $('#map').remove();
    container.append('<div id="map"></div>');

    // create new gmap
    $('#map').gmap3({
        action: 'init',
        options: {
            center: [dlat, dlng],
            zoom: dzoom,
            mapTypeId: dmaptype,
            mapTypeControl: true,
            mapTypeControlOptions: {
                style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
            },
            navigationControl: true,
            scrollwheel: true,
            streetViewControl: true
        }
    });

}​

Here's an alternative method: rather than destroying the map and recreating it entirely, you could save the initial state and the restore it to that state like this: http://jsfiddle/UvAL3/1/

(these functions existed as part of the API in V2)

发布评论

评论列表(0)

  1. 暂无评论