【发布时间】:2018-01-14 23:58:03
【问题描述】:
我正在尝试从地图上的点击事件中获取坐标
这是我试图得到的结果:Here
这里是我的代码:
import { Component, OnInit } from '@angular/core';
import { ViewChild } from '@angular/core';
import { } from '@types/googlemaps';
import { map } from 'rxjs/operator/map';
import { Input, Output, EventEmitter } from '@angular/core';
@Component({
selector: 'app-google-map',
templateUrl: './google-map.component.html',
styleUrls: ['./google-map.component.css']
})
export class GoogleMapComponent implements OnInit {
constructor() { }
markers: Array<Object>
myLatLng = { lat: 53.131083, lng: 23.154742 };
latitude: 53.131083;
longitute: 23.154742;
googleMap: google.maps.Map;
ngOnInit() {
var mapCanvas = document.getElementById("map");
var myCenter = new google.maps.LatLng(53.131083, 23.154742);
var mapOptions = {
center: myCenter,
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
this.googleMap = new google.maps.Map(mapCanvas, mapOptions);
google.maps.event.addListener(map, 'click', function (event) {
this.placeMarker(event);
});
}
placeMarker(event) {
//-------------- i can't get map coords -------------
var eventLatLng = { lat: event.clientX, lng: event.clientY };
console.log(eventLatLng);
var marker = new google.maps.Marker({
position: this.myLatLng,
map: this.googleMap
});
var infowindow = new google.maps.InfoWindow({
content: 'Marker Location:' + marker.getPosition()
});
infowindow.open(this.googleMap, marker);
}
addBicycleLayer() {
var bikeLayer = new google.maps.BicyclingLayer();
bikeLayer.setMap(this.googleMap);
}
setMapType(mapTypeId: string) {
this.googleMap.setMapTypeId(mapTypeId)
}
setCenter(e: any) {
e.preventDefault();
this.googleMap.setCenter(new google.maps.LatLng(this.latitude, this.longitute));
}
}
HTML 组件:
<div id="map" style="width:100%;height:600px" (click)="placeMarker($event)">
</div>
我不知道如何获得地图坐标。这些在 placeMarker 函数中的坐标只是客户端坐标(未与地图集成) 我找到了一些https://angular-maps.com/,但这将是一个大项目,我不想依赖这个。 我将不胜感激任何帮助:)
干杯
【问题讨论】:
-
看看 Google Maps JS API,你不应该使用
event.latLng来获取坐标位置Marker(event) (developers.google.com/maps/documentation/javascript/3.exp/…)? -
试过了,用简单的语言,角度不知道 event.latLng 是什么,它在 Firefox 中显示了一个错误(你正在谈论的 event.latLng 在这里:w3schools.com/graphics/… 我想要得到相同的结果
-
我可以从我把自己放在代码中的坐标中添加标记,但很难通过点击事件来实现它并从地图中获取它
-
我不确定我是否遵循,您正在尝试从点击事件中获取坐标,即事件对象,对吗?如果是这样,Firefox 会为
event.latLng显示什么错误?如果不能,请您进一步解释您要做什么。 -
我想从地图中获取纬度和经度以制作标记,来自“点击”事件。 (我将点击地图,获取纽约街道的坐标并在地图上做标记)。将代码更改为 event.latLng(请参阅 var 标记中的“位置”)并单击地图后,我收到此错误:ibb.co/hsESGm
标签: javascript angular google-maps typescript google-maps-api-3