小程序端
前端
wx.ready(function() {
wx.getLocation({
type: 'wgs84',// 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
success: function (res) {
var latitude = res.latitude; // 纬度,浮点数,范围为90 ~ -90
var longitude = res.longitude; // 经度,浮点数,范围为180 ~ -180。
$.ajax({
type:'POST',
url:"",
data:{latitude:latitude,longitude:longitude},
async:false,
success:function(res){
var res = eval('(' + res + ')');
if(res.code==1){
},
cancel: function (res) {
YDUI.dialog.toast('您已拒绝授权', 'none', 1500);
}
});
});
接口
$latitude =$_GPC['latitude'];
$longitude =$_GPC['longitude'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://api.map.baidu.com/geocoder?location={$latitude},{$longitude}&output=xml&key=5445454545454545");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //如果把这行注释掉的话,就会直接输出
$result=curl_exec($ch);
curl_close($ch);
$xml = simplexml_load_string($result);
$data = json_decode(json_encode($xml),TRUE);
// $res_data['city'] = $data['result']['addressComponent']['city'];
// $res_data['province'] = $data['result']['addressComponent']['province'];
// $res_data['district'] = $data['result']['addressComponent']['district'];
$city = $data['result']['addressComponent']['district'];