xkjyeah / vue-google-maps

Google maps component for vue with 2-way data binding
https://xkjyeah.github.io/vue-google-maps/
1.88k stars 474 forks source link

Is it possible to search an area based on the position of map? #786

Closed TomBell95 closed 1 year ago

TomBell95 commented 2 years ago

Hey guys - this plugin is a life saver! Does anyone know how to implement search functionality based on the position of the map (i.e. like airbnb)?

Screenshot 2022-01-05 at 17 24 26

If required I can post my code - all the lat/lng's are pulled from an API and I want to make area's searchable on the map for a better user experience.

Screenshot 2022-01-05 at 17 27 41

Thanks!

kkrell2016 commented 2 years ago

So you mean, that only the marker in the given area a shown?

-You can filter it serverside. Give the API the bounding box of the map to just show this markers. -Another way would be to get a ref to the map and use the computation functions of the google api. -Or just show the data where lat and lng is between lat&lng of the bounding box.

Will depend on your code.

srestraj commented 2 years ago

Maybe I'm a little late to the party 😁. Considering that your API filters data based on the lat/lng provided, you can achieve this by first firing an event on dragend and calling the getCenter() method to get the lat/lng of the center.

<template>
<GmapMap
        :center="{
          lat: parseFloat(center.lat),
          lng: parseFloat(center.lng),
        }"
        ref="map"
        @dragend="boundsChanged($event)"
        @zoom_changed="boundsChanged($event)"
>...</GmapMap>

</template>
<script>
methods: {
     boundsChanged(e) {
            // get the lat/lng of the center
            console.log(this.$refs.map.$mapObject.getCenter().lat(), this.$refs.map.$mapObject.getCenter().lng())
     }
}
</script>

With this, what I would do is only display the "Search this area" button when the dragend or zoom_changed event is triggered and then use the center's lat/lng to call the API for nearby results.

But it depends on your API too.