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

get lat-lng click on the map

Open Akifcan opened this issue 5 years ago • 10 comments

hello, how can i do when clicked map get a lng lat coorditanes?

i tried this way

> <script type="text/javascript">
> 	import {gmapApi} from 'vue2-google-maps'
> 
> 
> 	export default{
> 		data(){
> 			return{
> 				markers:[
> 				{
> 					position: {lat: 10, lng: 10},
> 				},
> 				{
> 					position: {lat: 50, lng: 10},
> 				}
> 				]
> 			}
> 		},
> 		methods: {
> 			mark(){
> 				console.log(this.google)
> 			}
> 		},
> 		computed: {
> 			google: gmapApi
> 		}
> 	}
> </script>
<template>
	<q-page padding>
		
		<GmapMap
		id='map'
		:center="{lat:10, lng:10}"
		:zoom="7"
		map-type-id="terrain"
		style="width: 100%; height: 500px"
		@click='mark()'
		>
		<GmapMarker
		v-for='marker in markers'
		:position="marker.position"
		:clickable="true"
		:draggable="true"
		/>

	</GmapMap>		


</q-page>
</template>

and return this values

LatLng: ƒ (a,b,c)
arguments: null
caller: null
length: 3
name: ""
prototype: {toString: ƒ, toJSON: ƒ, equals: ƒ, toUrlValue: ƒ, constructor: ƒ}
__proto__: ƒ ()
[[FunctionLocation]]: js?key=AIzaSyD8k4GDb…ueGoogleMapsInit:69
[[Scopes]]: Scopes[2]

but dont write any info lat lng

Akifcan avatar Nov 21 '19 14:11 Akifcan

@Akifcan You can get the coordinates by calling the lat() and lng() functions like this:

           mark(event) {
               console.log(event.latLng.lat())
               console.log(event.latLng.lng())
           }

gazben avatar Dec 02 '19 15:12 gazben

console.log(event.latLng.lat()) console.log(event.latLng.lng())

This works perfectly if

<GmapMap
   // ...
 @click="mark"
/>
//...
methods: {

  mark(event) {
    console.log(event.latLng.lat())
     console.log(event.latLng.lng())
  }
}

Tobenna-KA avatar May 04 '20 18:05 Tobenna-KA

the function event.latLng.lat() is work only if function is called by clicking on marker not map

meharwaheed avatar Aug 13 '21 16:08 meharwaheed

Cool! @gazben. You're the best.

Thanks a lot. ❤

modavidc avatar Dec 30 '21 01:12 modavidc

@meharwaheed it's not true. Maybe you had a bad try.

It worked perfect for me the first time.

modavidc avatar Dec 30 '21 01:12 modavidc

Is there any way to show the coordinates, without defining @click in GmapMap?

choubey1 avatar Jan 13 '22 04:01 choubey1

@gazben

choubey1 avatar Jan 13 '22 04:01 choubey1

I don't think so. Without the @click what coordinates do you wnat to query?

gazben avatar Jan 13 '22 08:01 gazben

I want to show lat and lng but I don't want to put @click in GmapMap

choubey1 avatar Jan 13 '22 08:01 choubey1

Using the bounds_changed event, I can see these results in the console. I hope it can be useful in some cases.

// vue2-google-maps in Nuxt 2.15.3

<gmap-map //.... @bounds_changed="setCoordinates" @dragend="coordinates" //dragend >

data() { return { coordinatesMap: null, } }, methods: { setCoordinates(newCoordinates) { this.coordinatesMap = newCoordinates }, coordinates() { //Do something console.log(this.coordinatesMap.Ab) console.log(this.coordinatesMap.Ua) }, }

// Console { "h": 47.371524727114014, "j": 47.395845736123185 } { "h": -1.2169070898230872, "j": -1.183604782694181 } /***** I think it is associated with *****/ { "south": 47.371524727114014, "west": -1.2169070898230872, "north": 47.395845736123185, "east": -1.183604782694181 }

image

orlando-mm avatar Jun 10 '22 04:06 orlando-mm