Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/444.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_Ajax_Google Maps_Geolocation - Fatal编程技术网

Javascript 谷歌地图API-获取用户地址

Javascript 谷歌地图API-获取用户地址,javascript,ajax,google-maps,geolocation,Javascript,Ajax,Google Maps,Geolocation,我尝试了几种方法,用地理定位的方法获得游客所在国家的位置,但仍然没有用 有没有人能帮助atall,因为我想得到一个国家的访客,并使用ajax将其写入数据库 我当前的代码 <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script> <script> var geocoder; if (navigator.geolocation

我尝试了几种方法,用地理定位的方法获得游客所在国家的位置,但仍然没有用

有没有人能帮助atall,因为我想得到一个国家的访客,并使用ajax将其写入数据库

我当前的代码

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
<script>
  var geocoder;

  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(successFunction, errorFunction);
} 
//Get the latitude and the longitude;
function successFunction(position) {
    var lat = position.coords.latitude;
    var lng = position.coords.longitude;
    codeLatLng(lat, lng)
}

function errorFunction(){
    alert("Geocoder failed");
}

  function initialize() {
    geocoder = new google.maps.Geocoder();



  }

  function codeLatLng(lat, lng) {

    var latlng = new google.maps.LatLng(lat, lng);
    geocoder.geocode({'latLng': latlng}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
      console.log(results)
      var country = results[7].formatted_address;
      $('.location').text(country);
      var dataStrings = 'email=<?php echo $userid;?>&location='+ country;  
      console.log(dataStrinsg);
      $.ajax({
        type: "POST",
        url: "location-update.php",
        data: dataStrings,
        success: function() {
            location.reload();
            }
      });
      return false;
        if (results[1]) {
         //formatted address
        // alert(results[0].formatted_address)
        //find country name
             for (var i=0; i<results[0].address_components.length; i++) {
            for (var b=0;b<results[0].address_components[i].types.length;b++) {

            //there are different types that might hold a city admin_area_lvl_1 usually does in come cases looking for sublocality type will be more appropriate
                if (results[0].address_components[i].types[b] == "administrative_area_level_1") {
                    //this is the object you are looking for
                    city= results[0].address_components[i];
                    break;
                }
            }
        }
        //city data
        alert(city.short_name + " " + city.long_name)


        } else {
          alert("No results found");
        }
      } else {
        alert("Geocoder failed due to: " + status);
      }
    });
  }</script>
假设在地理编码之前调用initalize,则其他地理编码将未定义:

您正在此处访问结果[7]:


…但无法保证结果[7]的存在

您能指出代码的一个实时运行版本,或者提供一个我们可以运行的JSFIDLE吗?除了最简单的没有上下文的代码外,很难调试所有代码。initalize是用body标记加载的。现在运行Initalize时,会在警报框中弹出Geocoder failed:/
var country = results[7].formatted_address;