从map.data类获取标记使用geojson-google-maps

Getting marker from map.data class use geojson google maps

本文关键字:geojson-google-maps 获取 map data      更新时间:2023-09-26

首先,我是从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启动标记,我如何使动作侦听器?以及如何获取地图中的标记?

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

感谢

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对象。