gpt4 book ai didi

javascript - 放大时增加自定义标记的大小?使用 Google map 和 GeoJSON

转载 作者:行者123 更新时间:2023-12-03 07:10:36 25 4
gpt4 key购买 nike

我有一张 map ,通过 GeoJSON 文件调用 150 个位置,每个位置都由自定义图像 (22px,17px) 表示。当放大(缩放 17 后)时,问题就出现了,这些标记看起来相对较小。我想知道是否有一种方法可以确保每次放大时图标的大小都相同。

这是代码:

<!DOCTYPE html>
<html>

<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
html,
body,
#map-canvas {
height: 100%;
margin: 0px;
padding: 0px
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?v=3&amp;sensor=false &libraries=visualization"></script>

<script>
var map;

function initialize() {


var mapOptions = {
zoom: 13,
scrollwheel: true,
panControl: true,
zoomControl: true,
scaleControl: false,
disableDefaultUI: false,
center: new google.maps.LatLng(19.043516, -98.198232),
mapTypeControlOptions: {
mapTypeIds: [google.maps.MapTypeId.ROADMAP, 'map_style']
}
};

map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);



var script = document.createElement('script');
script.src = 'https://storage.googleapis.com/operations_poligone/datos/Test_JSON11.geojson';
document.getElementsByTagName('head')[0].appendChild(script);


map.data.setStyle(function(feature) {
var mag = 3;
var grd = feature.getProperty('Grd');
var ctgData = feature.getProperty('Ctg');
var ctgColor = {
"Seguridad": "https://storage.googleapis.com/operations_poligone/iconos/Safety_T02.png",
"Espacio": "https://storage.googleapis.com/operations_poligone/iconos/Neat_T02.png",
"Iluminacion": "https://storage.googleapis.com/operations_poligone/iconos/Lightning_T03.png"
};
return ({
icon: ctgColor[ctgData]
});
});

}

function eqfeed_callback(results) {
map.data.addGeoJson(results);
}

google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>

<body>
<div id="map-canvas"></div>
</body>

</html>

我在这里看到了一些解决方案,但我不知道从外部调用 GeoJSON 文件时如何应用它。 (How to set Google maps marker size dynamically)

最佳答案

您可以像 question you reference 中那样放大现有标记。

google.maps.event.addListener(map, 'zoom_changed', function() {
map.data.setStyle(function(feature) {
var mag = 3;
var grd = feature.getProperty('Grd');
var ctgData = feature.getProperty('Ctg');
var ctgColor = {
"Seguridad": "https://storage.googleapis.com/operations_poligone/iconos/Safety_T02.png",
"Espacio": "https://storage.googleapis.com/operations_poligone/iconos/Neat_T02.png",
"Iluminacion": "https://storage.googleapis.com/operations_poligone/iconos/Lightning_T03.png"
};
var size;
var zoom = map.getZoom();
if (zoom > 13) {
size = new google.maps.Size(44,34);
} else {
size = new google.maps.Size(22,17);
}
return ({
icon: {url: ctgColor[ctgData],
size: size,
scaledSize: size}
});
});
})

它们会变得模糊,您最好为更高的缩放级别创建单独的标记。

代码片段:

var map;

function initialize() {
var mapOptions = {
zoom: 13,
scrollwheel: true,
panControl: true,
zoomControl: true,
scaleControl: false,
disableDefaultUI: false,
center: new google.maps.LatLng(19.043516, -98.198232),
mapTypeControlOptions: {
mapTypeIds: [google.maps.MapTypeId.ROADMAP, 'map_style']
}
};

map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);

var script = document.createElement('script');
script.src = 'https://storage.googleapis.com/operations_poligone/datos/Test_JSON11.geojson';
document.getElementsByTagName('head')[0].appendChild(script);


map.data.setStyle(function(feature) {
var mag = 3;
var grd = feature.getProperty('Grd');
var ctgData = feature.getProperty('Ctg');
var ctgColor = {
"Seguridad": "https://storage.googleapis.com/operations_poligone/iconos/Safety_T02.png",
"Espacio": "https://storage.googleapis.com/operations_poligone/iconos/Neat_T02.png",
"Iluminacion": "https://storage.googleapis.com/operations_poligone/iconos/Lightning_T03.png"
};
return ({
icon: ctgColor[ctgData]
});
});
google.maps.event.addListener(map, 'zoom_changed', function() {
map.data.setStyle(function(feature) {
var mag = 3;
var grd = feature.getProperty('Grd');
var ctgData = feature.getProperty('Ctg');
var ctgColor = {
"Seguridad": "https://storage.googleapis.com/operations_poligone/iconos/Safety_T02.png",
"Espacio": "https://storage.googleapis.com/operations_poligone/iconos/Neat_T02.png",
"Iluminacion": "https://storage.googleapis.com/operations_poligone/iconos/Lightning_T03.png"
};
var size;
var zoom = map.getZoom();
document.getElementById("zoom").innerHTML = zoom;
if (zoom > 13) {
size = new google.maps.Size(44, 34);
} else {
size = new google.maps.Size(22, 17);
}
return ({
icon: {
url: ctgColor[ctgData],
size: size,
scaledSize: size
}
});
});
})
}

function eqfeed_callback(results) {
map.data.addGeoJson(results);
}

google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map-canvas {
height: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id="zoom"></div>
<div id="map-canvas"></div>

关于javascript - 放大时增加自定义标记的大小?使用 Google map 和 GeoJSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36622792/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com