Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/440.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 如何将从地理位置获取的城市名称存储到变量并发送ajax请求? 新Vue({ el:'时尚', 数据:{ 数据:{}, }, 挂载(){ var self=这个; navigator.geolocation.getCurrentPosition(成功,错误); 功能成功(职位){ 地理编码变量https://maps.googleapis.com/maps/api/geocode/json?latlng='+position.coords.latitude+'%2C'+position.coords.longitude+'&language=en'; $.getJSON(地理编码).done(函数(位置){ $('#country').html(location.results[0]。address_components[5]。long_name); $('#state').html(location.results[0]。address_components[4]。long_name); $('#city').html(location.results[0]。address_components[2]。long_name); $('#address').html(location.results[0]。格式化的#address); $('#latitude').html(position.coords.latitude); $('#longitude').html(position.coords.longitude); }) var lat=位置坐标纬度; $.ajax({ 网址:'https://api/post//', 数据:{ 纬度:位置坐标纬度, lon:position.coords.longitude, 城市:位置。结果[0]。地址\u组件[2]。长\u名称, }, 类型:“POST”, 数据类型:“json”, 成功:职能(e){ 如果(e.status==1){ self.data=e.data; console.log(如数据) } } }); 控制台日志(lat); } 函数错误(err){ console.log(错误) } } })_Javascript_Jquery_Ajax_Google Maps_Vue.js - Fatal编程技术网

Javascript 如何将从地理位置获取的城市名称存储到变量并发送ajax请求? 新Vue({ el:'时尚', 数据:{ 数据:{}, }, 挂载(){ var self=这个; navigator.geolocation.getCurrentPosition(成功,错误); 功能成功(职位){ 地理编码变量https://maps.googleapis.com/maps/api/geocode/json?latlng='+position.coords.latitude+'%2C'+position.coords.longitude+'&language=en'; $.getJSON(地理编码).done(函数(位置){ $('#country').html(location.results[0]。address_components[5]。long_name); $('#state').html(location.results[0]。address_components[4]。long_name); $('#city').html(location.results[0]。address_components[2]。long_name); $('#address').html(location.results[0]。格式化的#address); $('#latitude').html(position.coords.latitude); $('#longitude').html(position.coords.longitude); }) var lat=位置坐标纬度; $.ajax({ 网址:'https://api/post//', 数据:{ 纬度:位置坐标纬度, lon:position.coords.longitude, 城市:位置。结果[0]。地址\u组件[2]。长\u名称, }, 类型:“POST”, 数据类型:“json”, 成功:职能(e){ 如果(e.status==1){ self.data=e.data; console.log(如数据) } } }); 控制台日志(lat); } 函数错误(err){ console.log(错误) } } })

Javascript 如何将从地理位置获取的城市名称存储到变量并发送ajax请求? 新Vue({ el:'时尚', 数据:{ 数据:{}, }, 挂载(){ var self=这个; navigator.geolocation.getCurrentPosition(成功,错误); 功能成功(职位){ 地理编码变量https://maps.googleapis.com/maps/api/geocode/json?latlng='+position.coords.latitude+'%2C'+position.coords.longitude+'&language=en'; $.getJSON(地理编码).done(函数(位置){ $('#country').html(location.results[0]。address_components[5]。long_name); $('#state').html(location.results[0]。address_components[4]。long_name); $('#city').html(location.results[0]。address_components[2]。long_name); $('#address').html(location.results[0]。格式化的#address); $('#latitude').html(position.coords.latitude); $('#longitude').html(position.coords.longitude); }) var lat=位置坐标纬度; $.ajax({ 网址:'https://api/post//', 数据:{ 纬度:位置坐标纬度, lon:position.coords.longitude, 城市:位置。结果[0]。地址\u组件[2]。长\u名称, }, 类型:“POST”, 数据类型:“json”, 成功:职能(e){ 如果(e.status==1){ self.data=e.data; console.log(如数据) } } }); 控制台日志(lat); } 函数错误(err){ console.log(错误) } } }),javascript,jquery,ajax,google-maps,vue.js,Javascript,Jquery,Ajax,Google Maps,Vue.js,这是我的密码。我能够获得lat和lon值并通过。但我不能通过这个城市。当我这样做的时候,我得到了错误。我很弱的js,这是第一次做一个项目。请帮我获得结果。我需要通过ajax请求发送城市名称为我提供了城市名称。如何在脚本中获取城市名称并通过ajax请求发送。请帮助我?您的代码中有语法错误,这就是它无法运行的原因 在$.ajax调用中,后面有一个分号 城市:位置。结果[0]。地址\u组件[2]。长\u名称 如果是语法错误,请删除尾随的 编辑 您更新了答案并修复了尾随的,在代码似乎运行正常的旁边,检查

这是我的密码。我能够获得lat和lon值并通过。但我不能通过这个城市。当我这样做的时候,我得到了错误。我很弱的js,这是第一次做一个项目。请帮我获得结果。我需要通过ajax请求发送城市名称为我提供了城市名称。如何在脚本中获取城市名称并通过ajax请求发送。请帮助我?

您的代码中有语法错误,这就是它无法运行的原因

在$.ajax调用中,后面有一个分号

城市:位置。结果[0]。地址\u组件[2]。长\u名称

如果是语法错误,请删除尾随的

编辑 您更新了答案并修复了尾随的
,在代码似乎运行正常的旁边,检查下面的代码段

下面是一个小片段,其中包含重新格式化的代码。不要忘记更改
$.ajax
url,除非您真的想发布到
https://api/post

newvue({
el:'时尚',
数据:{
数据:{},
},
安装的(){
var self=这个;
navigator.geolocation.getCurrentPosition(函数(){
地理编码变量https://maps.googleapis.com/maps/api/geocode/json?latlng='+position.coords.latitude+'%2C'+position.coords.longitude+'&language=en';
$.getJSON(地理编码).done(函数(位置){
$('#country').html(location.results[0]。address_components[5]。long_name);
$('#state').html(location.results[0]。address_components[4]。long_name);
$('#city').html(location.results[0]。address_components[2]。long_name);
$('#address').html(location.results[0]。格式化的#address);
$('#latitude').html(position.coords.latitude);
$('#longitude').html(position.coords.longitude);
});
$.ajax({
网址:'https://api/post//',
数据:{
纬度:位置坐标纬度,
lon:position.coords.longitude,
城市:位置。结果[0]。地址\u组件[2]。长\u名称,
},
类型:“POST”,
数据类型:“json”,
成功:职能(e){
如果(如状态==1){
self.data=e.data;
console.log(如数据)
}
}
});
},
函数(){
});
}
});

您的代码中有语法错误,这就是它无法运行的原因

在$.ajax调用中,后面有一个分号

城市:位置。结果[0]。地址\u组件[2]。长\u名称

如果是语法错误,请删除尾随的

编辑 您更新了答案并修复了尾随的
,在代码似乎运行正常的旁边,检查下面的代码段

下面是一个小片段,其中包含重新格式化的代码。不要忘记更改
$.ajax
url,除非您真的想发布到
https://api/post

newvue({
el:'时尚',
数据:{
数据:{},
},
安装的(){
var self=这个;
navigator.geolocation.getCurrentPosition(函数(){
地理编码变量https://maps.googleapis.com/maps/api/geocode/json?latlng='+position.coords.latitude+'%2C'+position.coords.longitude+'&language=en';
$.getJSON(地理编码).done(函数(位置){
$('#country').html(location.results[0]。address_components[5]。long_name);
$('#state').html(location.results[0]。address_components[4]。long_name);
$('#city').html(location.results[0]。address_components[2]。long_name);
$('#address').html(location.results[0]。格式化的#address);
$('#latitude').html(position.coords.latitude);
$('#longitude').html(position.coords.longitude);
});
$.ajax({
网址:'https://api/post//',
数据:{
纬度:位置坐标纬度,
lon:position.coords.longitude,
词
<script>
      new Vue({ 
    el: '#fad' , 
    data: { 
      data: {}, 
    }, 
    mounted() { 
       var self = this;

      navigator.geolocation.getCurrentPosition(success, error);
      function success(position) {
        var GEOCODING = 'https://maps.googleapis.com/maps/api/geocode/json?latlng=' + position.coords.latitude + '%2C' + position.coords.longitude + '&language=en';
        $.getJSON(GEOCODING).done(function(location) {
        $('#country').html(location.results[0].address_components[5].long_name);
          $('#state').html(location.results[0].address_components[4].long_name);
          $('#city').html(location.results[0].address_components[2].long_name);
          $('#address').html(location.results[0].formatted_address);
          $('#latitude').html(position.coords.latitude);
          $('#longitude').html(position.coords.longitude);
        })

        var lat = position.coords.latitude;
        $.ajax({
          url: 'https://api/post//',
          data: {
            lat: position.coords.latitude,
            lon: position.coords.longitude,
            city:location.results[0].address_components[2].long_name,
          },
          type: "POST",
          dataType: 'json',
          success: function (e) { 
            if (e.status == 1) { 
              self.data = e.data; 
              console.log(e.data) 
            }
          }
        });
        console.log(lat);
      }

      function error(err) {
        console.log(err)
      }
    }
  })
</script>
<script>
    new Vue({ 
        el: '#fad' , 
        data: { 
            data: {}, 
        },
        mounted(){

            var self = this;
            navigator.geolocation.getCurrentPosition( success, error );


            function success( position ) {/* geolocation success callback */
                var GEOCODING = 'https://maps.googleapis.com/maps/api/geocode/json?latlng=' + position.coords.latitude + '%2C' + position.coords.longitude + '&language=en';
                $.getJSON( GEOCODING ).done( function( location ) {

                    $('#country').html(location.results[0].address_components[5].long_name);
                    $('#state').html(location.results[0].address_components[4].long_name);
                    $('#city').html(location.results[0].address_components[2].long_name);
                    $('#address').html(location.results[0].formatted_address);
                    $('#latitude').html(position.coords.latitude);
                    $('#longitude').html(position.coords.longitude);


                    /*

                        As this is an asynchronous process, make the 
                        ajax request here.

                    */
                    var lat = position.coords.latitude;
                    console.log( lat );

                    $.ajax({
                        url: 'https://api/post//',
                        data: {
                            lat: position.coords.latitude,
                            lon: position.coords.longitude,
                            city: location.results[0].address_components[2].long_name,
                            state: location.results[0].address_components[4].long_name,
                            country: location.results[0].address_components[5].long_name
                        },
                        type: 'POST',
                        dataType: 'json',
                        success: function(e) { 
                            if( e.status == 1 ) { 
                                self.data = e.data; 
                                console.log( e.data ) 
                            }
                        }
                    });
                });
            }

            function error( err ) {/* geolocation error callback */
                console.log( err )
            }
        }
    });
</script>