垃圾堆中的精品

垃圾堆中的精品

码科更新腾讯地图接口

码科在后台选择定位时使用的是腾讯地图的JavascriptAPI v2,但接口已被腾讯腾讯弃用,故更新为WebServiceAPI

修改文件make_speed/core/public/assets/js/backend/carhailing/pickup_point.js

//调用地址解析类
geocoder = {
	getAddress: (latLng, funcSuc, funcErr) => {
		$.ajax({
			url:'busines.business/getaddress?type=0',
			data: typeof latLng == 'object' ? JSON.parse(JSON.stringify(latLng)) : { lat:latLng.split(',')[0], lng:latLng.split(',')[1] },
			dataType: "json",
			success: res => {
				$.isFunction(funcSuc) && funcSuc(res)
			},error: err => {
				$.isFunction(funcErr) && funcErr(err)
			}
	   })
	},
	getLocation: (addr, funcSuc, funcErr) => {
		$.ajax({
			url:'busines.business/getaddress?type=1',
			data:{
			   address : addr
			},
			success:function(res){
				$.isFunction(funcSuc) && funcSuc(res)
			},error:function(err){
				$.isFunction(funcErr) && funcErr(err)
			}
	   })
	},
	callFunc: result => {
		marker.setMap(null);
		result.data.location = new qq.maps.LatLng(result.data.location.lat,result.data.location.lng)
		map.setCenter(result.data.location);
		marker = new qq.maps.Marker({
			icon: icon,
			shadow: shadow,
			map:map,
			position: result.data.location
		});
		if(result.data.address_components){
			var street = result.data.address_components.street == 'undefined' ? '' :result.data.address_components.street;
			var streetNumber = typeof(result.data.address_components.streetNumber) == 'undefined' ?  '' :result.data.address_components.streetNumber;
		}else{
			var street = result.data.address_component.street == 'undefined' ? '' :result.data.address_component.street;
			var streetNumber = typeof(result.data.address_component.streetNumber) == 'undefined' ?  '' :result.data.address_component.streetNumber;
		}
		
		$('input[name="search-text"]').val(result.data.address);
		//赋值表单
		$('input[name="row[latitude]"]').val(result.data.location.lat);
		$('input[name="row[longitude]"]').val(result.data.location.lng);
		$('input[name="row[address]').val(street+streetNumber);

		$('#search-btn').text('搜索地址');
	}
}

qq.maps.event.addListener(
	map,
	'click',
	function(event) {
		marker.setPosition(event.latLng);
		$('#search-btn').text('搜索中..');
		geocoder.getAddress(event.latLng, result=>{
			geocoder.callFunc(result)
		}, result => {
			alert('搜索失败!请输入准确地址');
			$('#search-btn').text('搜索地址');
		});
	}
);

//去掉腾讯地图logo等小部件
$("#map-container").bind("DOMNodeInserted",function(e){
	var length = $("#map-container div:first-child").children('div').length;
	$("#map-container > div > div").not(":first").remove();
});

//搜索地址
$('#search-btn').click(function(){
	$('#search-btn').text('搜索中..');
	var addre = $('input[name="search-text"]').val();
	geocoder.getLocation(addre, result=>{
		geocoder.callFunc(result)
	})
})

然后就可以使用了,使用的是后台已有的接口。但该接口的key值是写死的,如果要更换成后台设置的腾讯地图key,则需要修改make_speed/core/application/admin/controller/busines/Business.php

public function getaddress()
{
	$lat = !empty($_REQUEST['lat']) ? floatval($_REQUEST['lat']) : 0;
	$lng = !empty($_REQUEST['lng']) ? floatval($_REQUEST['lng']) : 0;
	$type = !empty($_REQUEST['type']) ? intval($_REQUEST['type']) : 0;
	$address = !empty($_REQUEST['address']) ? trim($_REQUEST['address']) : '';
	$url = "https://apis.map.qq.com/ws/geocoder/v1/?";
	//$tencenkey = 'Y4SBZ-XG76W-EMORO-RFJ6H-J2WNV-ONBKS';
	$tencenkey = Db::name('setting')->where(array('uniacid' => $GLOBALS['uniacid'], 'key' => 'rider_tencent_key'))->value('value');
	if (!empty($type) && !empty($address)) {
		$url .= "key=" . $tencenkey . "&address=" . $address;
		$html = file_get_contents($url);
		$results = json_decode($html, true);
		if (!empty($results['status']) || empty($results['result'])) {
			$this->error('地图响应失败,请稍候重试' . $results['status']);
		}
		$this->success('', '', $results['result']);
	}
	$param = array('location' => "{$lat},{$lng}", 'get_poi' => 0, 'key' => $tencenkey);
	$url .= http_build_query($param);
	$html = file_get_contents($url);
	$results = json_decode($html, true);
	
	if (!empty($results['status']) || empty($results['result'])) {
		$this->error('地图响应失败,请稍候重试' . $results['status']);
	}
	$this->success('', '', $results['result']);
}

评论回复

应监管要求,暂停评论,如有需要联系QQ505097558。

回到顶部