将Google地图默认为卫星视图?

时间:2015-10-02 11:39:34

标签: jquery google-maps

我希望谷歌地图默认为卫星视图,我迷路了。这来自模板怪物?我到处搜索,无法找到任何可以实现的东西。请帮忙。

(function ($) {
    'use strict'

var def_settings = {
        cntClass: 'map',
        mapClass: 'map_model',
        locationsClass: 'map_locations',
        marker: {
            basic: 'images/gmap_marker.png',
            active: 'images/gmap_marker_active.png'
        },
        styles: []
    },

    defaults = {
        map: {
            x: -90.044545,
            y: 37.967563,
            zoom: 16
        },
        locations: []
    };


var getLocations = function ($map, settings) {
    var $locations = $map.parent().find('.' + settings.locationsClass).find('li');

    var locations = [];


    if ($locations.length > 0) {
        $locations.each(function (i) {
            var $loc = $(this);

            if ($loc.data('x') && $loc.data('y')) {
                locations[i] = {
                    x: $loc.data('x'),
                    y: $loc.data('y'),
                    basic: $loc.data('basic') ? $loc.data('basic') : settings.marker.basic,
                    active: $loc.data('active') ? $loc.data('active') : settings.marker.active
                }

                if (!$.trim($loc.html())) {
                    locations[i].content = false;
                } else {
                    locations[i].content = '<div class="iw-content">' + $loc.html() + '</div>';
                }
            }
        });
    }
    return locations;
}

$.fn.googleMap = function (settings) {

    settings = $.extend(true, {}, def_settings, settings);

    $(this).each(function () {
        var $this = $(this);

        var options = $.extend(
            true, {}, defaults,
            {
                map: {
                    x: $this.data('x'),
                    y: $this.data('y'),
                    zoom: $this.data('zoom')
                },
                locations: getLocations($this, settings)
            }
        );

        var map = new google.maps.Map(this, {
                center: new google.maps.LatLng(
                    parseFloat(options.map.y),
                    parseFloat(options.map.x)
                ),
                scrollwheel: false,
                styles: settings.styles,
                zoom: options.map.zoom
            }),
            infowindow = new google.maps.InfoWindow(),
            markers = [];

        for (var i in options.locations) {
            markers[i] = new google.maps.Marker(
                {
                    position: new google.maps.LatLng(
                        parseFloat(options.locations[i].y),
                        parseFloat(options.locations[i].x)),
                    map: map,
                    icon: options.locations[i].basic,
                    index: i
                }
            );


            if (options.locations[i].content) {
                google.maps.event.addListener(markers[i], 'click', function () {
                    for (var j in markers) {
                        markers[j].setIcon(options.locations[j].basic);
                    }

                    infowindow.setContent(options.locations[this.index].content);
                    infowindow.open(map, this);
                    $('.gm-style-iw').parent().parent().addClass("gm-wrapper");
                    this.setIcon(options.locations[this.index].active);
                });
                google.maps.event.addListener(infowindow, 'closeclick', function () {
                    for (var j in markers) {
                        markers[j].setIcon(options.locations[j].basic);
                    }
                });
            }
        }

        google.maps.event.addDomListener(window, 'resize', function() {
            map.setCenter(new google.maps.LatLng(
                parseFloat(options.map.y),
                parseFloat(options.map.x)
            ));
        });
    });
};


})(jQuery);

1 个答案:

答案 0 :(得分:0)

您可以在创建地图时提供mapTypeId:google.maps.MapTypeId.SATELLITE。在默认设置中,将mapTypeId默认添加到卫星视图。

var def_settings = {
        cntClass: 'map',
        mapClass: 'map_model',
        locationsClass: 'map_locations',
        marker: {
            basic: 'images/gmap_marker.png',
            active: 'images/gmap_marker_active.png'
        },
        styles: [],
        mapTypeId: google.maps.MapTypeId.SATELLITE

    },

//和init。图

 var map = new google.maps.Map(this, {
                    center: new google.maps.LatLng(
                        parseFloat(options.map.y),
                        parseFloat(options.map.x)
                    ),
                    scrollwheel: false,
                    styles: settings.styles,
                    zoom: options.map.zoom,
                    mapTypeId: settings.mapTypeId
                })

或者您可以执行此操作(对于未在def_settings中添加mapTypeId时定义的google)(从def_settings对象中删除mapTypeId):

 var map = new google.maps.Map(this, {
                        center: new google.maps.LatLng(
                            parseFloat(options.map.y),
                            parseFloat(options.map.x)
                        ),
                        scrollwheel: false,
                        styles: settings.styles,
                        zoom: options.map.zoom,
                        mapTypeId: settings.mapTypeId||google.maps.MapTypeId.SATELLITE
                    })