Angular 2+ Google Maps从地图上的点击位置获取坐标以更新标记位置 [英] Angular 2+ Google Maps get coordinates from click position on map to update marker position

查看:53
本文介绍了Angular 2+ Google Maps从地图上的点击位置获取坐标以更新标记位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一些可能真的很简单的事情,但是我似乎无法弄清楚.

I want to do something which is probably really simple however I can't seem to figure it out.

我将Angular2 + Google Maps模块集成到了我的Angular项目中( https://angular-maps.com/).现在,我希望能够通过在地图上的某处单击来替换标记.为此,我需要获取用户在地图上单击的位置的坐标.如果我有这些坐标,则可以更新经度和纬度以移动标记.但是,我不确定如何获取点击的位置.

I integrated the Angular2+ Google Maps module to my Angular project (https://angular-maps.com/). Now I want to be able to replace the marker by clicking somewhere on the map. In order to do this I need to fetch the coordinates of the location where the user clicked on the map. If I have these coordinates I can update the longitude and latitude to move the marker. However I am not sure how to fetch the clicked location.

这是我在html文档中的地图实现:

This is my map implementation in the html document:

<agm-map [latitude]="latitude" [longitude]="longitude" [zoom]="zoom" [streetViewControl]="false" [mapTypeControl]="true" [fullscreenControl]="true" (mapClick)="placeMarker()">
   <agm-marker [latitude]="latitude" [longitude]="longitude" [iconUrl]="'assets/geomarker.png'"></agm-marker>
   <agm-circle [latitude]="latitude" [longitude]="longitude" [radius]="20" [fillOpacity]="0.50" [fillColor]="'#00A19A'"></agm-circle>
</agm-map>

我正在使用mapClick输出事件.( https://angular-maps.com/api-docs/agm-core/components/AgmMap.html )但是,此事件似乎未发出任何坐标.我现在正在参加这样的活动:

I am using the mapClick output event. (https://angular-maps.com/api-docs/agm-core/components/AgmMap.html) However this event does not seem to emit any coordinates. I am cathing the event like this right now:

placeMarker(){
  console.log(event);
}

这是输出:

MouseEvent {isTrusted: true, screenX: 3657, screenY: 67, clientX: 401, clientY: 318…}
altKey
:
false
bubbles
:
true
button
:
0
buttons
:
0
cancelBubble
:
false
cancelable
:
true
clientX
:
401
clientY
:
318
composed
:
true
ctrlKey
:
false
currentTarget
:
null
defaultPrevented
:
false
detail
:
1
eventPhase
:
0
fromElement
:
null
isTrusted
:
true
layerX
:
364
layerY
:
154
metaKey
:
false
movementX
:
0
movementY
:
0
offsetX
:
364
offsetY
:
154
pageX
:
401
pageY
:
318
path
:
Array(23)
relatedTarget
:
null
returnValue
:
true
screenX
:
3657
screenY
:
67
shiftKey
:
false
sourceCapabilities
:
InputDeviceCapabilities
srcElement
:
div
target
:
div
timeStamp
:
18285.225000000002
toElement
:
div
type
:
"click"
view
:
Window
which
:
1
x
:
401
y
:
318
__proto__
:
MouseEvent

推荐答案

我自己找到了答案.

在HTML方面,我不得不使用:

On the HTML side I had to use:

(mapClick)="placeMarker($event)"

在打字稿方面,我必须使用:

And on the typescript side I had to use:

  placeMarker($event){
    console.log($event.coords.lat);
    console.log($event.coords.lng);
  }

这将分别返回纬度和经度,现在我可以将这些坐标推入HTML文件中的标记以更新其位置.

This returns the latitude and lontitude individually, now I can push these coordinates to the marker in the HTML file to update its location.

这篇关于Angular 2+ Google Maps从地图上的点击位置获取坐标以更新标记位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆