如何使用谷歌地图API获取当前位置名称 [英] how to get current postion name using google map api
问题描述
我有 google
map
api,它可以获取 current $在
纬度
和经度
中c $ c>用户位置
但相反,我想获取位置名称
。
vidyaranyapura
这里是demo: https://jsfiddle.net/aoe4vf17/100/
html : javascript: 您可以使用反向地理编码这个目的。 更多信息可以在这里找到。 i have for example my location is bangalore => vidyaranyapura i want to get here is demo : https://jsfiddle.net/aoe4vf17/100/ html: javascript:
You can use reverse geocoding for this purpose.
Here is a working example with a More information can be found here.
这篇关于如何使用谷歌地图API获取当前位置名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
var map = new google.maps.Map(document.getElementById(map),{
center:new google.maps.LatLng(53.3478,-6.2597) ,
zoom:16,
mapTypeId:google.maps.MapTypeId.ROADMAP
});
var infoWindow = new google.maps.InfoWindow({
map:map
});
//尝试HTML5地理位置。
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(function(position){
var pos = {
lat:position.coords.latitude,
lng:position.coords.longitude
};
console.log(position);
infoWindow.setPosition(pos);
infoWindow.setContent('< b> < / b>');
map.setCenter(pos);
var marker = new google.maps.Marker({
position:pos,
map: map,
title:String(pos.lat)+,+ String(pos.lng),
});
},function(){
handleLocationError(true ,infoWindow,map.getCenter());
});
} else {
//浏览器不支持地理位置
handleLocationError(false,infoWindow,map.getCenter());
下面是一个工作示例,其中提供给Google API的 latitude
和 longitude
。
https://developers.google.com/maps/documentation/javascript/examples/geocoding-reverse
var geocoder = new google.maps.Geocoder; function geocodeLatLng(lat,lng){var latlng = {lat:lat,lng:lng}; geocoder.geocode({'location':latlng},function(results,status){if(status ==='OK'){if(results [0]){//这是你的格式化地址window.alert(results [0] .formatted_address);} else {window.alert('No results found');}} else {window.alert('Geocoder failed due to:'+ status);}});} geocodeLatLng(53.3478, - 6.2597);
< script src =https:/ /maps.googleapis.com/maps/api/js?v=3&sensor=false\"></script>
google
map
api which is able to get the current
user location
in latitude
and longitude
but instead i want to get the location name
.vidyaranyapura
<div id="map"></div>
var map = new google.maps.Map(document.getElementById("map"), {
center: new google.maps.LatLng(53.3478, -6.2597),
zoom: 16,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infoWindow = new google.maps.InfoWindow({
map: map
});
// Try HTML5 geolocation.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
console.log(position);
infoWindow.setPosition(pos);
infoWindow.setContent('<b>You are here.</b>');
map.setCenter(pos);
var marker = new google.maps.Marker({
position: pos,
map: map,
title: String(pos.lat) + ", " + String(pos.lng),
});
}, function() {
handleLocationError(true, infoWindow, map.getCenter());
});
} else {
// Browser doesn't support Geolocation
handleLocationError(false, infoWindow, map.getCenter());
}
latitude
and longitude
provided to the Google API.
https://developers.google.com/maps/documentation/javascript/examples/geocoding-reversevar geocoder = new google.maps.Geocoder;
function geocodeLatLng(lat, lng) {
var latlng = {
lat: lat,
lng: lng
};
geocoder.geocode({
'location': latlng
}, function(results, status) {
if (status === 'OK') {
if (results[0]) {
//This is yout formatted address
window.alert(results[0].formatted_address);
} else {
window.alert('No results found');
}
} else {
window.alert('Geocoder failed due to: ' + status);
}
});
}
geocodeLatLng(53.3478, -6.2597);
<script src="https://maps.googleapis.com/maps/api/js?v=3&sensor=false"></script>