简体   繁体   English

在数组中显示多个标记-Google Maps API

[英]Display Multiple Markers in Array - Google Maps API

How can I clean this so that the code handles multiple listings better? 我如何清理它,以便代码更好地处理多个清单? I have seen some code examples that pull the marker info from an array, but I can't seem to get it to work. 我已经看到了一些代码示例,这些示例从数组中提取标记信息,但似乎无法正常工作。

The markers need to have "icon:", "url:" and "title:" attributes. 标记必须具有“ icon:”,“ url:”和“ title:”属性。 The "icon:" so I can change each markers appearance, the "url:" to point through to the page dedicated to each marker and the "title" just to add the markers name on hover. “图标:”使我可以更改每个标记的外观,“ URL:”以指向每个标记专用的页面,而“标题”仅用于在悬停时添加标记名称。

I also need the array in the script as I am not gathering it from a database or anything like that. 我还需要脚本中的数组,因为我不是从数据库或类似的数据库中收集数组。

I am pretty green when it comes to javascript and google maps api, any help would be greatly appreciated. 对于javascript和google maps api,我非常环保,任何帮助将不胜感激。

function createMap(lat, lng, zoomVal) {
 var mapOptions = { center: new google.maps.LatLng(lat, lng),    
 zoom: zoomVal,   
 scrollwheel: false,  
 mapTypeId: google.maps.MapTypeId.ROADMAP 
}; 
map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);


var myLatlnglow = new google.maps.LatLng(23.654332,-79.387867);

var markerlow1 = new google.maps.Marker({
    position: myLatlnglow,
    icon: 'images/map-dot.png',
    map: map,
    url: '#',
    title: 'Name'
});
google.maps.event.addListener(markerlow1, 'click', function() {
window.location.href = markerlow1.url;
});


var myLatlnglow = new google.maps.LatLng(23.688458,-79.300619);

var markerlow = new google.maps.Marker({
    position: myLatlnglow,
    icon: 'images/map-dot.png',
    map: map,
    url: '#',
    title: 'Name'
});
google.maps.event.addListener(markerlow, 'click', function() {
window.location.href = markerlow.url;
});


}

var map;
function initialize() { 
 createMap(23.668493, -29.410812,12);
  if(navigator.geolocation) {  
  success = function(position) {    
 createMap(position.coords.latitude, position.coords.longitude,13);  
};   
error = function() {
 createMap(23.648493, -29.410812,12); 
}    
 navigator.geolocation.getCurrentPosition(success, error);  
} 
}

If I were you I would make an object of objects for the sake of readability. 如果我是你,那么出于可读性考虑,我将对象作为对象。 If you must have efficiency, and want to save space and typing, replace the object with an array and address the data by index (0,1,2...) 如果必须提高效率,并且想节省空间和键入内容,请用数组替换对象,并按索引(0,1,2 ...)处理数据。

Here's a demo 这是一个演示

    // inside function createMap(...)
      ...
    markerData = {
        bing: {
            lat: 23.654332,
            lng: -79.387867,
            icon: "http://labs.google.com/ridefinder/images/mm_20_red.png",
            url: "http://www.bing.com/",
            title: "some search engine"
        },
        yahoo: {
            lat: 23.688458,
            lng: -79.300619,
            icon: "http://labs.google.com/ridefinder/images/mm_20_blue.png",
            url: "http://www.yahoo.com/",
            title: "Great free games"
        }
    };

    for (markerId in markerData) {
        markers[markerId] = createMarker(markerData[markerId]);
    }

    markers['bing'].setTitle("new title");
}

function createMarker(data) {
    var myLatLng = new google.maps.LatLng(data.lat, data.lng);

    var marker = new google.maps.Marker({
        position: myLatLng,
        icon: data.icon,
        map: map,
        title: data.title
    });
    google.maps.event.addListener(marker, 'click', function() {
        window.location.href = data.url;
    });

    return marker;
}

It should also help later to save the references to the created markers, I'm using the global object markers and the same IDs. 稍后还将有助于保存对创建的标记的引用,我使用的是全局对象markers和相同的ID。

var map;
var markers = {};

If you later need to change a marker property it is accessible through markers . 如果以后需要更改标记属性,则可以通过markers访问它。

   markers['bing'].setTitle("new title");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM