不能红色财产推动'未定义的

时间:2017-10-16 08:01:15

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

我有一个简单的谷歌地图与自定义样式,绘制地图效果很好,但在我想绘制标记的点我得到这个错误:"不能红色属性'推送'未定义"。似乎marker未定义,但我不知道为什么。

var i = 0;
var map,
    markersArray,
    marker = [];
var geocoder;
var latitude = $(".js-coordinates").data("latitude");
var longitude = $(".js-coordinates").data("longitude");

function init() {
    var mapOptions = {
        center: new google.maps.LatLng(47.621792911667455, 12.208831455490099),
        zoom: 15,
        gestureHandling: 'auto',
        fullscreenControl: false,
        zoomControl: false,
        disableDoubleClickZoom: true,
        mapTypeControl: false,
        scaleControl: false,
        scrollwheel: true,
        streetViewControl: false,
        draggable: true,
        clickableIcons: false,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        styles: [{
        "featureType": "landscape",
        "elementType": "labels",
        "stylers": [{ "visibility": "off" }] }, { "featureType": "transit", "elementType": "labels", "stylers": [{ "visibility": "off" }] }, { "featureType": "poi", "elementType": "labels", "stylers": [{ "visibility": "off" }] }, { "featureType": "water", "elementType": "labels", "stylers": [{ "visibility": "off" }] }, { "featureType": "road", "elementType": "labels.icon", "stylers": [{ "visibility": "off" }] }, { "stylers": [{ "hue": "#00aaff" }, { "saturation": -100 }, { "gamma": 2.15 }, { "lightness": 12 }] }, { "featureType": "road", "elementType": "labels.text.fill", "stylers": [{ "visibility": "on" }, { "lightness": 24 }] }, { "featureType": "road", "elementType": "geometry", "stylers": [{ "lightness": 57 }] }]
};

var mapElement = document.getElementById('googleMap');

map = new google.maps.Map(mapElement, mapOptions);
geocoder = new google.maps.Geocoder();

var locations = [{ "title": "Freisinger Holzbau GmbH",
    "address": "",
    "desc": "",
    "tel": "",
    "int_tel": "",
    "email": "",
    "web": "",
    "web_formatted": "",
    "open": "",
    "time": "",
    "lat": latitude,
    "lng": longitude,
    "vicinity": "Wildbichler Straße 1a, Ebbs",
    "open_hours": "",
    "marker": {
        "fillColor": "#4CAF50",
        "fillOpacity": 1,
        "strokeWeight": 0,
        "scale": 1.5,
        "path": "M10.2,9.6C4.5,9.6-0.1,14.3-0.1,20c0,5.7,10.3,12.1,10.3,16.6c0-4.5,10.3-10.9,10.3-16.6S15.9,9.6,10.2,9.6z M10.2,24.3c-2.4,0-4.4-2-4.4-4.4s2-4.4,4.4-4.4s4.4,2,4.4,4.4S12.6,24.3,10.2,24.3z", "anchor": { "x": 10, "y": 30 }, "origin": { "x": 0, "y": 0 }, "style": 2 }, "iw": { "address": true, "desc": true, "email": true, "enable": true, "int_tel": true, "open": true, "open_hours": true, "photo": true, "tel": true, "title": true, "web": true } }];

for (i = 0; i < locations.length; i++) {

    marker = new google.maps.Marker({
        icon: '/assets/images/marker.png',
        position: new google.maps.LatLng(locations[i].lat, locations[i].lng),
        map: map,
        title: locations[i].title,
        address: locations[i].address,
        desc: locations[i].desc,
        tel: locations[i].tel,
        int_tel: locations[i].int_tel,
        vicinity: locations[i].vicinity,
        open: locations[i].open,
        open_hours: locations[i].open_hours,
        photo: locations[i].photo,
        time: locations[i].time,
        email: locations[i].email,
        web: locations[i].web,
        iw: locations[i].iw
    });

    markersArray.push(marker);
   }
}

2 个答案:

答案 0 :(得分:1)

您应该初始化 markersArray

var markersArray =[];

答案 1 :(得分:0)

您没有初始化mapmarkersArray

的值
var map,
    markersArray,
    marker = [];

markersArray未定义,因此您可以从此处获得错误

markersArray.push(marker);

只需初始化该值即可解决问题

var map,
    markersArray = [],
    marker = [];