Google Maps-在特定点(长/纬度)上触发数据层上的点击事件 [英] Google Maps - Trigger a click event on a data layer at a specific point (long/lat)

查看:85
本文介绍了Google Maps-在特定点(长/纬度)上触发数据层上的点击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用GeoJSON加载Google地图,并且在加载地图并应用了数据层之后,我想为用户自动在特定点触发click事件.随后,他们可以在整个地图上单击以与之交互.

I am loading a google map with GeoJSON, and after the map is loaded and the data layers are applied, I would like to trigger a click event at a specific point automatically for the user. Subsequently, they can click all over the map to interact with it.

因此,对于自动加载部分,我尝试了如下操作:

So for the automatic load part, I tried something like this:

var x = new google.maps.LatLng(myLongitude, myLatitude);
google.maps.event.trigger(map.data, 'click', WHAT_GOES_HERE?);

但是我无法弄清楚该函数的最后一部分.单击相应的功能是这样的:

but I can't figure out what goes in the last part of that function. The corresponding function for clicking is this:

 map.data.addListener('click', function (event) {
    ...
    code
    ...
   }

事件触发,但事件当然为null.我需要(事件)填充一个功能(这就是预期的类型),但我似乎无法弄清楚如何从长/纬度中获取该功能.

The event fires, but event is null of course. I need (event) to be populated with a feature (that's what the type is expected to be) but I can't seem to figure out how to get the feature from a long/lat.

因此,我加载了我的数据层,我有了我的经纬度,但似乎无法从经纬度检索要素.关于如何检索此的任何建议?

So I load my data layers, I have my long/lat, but I can't seem to retrieve a feature from the long/lat. Any suggestions on how to retrieve this?

推荐答案

点击(类似于)(假设我们在其中放置了一个标记):

A (on)Click looks like this (let's say we put a marker there):

map.addListener('click', function (event) {
  var position = {lat: event.latLng.lat(), lng: event.latLng.lng()}
  //alert(JSON.stringify(position));
  var marker = new google.maps.Marker({position: position, map: map});
});

所以我们扭转一下.

假设您有点击布鲁塞尔或巴黎的按钮

let's say you heve buttons that invoke a click on Brussels or Paris

<input type="button" value="click on Brussels" onclick="clickOnMap(50.85, 4.35)">
<input type="button" value="click on Paris" onclick="clickOnMap(48.84, 2.35)">
<script>
function clickOnMap(lat, lng) {
  var event = {latLng: {}};
  event.latLng.lat = function() { return lat };
  event.latLng.lng = function() { return lng };
  google.maps.event.trigger(map, 'click', event);
}   
</script>

在同一坐标上单击按钮将具有与单击地图相同的效果.

Clicking on the button will have the same effect as clicking on the map, on the same coordinates.

当然,您可以自动调用clickOnMap函数.

And of course you can call the function clickOnMap automatically.

您对此有帮助吗?

假设您需要更多事件属性,例如event.feature.getProperty("name");

let's say you need more properties of event, like event.feature.getProperty("name");

尝试添加以下内容: 我不知道还会有什么,但是您可以继续添加这样的属性.

Try adding something like this: I don't know what else is expected, but you can keep adding properties like this.

function clickOnMap(lat, lng, name) {
  var event = {latLng: {}, feature: {}};
  event.latLng.lat = function() { return lat };
  event.latLng.lng = function() { return lng };
  event.feature.getProperty = function(property) { return name; };
  google.maps.event.trigger(map, 'click', event);
}

这篇关于Google Maps-在特定点(长/纬度)上触发数据层上的点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆