获取Google Maps-Markerclusterer v3搜索区域中所有标记的列表

时间:2019-03-26 07:37:36

标签: jquery google-maps google-maps-api-3 markerclusterer

我需要在Google地图的缩放区域中获取标记列表。

我已经检查了另一个问题:Get a list of markers in bounds of google map using markerclusterer v3

哪个接近我想要的,但不完全相同。

这是我用来生成地图的JavaScript

$('#autocomplete').click(function() {
  $(this).val('');
   $(this).attr('placeholder', '');
});
var map, places, infoWindow;
var markers = [];
var autocomplete;
var countryRestrict = {
    'country': 'dk'
};

function initMap() {

    map = new google.maps.Map(document.getElementById('map'), {
        zoom: 7,
        center: {
            lat: 56.220512,
            lng: 11.245051
        },
        mapTypeControl: false,
        panControl: true,
        zoomControl: true,
        streetViewControl: false
    });

    autocomplete = new google.maps.places.Autocomplete(
        (
            document.getElementById('autocomplete')), {
            componentRestrictions: countryRestrict
        });
    places = new google.maps.places.PlacesService(map);

    autocomplete.addListener('place_changed', onPlaceChanged);

    setMarkers(map);
    var marker = new google.maps.Marker({
        map: map,
        anchorPoint: new google.maps.Point(0, -29)
    });

    autocomplete.addListener('place_changed', function() {
        marker.setVisible(false);
        var place = autocomplete.getPlace();
        if (!place.geometry) {
            event.preventDefault();
        }

        if (place.geometry.viewport) {} else {
            map.setCenter(place.geometry.location);
            map.setZoom(10);
        }
        marker.setPosition(place.geometry.location);
        marker.setVisible(true);

        var address = '';
        if (place.address_components) {
            address = [
                (place.address_components[0] && place.address_components[0].short_name || ''),
                (place.address_components[1] && place.address_components[1].short_name || ''),
                (place.address_components[2] && place.address_components[2].short_name || '')
            ].join(' ');
        }

    });
}
var beaches = [
    <?php while ( have_rows('tilfoj_vaerksted_repeater') ) : the_row(); ?>

    ['test', <?php the_sub_field('latitude_tilfoj_vaerksted_repeater'); ?>, <?php the_sub_field('longitude_tilfoj_vaerksted_repeater'); ?>, 4],
    <?php endwhile; ?>

];

function setMarkers(map) {
    for (var i = 0; i < beaches.length; i++) {
        var beach = beaches[i];
        var marker = new google.maps.Marker({
            position: {
                lat: beach[1],
                lng: beach[2]
            },
            map: map,
            icon: "/wp-content/uploads/2019/03/map-icon.png",
        });
    }
}

function onPlaceChanged() {

    var place = autocomplete.getPlace();
    if (place.geometry) {
        map.panTo(place.geometry.location);
        map.setZoom(10);
    } else {
        document.getElementById('autocomplete').placeholder = 'Søg';
    }


}

我真正需要的是在Google Maps的搜索区域中获取所有标记,然后隐藏/显示Google Map下面的div元素。您可以在下面的链接中看到当前示例。我想我可以使用Markercluster,但我不太确定该怎么做。

这是生产站点的链接:

1 个答案:

答案 0 :(得分:0)

我找到了一个完美的解决方案。

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    imageView = findViewById(R.id.img);
    myDrawView = findViewById(R.id.draw);


 myDrawView.setBackground(getResources()
   .getDrawable(R.drawable.bg_customer));
    Button button1 = findViewById(R.id.button1);
    button1.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {
            Bitmap well = myDrawView.getBitmap();
            imageView.setImageBitmap(well);
            imageView.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View view)
                {
                    //now save in room database 
                }
            });
        }
    });
}

}