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

时间:2017-09-27 12:14:58

标签: javascript html5 google-maps google-maps-api-3

我有google map api,可以current locationlatitude获取longitude用户location name,但我希望得到vidyaranyapura

例如我的位置是 bangalore =&gt; vidyaranyapura 我想得到<div id="map"></div>

这是demo:https://jsfiddle.net/aoe4vf17/100/

HTML:

 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:

Variable = awk 'Some code' filename.txt

3 个答案:

答案 0 :(得分:0)

您可以从此json获取数据: https://maps.google.com/maps/api/geocode/json 使用网址参数<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <ns2:getExistenceMunaResponse xmlns:ns2="http://www.fimasys.com/fsb"> <existenceMuna>true</existenceMuna> </ns2:getExistenceMunaResponse> </soap:Body> </soap:Envelope>

答案 1 :(得分:0)

您可以为此目的使用反向地理编码。 以下是向Google API提供[ { timestamp: "2017/09/26", "ABC": 1, "DEF": 0 }, { timestamp: "2017/09/27", "ABC": 2, "DEF": 3 } ] latitude的工作示例。

可在此处找到更多信息 https://developers.google.com/maps/documentation/javascript/examples/geocoding-reverse

longitude
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]) {

        //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);

答案 2 :(得分:0)

使用此功能并确保在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
            }
        );
}