【发布时间】:2015-11-23 22:55:14
【问题描述】:
我正在尝试构建一个基于地图的站点,该站点可识别用户的地理位置,在他/她的位置绘制一个标记,然后使用该标记/位置单击数据层(在本例中为 GeoJSON 层) .本质上,如果用户位于 geojson 文件划定的区域,则用户的位置应自动触发信息窗口。理想情况下,每次用户更改位置时,都会单击地图以检查此 GeoJSON 图层以获取信息。
到目前为止,我可以成功获取用户的位置。地图以该位置为中心。手动单击 GeoJSON 图层也会正确填充信息窗口。但获取用户位置时不会自动点击。
我见过很多强制点击标记的例子,但我似乎找不到点击数据层的例子。不幸的是,我更像是一名 GIS 人员,被分配到这方面的编码工作,这超出了我的范围,所以我很难弄清楚我哪里出了问题。
这是脚本,也许我在这里犯了一个错误:
$<script type="text/javascript">
//centers the map on Iowa City
var map,
currentPositionMarker,
mapCenter = new google.maps.LatLng(41.661354, -91.534729),
map;
function initializeMap()
{
map = new google.maps.Map(document.getElementById('map_canvas'), {
zoom: 18,
center: mapCenter,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
map.data.loadGeoJson('test2.json');
map.data.setStyle({
strokeColor: '#2687bf',
strokeWeight: 5
});
map.data.addListener('click', function(event) {
document.getElementById('info-box').textContent =
event.feature.getProperty('description');
});
}
function locError(error) {
// the current position could not be located
alert("The current position could not be found!");
}
function setCurrentPosition(pos) {
currentPositionMarker = new google.maps.Marker({
map: map,
draggable: true,
position: new google.maps.LatLng(
pos.coords.latitude,
pos.coords.longitude
),
title: "Current Position"
});
new google.maps.event.trigger( 'test2.json', 'click' );
map.panTo(new google.maps.LatLng(
pos.coords.latitude,
pos.coords.longitude
));
}
function displayAndWatch(position) {
// set current position
setCurrentPosition(position);
// watch position
watchCurrentPosition();
}
function watchCurrentPosition() {
var positionTimer = navigator.geolocation.watchPosition(
function (position) {
setMarkerPosition(
currentPositionMarker,
position
);
});
}
function setMarkerPosition(marker, position) {
marker.setPosition(
new google.maps.LatLng(
position.coords.latitude,
position.coords.longitude)
);
}
function initLocationProcedure() {
initializeMap();
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(displayAndWatch, locError);
} else {
alert("Your browser does not support the Geolocation API");
}
}
$(document).ready(function() {
initLocationProcedure();
});
</script>
</head>
<body>
<div id="map_canvas" style="height:600px;"></div>
<div id="info-box" style="height:250px;">INFO</div>
</body>
</html>
这里是指向我的 JSON 和完整 HTML 文件的链接: https://sites.google.com/site/ecocritkml/coding
JSON 显然是爱荷华州爱荷华市特有的,但它可以在文本编辑器中轻松修改。任何想法在这里都会很有帮助。
【问题讨论】:
标签: javascript jquery json google-maps google-maps-api-3