2016-04-28 93 views
0

我使用Google地圖構建商店定位器。它適用於所有瀏覽器,但Safari瀏覽器,我不知道爲什麼。 Safari會拋出一個錯誤: SyntaxError:意外的標記'='。在參數聲明後需要''''或','。Safari JS語法錯誤:意外的標記'='

下面的代碼(我shortend了一點):

var marker = []; 
var LatLng; 
var infoWindow = new google.maps.InfoWindow(); 
var markerInfo = []; 
var bounds = new google.maps.LatLngBounds(); 
var queryLatlng = new google.maps.LatLng(50.9683679, 10.7635554); 
var zoom = 6; 
var markers = []; 
var mapOptions = { 
    center: queryLatlng, 
    zoom: zoom, 
    scroll: false, 
    scrollwheel: false, 
    streetViewControl: false, 
    mapTypeControl: true, 
    mapTypeControlOptions: { 
     position: google.maps.ControlPosition.TOP_LEFT 
    }, 
    zoomControl: true, 
    zoomControlOptions: { 
     position: google.maps.ControlPosition.TOP_LEFT 
    }, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
}; 
var map = new google.maps.Map(document.getElementById("map"),mapOptions); 
var styles = [{ 
    url: '../icon_marker_cluster30.png', 
    height: 30, 
    width: 30, 
    anchor: [5, 0], 
    textColor: '#ffffff', 
    textSize: 13 
    }, { 
    url: '../icon_marker_cluster40.png', 
    height: 40, 
    width: 40, 
    anchor: [9, 0], 
    textColor: '#ffffff', 
    textSize: 14 
    }, { 
    url: '../icon_marker_cluster50.png', 
    width: 50, 
    height: 50, 
    anchor: [13, 0], 
    textColor: '#ffffff', 
    textSize: 16 
    }]; 

function init(){    
    latlng = new google.maps.LatLng(50.343597, 19.007915); 
    marker['1'] = new google.maps.Marker({ 
      position: latlng, 
      map: map 
    }); 

    markers.push(marker['1']); 

    marker['1'].addListener('click', function() { 
     map.setZoom(15); 
     map.setCenter(marker['1'].getPosition()); 
    }); 

    bounds.extend(latlng); 

    markerInfo['1'] = createMarkerInfo("store", "address", "zipcode", "city"); 
    google.maps.event.addListener(marker['1'], 'click', function() { 
     infoWindow.setContent(markerInfo['1']); 
     infoWindow.open(map, marker['1']); 
    }); 


    var markerCluster = new MarkerClusterer(map, markers, {styles: styles}); 

    google.maps.event.addListener(map, 'idle', function() { 
     showVisibleMarkers(); 
    }); 

    map.fitBounds(bounds); 
} 

function createMarkerInfo(name, address, zip, city, phone = null, mail = null){ 
    var output = ''; 

    output += '<span class="label">' + name + '</span><br /><br />'; 
    output += address + '<br />'; 
    output += zip + ' ' + city + '<br />'; 

    if(phone) 
     output += '<br /><i class="fa fa-phone"></i> ' + phone; 
    if(mail) 
     output += '<br /><i class="fa fa-envelope"></i> <a href="mailto:' + mail + '">' + mail + '</a>'; 

    return output; 
} 

function showMarker(id){ 
    google.maps.event.trigger(marker[id], 'click'); 
    $('html, body').animate({ 
     scrollTop: $("#map").offset().top 
    }, 500); 
} 

function showVisibleMarkers() { 
    var bounds = map.getBounds(); 

    for (var i = 0; i < markers.length; i++) { 
     var marker = markers[i], 
      infoPanel = $('table tr[data-marker="'+i+'"]'); // array indexes start at zero, but not our class names :) 

     if(bounds.contains(marker.getPosition())===true) { 
      infoPanel.removeClass('hidden'); 
     } 
     else { 
      infoPanel.addClass('hidden'); 
     } 
    } 

    $("table tr").removeClass("odd"); 
    $("table tr:visible:odd").addClass("odd"); 
} 

google.maps.event.addDomListener(window, "load", init); 

我做了一些googleling,發現一些提示與「匿名函數」,但我不知道如何以及在何處的問題發生。

編輯: 我可以將問題追蹤到函數'createMarkerInfo'。但仍然沒有頭緒:(

+0

您發佈的代碼是正確的,這個錯誤可能發生在你沒有包括在你的問題中的部分,Safari會在控制檯給你一個體面的堆棧跟蹤,告訴你錯誤發生的確切的線路 – marvinhagemeister

+0

這就是問題Safari告訴我,錯誤發生在'init'函數後面的空行 – Patricks

+0

我用完整的js代碼更新了我的帖子 – Patricks

回答

1

我發現了錯誤:問題是createMarkerInfo函數的參數如果您刪除手機= NULL,郵件= NULL一切工作只是罰款