点击谷歌地图API并获取地址
Click on Google Maps API and get the address
以下代码允许在单击时获取坐标。但是,如何使用谷歌地图API在地图上单击时获取地址或城市名称或地区名称或国家/地区?
var myLatlng = new google.maps.LatLng(41.38,2.18);
var myOptions = { zoom: 13, center: myLatlng}
var map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);
google.maps.event.addListener(map, 'click', function(event) {alert(event.latLng);});
html, body, #map-canvas {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=geometry,places&ext=.js"></script>
<div id="map-canvas"></div>
您可以通过地理编码器传递event.latLng
以获取地址:
var myLatlng = new google.maps.LatLng(41.38, 2.18);
var myOptions = {
zoom: 13,
center: myLatlng
}
var map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);
var geocoder = new google.maps.Geocoder();
google.maps.event.addListener(map, 'click', function(event) {
geocoder.geocode({
'latLng': event.latLng
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
alert(results[0].formatted_address);
}
}
});
});
小提琴
你不能只通过谷歌API的纬度,LNG得到一个结果,但你可以通过搜索纬度,LNG和一个半径内的关键字来获得一些结果。
获取您需要通过其 placeID 搜索的特定地点信息的唯一方法
var myLatlng = new google.maps.LatLng(41.38,2.18);
var myOptions = { zoom: 13, center: myLatlng}
var map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);
var infoWindow;
var service;
google.maps.event.addListener(map, 'click', function(event) {
service = new google.maps.places.PlacesService(map);
infoWindow = new google.maps.InfoWindow();
var request = {
location: event.latLng,
keyword: 'food',
radius:500
};
service.radarSearch(request, callback);
});
function callback(results, status) {
if (status !== google.maps.places.PlacesServiceStatus.OK) {
console.error(status);
return;
}
for (var i = 0, result; result = results[i]; i++) {
addMarker(result);
}
}
function addMarker(place) {
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location,
icon: {
url: 'http://maps.gstatic.com/mapfiles/circle.png',
anchor: new google.maps.Point(10, 10),
scaledSize: new google.maps.Size(10, 17)
}
});
google.maps.event.addListener(marker, 'click', function() {
service.getDetails(place, function(result, status) {
if (status !== google.maps.places.PlacesServiceStatus.OK) {
console.error(status);
return;
}
infoWindow.setContent(result.name+"<br>"+result.formatted_address+"<br>"+result.formatted_phone_number);
infoWindow.open(map, marker);
});
});
}
https://jsfiddle.net/3qeop8ud/2/
m is event =====>
code here
const latlng = {
lat: parseFloat(e.lat),
lng: parseFloat(e.lng),};
m.view.google.maps.Geocoder.prototype.geocode({ location: latlng },
function(results, status) {
if (status =='OK') {
if (results[1]) {
alert(results[1].formatted_address);
}
}});
相关文章:
- 获取docker容器中运行的节点应用程序的IP地址
- 如何在asp.net中获取地图上的数据库地址
- 从express请求对象获取客户端MAC地址
- 在Safari上使用JavaScript获取IPv4地址
- 点击谷歌地图API并获取地址
- 从URL地址获取数据
- PHP 脚本仅从 MySQL 数据库中获取地址字段的第一行
- 如何在谷歌地图中从纬度和经度获取地址位置
- 快速.js获取地址和端口,同时仍在执行
- 从反向地理编码回调中获取地址
- 在地图中单击后获取地址
- 如何在谷歌地图上制作一组标记,从html中获取地址
- 在java中,通过谷歌获取地址的经度和纬度的最佳方式是什么
- Ng地图拖动标记后获取地址
- 从数据库获取地址到谷歌地图
- 使用onhouse获取地址的问题
- 如何从中心(pan)谷歌地图API获取地址
- 从Google Places API获取地址信息
- 获取地址与长和晚使用谷歌地图api
- 获取地址位置的几何形状