Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用谷歌地图api获取当前职位名称_Javascript_Html_Google Maps_Google Maps Api 3 - Fatal编程技术网

Javascript 如何使用谷歌地图api获取当前职位名称

Javascript 如何使用谷歌地图api获取当前职位名称,javascript,html,google-maps,google-maps-api-3,Javascript,Html,Google Maps,Google Maps Api 3,我有googlemapapi,它能够在纬度和经度中获取当前用户位置,但我想获取位置名称 例如,我的位置是班加罗尔=>vidyaranyapura我想得到vidyaranyapura 以下是演示: html: <div id="map"></div> var map = new google.maps.Map(document.getElementById("map"), { center: new google.maps.LatLng(53.3478, -6.2

我有
google
map
api,它能够在
纬度
经度
中获取
当前
用户
位置
,但我想获取
位置名称

例如,我的位置是班加罗尔=>vidyaranyapura我想得到
vidyaranyapura

以下是演示:

html:

<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());
 }

javascript:

<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());
 }
var map=new google.maps.map(document.getElementById(“map”){
中心:新google.maps.LatLng(53.3478,-6.2597),
缩放:16,
mapTypeId:google.maps.mapTypeId.ROADMAP
});
var infoWindow=new google.maps.infoWindow({
地图:地图
});
//试试HTML5地理定位。
if(导航器.地理位置){
navigator.geolocation.getCurrentPosition(函数(位置){
var pos={
纬度:位置坐标纬度,
lng:position.coords.longitude
};
控制台日志(位置);
信息窗口。设置位置(pos);
infoWindow.setContent('您在这里');
地图设置中心(pos);
var marker=new google.maps.marker({
职位:pos,,
地图:地图,
标题:字符串(位置lat)+“,”+字符串(位置lng),
});
},函数(){
handleLocationError(true,infoWindow,map.getCenter());
});
}否则{
//浏览器不支持地理位置
handleLocationError(false,infoWindow,map.getCenter());
}

您可以从此json获取数据:
通过url参数
?latlng=[纬度,经度]&key=[你的_gmap_api_键]

你可以使用反向地理编码来实现这一目的。 下面是一个工作示例,其中向Google API提供了
纬度
经度

更多信息可在此处找到。

var geocoder=new google.maps.geocoder;
功能地理编码LATLNG(lat,lng){
var latlng={
拉特:拉特,
液化天然气:液化天然气
};
地理编码({
“位置”:latlng
},功能(结果、状态){
如果(状态=='OK'){
如果(结果[0]){
//这是您的格式化地址
window.alert(结果[0]。格式化的\u地址);
}否则{
window.alert(“未找到结果”);
}
}否则{
window.alert('地理编码器由于:'+状态而失败);
}
});
}
geocodeLatLng(53.3478,-6.2597)

利用此功能,并确保在https域上运行它

function getlocation(){
        /* Chrome need SSL! */
        var is_chrome = /chrom(e|ium)/.test( navigator.userAgent.toLowerCase() );
        var is_ssl    = 'https:' == document.location.protocol;
        if( is_chrome && ! is_ssl ){
            return false;
        }
        /* HTML5 Geolocation */
        navigator.geolocation.getCurrentPosition(
            function( position ){ // success cb

                /* Current Coordinate */
                var lat = position.coords.latitude;
                var lng = position.coords.longitude;
                var google_map_pos = new google.maps.LatLng( lat, lng );

                /* Use Geocoder to get address */
                var google_maps_geocoder = new google.maps.Geocoder();
                google_maps_geocoder.geocode(
                    { 'latLng': google_map_pos },
                    function( results, status ) {
                        if ( status == google.maps.GeocoderStatus.OK && results[0] ) {
                            console.log( results[0].formatted_address );
                            currentlocation = results[0].formatted_address;

                        }
                    }
                );
            },
            function(){ // fail cb
            }
        );
}