简体   繁体   English

从map.data类获取标记使用geojson Google Maps

[英]Getting marker from map.data class use geojson google maps

first of all I was initiate marker from geojson, and how I can get the marker if i want use marker for listener/action? 首先,我是从geojson启动标记的,如果我想将标记用于侦听器/操作,如何获取标记?

this is my script 这是我的剧本

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

    });
}

how I can make action listener if I initiate marker from geojson? 如果我从geojson启动标记,如何制作动作监听器? and how I can get the marker who existing in my map? 以及如何获取存在于地图中的标记?

please help me, any suggest would be appreciated 请帮助我,任何建议将不胜感激

thanks 谢谢

Instances of the google.maps.Data.Point class are not exactly a drop-in replacement for traditional google.maps.Marker objects. google.maps.Data.Point类的实例并不能完全替代传统的google.maps.Marker对象。 For starters, they are abstract data, not tied to a particular representation. 对于初学者来说,它们是抽象数据,不依赖于特定的表示形式。 It's up to the parent google.maps.Data layer to decide how to draw them. 由父级google.maps.Data图层决定如何绘制它们。

However, you can still capture events, with the caveat that the click happens on the Data layer, which receives a mouseEvent as argument. 但是,您仍然可以捕获事件,但需要注意的是单击发生在数据层上,该数据层将mouseEvent作为参数。 This argument contains the feature over which you just clicked. 此参数包含您刚刚单击的功能。

This means you need to declare: 这意味着您需要声明:

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

    map.setCenter(featurePosition); 

});

Please take into consideration that ingesting geoJson with the Data layer can result not just in Point geometries. 请注意,使用数据层摄取geoJson不仅会导致Point几何形状。 If you get a mix of points, polygons and linestrings, anything different from a point won't return a latLng object when you call its get method. 如果混合了点,多边形和线串,则与点不同的任何东西在调用其get方法时都不会返回latLng对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM