繁体   English   中英

Angular 2+ Google Maps从地图上的点击位置获取坐标以更新标记位置

[英]Angular 2+ Google Maps get coordinates from click position on map to update marker position

我想做一些可能非常简单的事情,但我似乎无法弄明白。

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

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

<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 )但是这个事件似乎没有发出任何坐标。 我现在正在这样做:

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方面,我不得不使用:

(mapClick)="placeMarker($event)"

在打字稿方面我不得不使用:

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

这将单独返回纬度和纬度,现在我可以将这些坐标推送到HTML文件中的标记以更新其位置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM