[英]google maps clustering in asp.net page
I have asp.net web forms page which has repeater to show multiple markers at the page. 我有asp.net Web表单页面,该页面具有中继器以在页面上显示多个标记。 I want to cluster the markers, I followed some examples found online but no success so far.
我想对标记进行聚类,我遵循了一些在网上找到的示例,但到目前为止没有成功。 This is what I have:
这就是我所拥有的:
html markup: html标记:
var markers = [ <asp:Repeater ID="rptMarkers" runat="server"> <ItemTemplate> { "title": '<%# Eval("Name") %>', "lat": '<%# Eval("Latitude") %>', "lng": '<%# Eval("Longitude") %>', "description": '<%# Eval("Name") %>' } </ItemTemplate> <SeparatorTemplate> , </SeparatorTemplate> </asp:Repeater> ];
var mapOptions = {
center: new google.maps.LatLng(43.648399, -79.485703),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
var markerCluster = new MarkerClusterer(map, markers, mapOptions );
</script>
<script type="text/javascript">
window.onload = function () {
var mapOptions = {
center: new google.maps.LatLng(43.648399, -79.485703),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var infoWindow = new google.maps.InfoWindow();
var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
for (i = 0; i < markers.length; i++) {
var data = markers[i]
var myLatlng = new google.maps.LatLng(data.lat, data.lng);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: data.title
});
(function (marker, data) {
google.maps.event.addListener(marker, "click", function (e) {
infoWindow.setContent(data.description);
infoWindow.open(map, marker);
});
})(marker, data);
}
}
</script>
<div id="dvMap" style="width: 500px; height: 277px">
</div>
</div>
Then, in the backend file I am getting all the data from database and sending to the repeater which properly renders multiple markers, as you can see at this screenshot as well: http://gyazo.com/049821984b889eefa551df663af0d2eb.png 然后,在后端文件中,我将从数据库中获取所有数据,并将其发送到正确呈现多个标记的转发器,您也可以在此屏幕截图中看到: http : //gyazo.com/049821984b889eefa551df663af0d2eb.png
This is the section I was trying to use to get the clustering working (visible in the code above as well): 这是我试图用来使集群正常工作的部分(也可以在上面的代码中看到):
var mapOptions = {
center: new google.maps.LatLng(43.648399, -79.485703),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
var markerCluster = new MarkerClusterer(map, markers, mapOptions );
When I view the source of the page this is the content I am getting regarding the markers data: 当我查看页面源代码时,这是我所获得的有关标记数据的内容:
<div class="divdisplay">
<script type="text/javascript" src="http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/src/markerclusterer.js"></script>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var markers = [
{
"title": 'Product Specialist',
"lat": '43.6877731',
"lng": '-79.2709108',
"description": 'Product Specialist'
}
,
{
"title": 'Finance Manager',
"lat": '43.7345524',
"lng": '-79.2561590',
"description": 'Finance Manager'
}
,
{
"title": 'New Job',
"lat": '43.7598255',
"lng": '-79.2252689',
"description": 'New Job'
}
,
{
"title": 'AD, Algorithmic Trading Development Team ',
"lat": '43.7240344',
"lng": '-79.3006776',
"description": 'AD, Algorithmic Trading Development Team '
}
,
{
"title": 'Financial Analyst - Investment Finance Systems Ana',
"lat": '43.7870613',
"lng": '-79.2761657',
"description": 'Financial Analyst - Investment Finance Systems Ana'
}
,
{
"title": 'New Business Coordinator CSR - Group Benefits',
"lat": '43.7891500',
"lng": '-79.1404511',
"description": 'New Business Coordinator CSR - Group Benefits'
}
,
{
"title": 'TD Job 1',
"lat": '43.7092267',
"lng": '-79.2962398',
"description": 'TD Job 1'
}
,
{
"title": 'Web Designer',
"lat": '43.7841207',
"lng": '-79.2917106',
"description": 'Web Designer'
}
,
{
"title": 'ceo',
"lat": '43.6488293',
"lng": '-79.3965691',
"description": 'ceo'
}
,
{
"title": 'IT Analyst',
"lat": '43.6546566',
"lng": '-79.3805941',
"description": 'IT Analyst'
}
,
{
"title": 'Part-Time MARKET RESEARCH INTERVIEWERS',
"lat": '43.6683729',
"lng": '-79.4392835',
"description": 'Part-Time MARKET RESEARCH INTERVIEWERS'
}
];
var mapOptions = {
center: new google.maps.LatLng(43.648399, -79.485703),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
var mcOptions = {gridSize: 50, maxZoom: 15};
var markerCluster = new MarkerClusterer(map, markers, mcOptions);
</script>
<script type="text/javascript">
window.onload = function () {
var mapOptions = {
center: new google.maps.LatLng(43.648399, -79.485703),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var infoWindow = new google.maps.InfoWindow();
var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
for (i = 0; i < markers.length; i++) {
var data = markers[i]
var myLatlng = new google.maps.LatLng(data.lat, data.lng);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: data.title
});
(function (marker, data) {
google.maps.event.addListener(marker, "click", function (e) {
infoWindow.setContent(data.description);
infoWindow.open(map, marker);
});
})(marker, data);
}
}
</script>
<div id="dvMap" style="width: 500px; height: 277px">
</div>
</div>
Every advice is greatly welcome. 任何建议都将受到欢迎。 Thanks, Laziale
谢谢,Laziale
You need to pass an array of google.maps.Marker objects to the marker clusterer - you're passing your array of initial marker data. 您需要将google.maps.Marker对象数组传递给标记集群器-您正在传递初始标记数据数组。
You need to create a new MarkerClusterer after all markers have been created, not before. 您需要在创建所有标记之后而不是之前创建一个新的MarkerClusterer。 So, remove these lines from the first script block in your example:
因此,从示例中的第一个脚本块中删除这些行:
var mcOptions = {gridSize: 50, maxZoom: 15};
var markerCluster = new MarkerClusterer(map, markers, mcOptions);
We'll move these to after all the markers have been created. 创建完所有标记后,我们将其移至。 So change your window.onload function to this:
因此,将您的window.onload函数更改为:
<script type="text/javascript">
window.onload = function () {
var mapOptions = {
center: new google.maps.LatLng(43.648399, -79.485703),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
// declare an array to keep all google.maps.Marker instances in:
var googleMarkers = [];
var infoWindow = new google.maps.InfoWindow();
var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
for (i = 0; i < markers.length; i++) {
var data = markers[i]
var myLatlng = new google.maps.LatLng(data.lat, data.lng);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: data.title
});
// add the new marker to the googleMarkers array
googleMarkers.push(marker);
(function (marker, data) {
google.maps.event.addListener(marker, "click", function (e) {
infoWindow.setContent(data.description);
infoWindow.open(map, marker);
});
})(marker, data);
}
// now all the markers have been created, make a new MarkerClusterer:
var mcOptions = {gridSize: 50, maxZoom: 15};
var markerCluster = new MarkerClusterer(map, googleMarkers, mcOptions);
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.