javascript - 从map.data类获取标记使用geojson谷歌地图

标签 javascript php google-maps google-maps-api-3 geojson

首先,我从 geojson 启动标记,如果我想将标记用于监听器/操作,如何获取标记?

这是我的脚本

var map;

function initMap() {
    //makes map
    map = new google.maps.Map(document.getElementById('map'), {
        center: {lat: -6.9034482, lng: 107.6081381},
        zoom: 9,
        styles: [{"featureType":"water","stylers":[{"saturation":43},{"lightness":-11},{"hue":"#0088ff"}]},{"featureType":"road","elementType":"geometry.fill","stylers":[{"hue":"#ff0000"},{"saturation":-100},{"lightness":99}]},{"featureType":"road","elementType":"geometry.stroke","stylers":[{"color":"#808080"},{"lightness":54}]},{"featureType":"landscape.man_made","elementType":"geometry.fill","stylers":[{"color":"#ece2d9"}]},{"featureType":"poi.park","elementType":"geometry.fill","stylers":[{"color":"#ccdca1"}]},{"featureType":"road","elementType":"labels.text.fill","stylers":[{"color":"#767676"}]},{"featureType":"road","elementType":"labels.text.stroke","stylers":[{"color":"#ffffff"}]},{"featureType":"poi","stylers":[{"visibility":"off"}]},{"featureType":"landscape.natural","elementType":"geometry.fill","stylers":[{"visibility":"on"},{"color":"#b8cb93"}]},{"featureType":"poi.park","stylers":[{"visibility":"on"}]},{"featureType":"poi.sports_complex","stylers":[{"visibility":"on"}]},{"featureType":"poi.medical","stylers":[{"visibility":"on"}]},{"featureType":"poi.business","stylers":[{"visibility":"simplified"}]}]
        });

    //load marker from geojson
    map.data.loadGeoJson('<?php echo base_url().'index.php/json_site/geojsongetmap'?>');

    // set style marker
    map.data.setStyle(function(feature){
        var tit = feature.getProperty('nm_site');
        return{
            title: tit,
            icon: '<?php echo base_url()?>assets/images/mark3.png'
        };
    });

    //marker event
    map.data.addListener(marker, 'click', function(event) {
       map.setZoom(11);
       map.setCenter(marker.getPosition());  // I need to get the position of the marker who i clicked

    });
}

如果我从 geojson 启动标记,如何创建 Action 监听器? 以及如何获取 map 中存在的标记?

请帮助我,任何建议将不胜感激

谢谢

最佳答案

google.maps.Data.Point 的实例类并不完全是传统 google.maps.Marker 对象的直接替代品。对于初学者来说,它们是抽象数据,不依赖于特定的表示。由家长决定google.maps.Data层来决定如何绘制它们。

但是,您仍然可以捕获事件,但需要注意的是,单击发生在数据层上,该数据层接收 mouseEvent 作为参数。该参数包含您刚刚单击的功能。

这意味着您需要声明:

google.maps.event.addListener(map.data,'click',function(mouseEvent) {
    var clickedFeature = mouseEvent.feature,
        featureGeometry = clickedFeature.getGeometry(),
        featurePosition = featureGeometry.get();

    map.setCenter(featurePosition); 

});

请考虑到,通过数据层摄取 geoJson 不仅会产生点几何图形。如果您混合使用点、多边形和线串,则当您调用其 get 方法时,与点不同的任何内容都不会返回 latLng 对象。

关于javascript - 从map.data类获取标记使用geojson谷歌地图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34276644/

相关文章:

javascript - 对于大数据,多选下拉搜索非常慢

Javascript 创建 n 个变量

php - MySQL 表中的结果重复?

android - MvvmCross + Fragments + Back Button = 崩溃或控件不响应/绑定(bind)

javascript - 在 typescript 中为嵌套命名空间创建类型

javascript - 如何使用ajax从phonegap中的服务器获取数据?

php - 用于 php 的 Memcache 扩展 - xampp windows 不工作

php - openshift Mysql ip的rhc命令

android - (Android)是否可以更改谷歌地图中多段线的形状?

安卓 : Google map showing multi location