【问题标题】:javascript - API google markerjavascript - API 谷歌标记
【发布时间】:2014-01-22 11:45:51
【问题描述】:

我有一个非常大的问题,我试图在我的网站上包含这个 javascript...但是...它不起作用...而且...我不知道为什么,有人可以帮帮我吗??

var map;
var places = [
    ['ferrara1', 44.831283, 11.617459, 'Ha funzionato ?'],
    ['ferrara2', 44.833283, 11.617459, 'se vedo tutto siiii !'],
    ['ferrara3', 44.886283, 11.617459, 'uahahahah'],
    ['ferrara4', 44.832283, 11.617459, 'è vivo'],
];

function initialize() {
    var mapOptions = {
    zoom: 15
    };
    map = new google.maps.Map(document.getElementById('maps'),mapOptions);                
    if(navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) {
            var pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
            var infowindow = new google.maps.InfoWindow({
                map: map,
                position: pos,
                content: 'Tu sei Qui.'
            });
            map.setCenter(pos);
    }, function() {
            handleNoGeolocation(true);
        });
    } else {
        handleNoGeolocation(false);
    }
    point(map,places);
};

function handleNoGeolocation(errorFlag) {
    if (errorFlag) {
        var content = 'Errore: Geolocalizzazione Fallita.';
    } else {
        var content = 'Errore: Questo browser non supporta la Geolocalizzazione.';
    }
    var options = {
        map: map,
        position: new google.maps.LatLng(44.831283, 11.617459),
        content: content
    };
    var infowindow = new google.maps.InfoWindow(options);
    map.setCenter(options.position);
};

function point(map, places){
    for (var i = 0; i < places.length; i++ ) {
        var azienda = places[i]
        var markerLatlng[i] = new google.maps.LatLng(azienda[1], azienda[2]);
        var infowindow = new google.maps.InfoWindow({
            content: azienda[3]
        });
        var marker[i] = new google.maps.Marker({
            position: markerLatlng[i],
            map: map,
            title: azienda[0]
        });
        google.maps.event.addListener(marker[i], 'click', function () {
            infowindow.open(map,marker[i])
        });
    }
};
google.maps.event.addDomListener(window, 'load', initialize);

编辑: 我尝试在我网站的一个 html 页面上包含这个 javascript,以显示带有一些标记的谷歌地图(我已经注释掉了一个),但它似乎不起作用,我不明白为什么。

问题似乎出在我的函数point 上,因为如果我删除它,其余代码将正常工作,并且谷歌地图加载正常(但我没有得到我想要的标记)。

我不确定如何使用浏览器继续调试 javascript 中的代码。

【问题讨论】:

  • 我建议将其格式化并放入小提琴中?
  • 这不起作用怎么办? SO 对于含糊不清的问题表现不佳。例如,您是否有控制台错误?
  • 代码看起来很好,你有什么错误吗?我假设您的页面上有一个名为“地图”的 html 中的 div?。
  • 您已经包含了指向 google maps API 的链接?
  • 看起来你已经错误地声明了数组。 var markerLatlng[i] = new google.maps.LatLng(azienda[1], azienda[2]);从外观上看,您可能甚至不需要数组。 IE。 var markerLatlng = new google.maps.LatLng(azienda[1], azienda[2]);

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


【解决方案1】:

您有两个未正确声明的数组实例。 1. var markerLatlng[i] = ... 2. 变量标记[i] = ...

function point(map, places){
    for (var i = 0; i < places.length; i++ ) {
        var azienda = places[i];
        var markerLatlng[i] = new google.maps.LatLng(azienda[1], azienda[2]);
        var infowindow = new google.maps.InfoWindow({
            content: azienda[3]
        });
        var marker[i] = new google.maps.Marker({
            position: markerLatlng[i],
            map: map,
            title: azienda[0]
        });
        google.maps.event.addListener(marker[i], 'click', function () {
            infowindow.open(map,marker[i]);
        });
    }
};

要么在赋值之前声明这些数组,要么在这种情况下你可能根本不需要数组。

先声明:

var markerLatLng = [], markers = [];

function point(map, places){
    for (var i = 0; i < places.length; i++ ) {
        var azienda = places[i];
        markerLatlng[i] = new google.maps.LatLng(azienda[1], azienda[2]);
        var infowindow = new google.maps.InfoWindow({
            content: azienda[3]
        });
        marker[i] = new google.maps.Marker({
            position: markerLatlng[i],
            map: map,
            title: azienda[0]
        });
        google.maps.event.addListener(marker[i], 'click', function () {
            infowindow.open(map,marker[i]);
        });
    }
};

没有数组:

function point(map, places){
    for (var i = 0; i < places.length; i++ ) {
        var azienda = places[i];
        var markerLatlng = new google.maps.LatLng(azienda[1], azienda[2]);
        var infowindow = new google.maps.InfoWindow({
            content: azienda[3]
        });
        var marker = new google.maps.Marker({
            position: markerLatlng,
            map: map,
            title: azienda[0]
        });
        google.maps.event.addListener(marker, 'click', function () {
            infowindow.open(map,marker);
        });
    }
};

编辑:添加更多分号以保持一致性:)

【讨论】:

    猜你喜欢
    • 2016-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-02
    • 1970-01-01
    • 2015-06-02
    相关资源
    最近更新 更多