簡體   English   中英

從map.data類獲取標記使用geojson Google Maps

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

首先,我是從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不僅會導致Point幾何形狀。 如果混合了點,多邊形和線串,則與點不同的任何東西在調用其get方法時都不會返回latLng對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM