<?php

// 根据经纬度换取省市等信息
require_once ("../user_include.inc");

ErrorLogger::doOutput("Compass...ajax_get_location_place.php....Start.", 0);

//获取参数
$unionId = ParamUtil::getRequestString("unionId");
$longitude = ParamUtil::getRequestString("longitude");//用户位置的经度
$latitude = ParamUtil::getRequestString("latitude");//用户位置的纬度

$result = array();

//用户所在【省市区】
//调用腾讯LGS服务解析用户地址信息
$tmpUrl = '/ws/geocoder/v1?key=' . LBS_KEY . '&location=' . $latitude . ',' . $longitude . LBS_SECRET;
$md5Str = md5($tmpUrl);
$lbsUrl = 'https://apis.map.qq.com/ws/geocoder/v1?key=' . LBS_KEY . '&location=' . $latitude . ',' . $longitude . '&sig=' . $md5Str;
$lbsObj =  file_get_contents($lbsUrl);
$jsonObj = json_decode($lbsObj); //对JSON格式的字符串进行编码
$lbsArray = get_object_vars($jsonObj);//转换成数组
if(isset($lbsArray['result'])) {
	$resultObj = $lbsArray['result'];
	//地理位置获取正常的情况下设定。 156：中国代码
	if($lbsArray['status'] == "0" && $resultObj->ad_info->nation_code==156) {
		//保存到用户当前地理位置中
		$result["province"] = $resultObj->address_component->province;
		$result["city"] = $resultObj->address_component->city;
		$result["district"] = $resultObj->address_component->district;
	}
} else {
	$result["message"] = "参数错误！";
	responseNG($result);
}

ErrorLogger::doOutput("Compass...ajax_get_location_place.php....End.", 0);

//返回结果
responseOK($result);

function responseNG($result) {
	$result = array("status"=>"NG", "result"=>$result);
	print json_encode($result);
	exit;
}
function responseOK($result) {
	$result = array("status"=>"OK", "result"=>$result);
	print json_encode($result);
	exit;
}
?>