vue-google-maps icon indicating copy to clipboard operation
vue-google-maps copied to clipboard

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

Open TomBell95 opened this issue 3 years ago • 2 comments

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!

TomBell95 avatar Jan 05 '22 17:01 TomBell95

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.

kkrell2016 avatar Jan 07 '22 11:01 kkrell2016

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.

srestraj avatar May 05 '22 04:05 srestraj