ZFSYS

通过google接口获取经纬度和地址

198
    $('.get_location').on('click',function(){
        findLatLonFromIP().then(
        function(value) {
            var lat = value.lat
            var lng = value.lng
            console.log(lat)
            console.log(lng)
            getCountryCodeFromLatLng(lat,lng)
        })
    })
function findLatLonFromIP() {
    return new Promise((resolve, reject) => {
      $.ajax({
        url: `https://www.googleapis.com/geolocation/v1/geolocate?key=AIzaSyD********nDlXF60Ro`,
        type: 'POST',
        data: JSON.stringify({considerIp: true}),
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: (data) => {
          if (data && data.location) {
            resolve({lat: data.location.lat, lng: data.location.lng});
          } else {
            reject('No location object in geolocate API response.');
          }
        },
        error: (err) => {
          reject(err);
        },
      });
    });
  }

function getCountryCodeFromLatLng(lat, lng) {     return new Promise((resolve, reject) => {       $.ajax({         url: https://maps.googleapis.com/maps/api/geocode/json?latlng=${lat},${lng}&key=AIzaSyD********nDlXF60Ro,         type: 'GET',         data: JSON.stringify({considerIp: true}),         dataType: 'json',         success: (data) => {           console.log('reverse geocode:', data.results[0].address_components);           data.results.some((address) => {             address.address_components.some((component) => {               if (component.types.includes('country')) {                 return resolve(component.short_name);               }             });           });           reject('Country not found in location information.');         },         error: (err) => {           reject(err);         },       });     }); }


  • 没有任何评论
最新帖子
[站长推荐] 一些站长/创业者使用的工具推荐 590
HTML+JS 全选与取消全选功能 544
子枫内容系统更新记录 448
有没有人做百度小程序啊 446
PHP 按照指定数量切割字符串 391
php composer更换国内源 374
a链接一键安装ios应用 369
composer 报错SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify 346
layui table 的宽度超出自动隐藏 342
怎么克服拖延症呀 338
最近热帖
[站长推荐] 一些站长/创业者使用的工具推荐 590
HTML+JS 全选与取消全选功能 544
子枫内容系统更新记录 448
有没有人做百度小程序啊 446
PHP 按照指定数量切割字符串 391
php composer更换国内源 374
a链接一键安装ios应用 369
composer 报错SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify 346
layui table 的宽度超出自动隐藏 342
怎么克服拖延症呀 338
近期热议
开始使用 160