国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > 建站經驗 > 正文

基于gps定位給ecshop手機端增加自動城市插件

2024-04-25 20:38:57
字體:
來源:轉載
供稿:網友

以前曾看到有人說php能獲取手機的ip,可是,在沒有用WiFi并且手機是3G或者4G的卡時,根本無法獲取手機的ip,所以在看到許多手機app都在用gps定位后,最模板也去研究了一下,經測試,確實可以獲取所在城市。下面,最模板來總結一下最模板的實現方法:

這里要先說明一下,如果手機端是ecshop自帶的,那么html代碼就寫在/mobile/templates/order.dwt中,php代碼寫在 /mobile/order.php中,如果是獨立的手機端,那么html代碼就寫在/mobile/themes/default/flow.dwt 中,php代碼寫在/mobile/flow.php中。

第一步,如果是ecshop自帶的手機端,在/mobile/templates/order.dwt中,寫入如下js代碼:

<script>

onload = function() {

var position_option = {

enableHighAccuracy: true,

maximumAge: 30000,

timeout: 20000

};

navigator.geolocation.getCurrentPosition(getPositionSuccess, getPositionError, position_option);

}

function getPositionSuccess( position ){

var lat = position.coords.latitude;

var lng = position.coords.longitude;

document.getElementById("latitude").value=lat;

document.getElementById("longitude").value=lng;

}

function getPositionError(error) {

switch (error.code) {

case error.TIMEOUT:

alert("連接超時,請重試");

break;

case error.PERMISSION_DENIED:

alert("您拒絕了使用位置共享服務,查詢已取消");

break;

case error.POSITION_UNAVAILABLE:

alert("獲取位置信息失敗");

break;

}

}

</script>

并找到<input class="ddbtn" type="submit" name="Submit" value="提交訂單" />,在前面寫入代碼:

<input type="hidden" id="latitude" name="latitude" value="" />

<input type="hidden" id="longitude" name="longitude" value="" />

如果是獨立的手機端,找到onload = function() ;在方法內寫入代碼:

enableHighAccuracy: true,

maximumAge: 30000,

timeout: 20000

};

navigator.geolocation.getCurrentPosition(getPositionSuccess, getPositionError, position_option);

在方法外面寫入代碼:

function getPositionSuccess( position ){

var lat = position.coords.latitude;

var lng = position.coords.longitude;

document.getElementById("latitude").value=lat;

document.getElementById("longitude").value=lng;

}

function getPositionError(error) {

switch (error.code) {

case error.TIMEOUT:

alert("連接超時,請重試");

break;

case error.PERMISSION_DENIED:

alert("您拒絕了使用位置共享服務,查詢已取消");

break;

case error.POSITION_UNAVAILABLE:

alert("獲取位置信息失敗");

break;

}

}

并找到代碼:<input type="submit" name="submit" value="提交訂單" class="c-btn3"/>,在前面寫入代碼:

<input type="hidden" id="latitude" name="latitude" value="" />

<input type="hidden" id="longitude" name="longitude" value="" />

這樣手機所在位置的經緯度就傳到了相應的php文件中。

第二步,兩種手機wap網站端都是一樣的操作,先找到代碼:$GLOBALS['db']->autoExecute($GLOBALS['ecs']->table('order_info'), $order, 'INSERT');

在前面寫入代碼:

$wei=$_POST['latitude'];

$jing=$_POST['longitude'];

$local_city=getRealyAddress($wei,$jing);

$order['order_city']=$local_city;

并在文件末尾寫入代碼:

function getRealyAddress($wei,$jing){

$place_url='http://api.map.baidu.com/geocoder?output=json&location='.$wei.','.$jing.'&key=ccea36ece20a7a6eb0666bc726957e85';

$json_place=file_get_contents($place_url);

$place_arr=json_decode($json_place,true);

$address=$place_arr['result']['addressComponent']['city'];

return $address;

}

如此,便大功告成了!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 容城县| 运城市| 珠海市| 精河县| 康保县| 亚东县| 阜南县| 留坝县| 公安县| 萝北县| 兴城市| 寿阳县| 东丰县| 孟村| 康保县| 民勤县| 德令哈市| 汉源县| 沂源县| 天峨县| 新昌县| 祁东县| 高平市| 崇仁县| 旬阳县| 旬邑县| 嘉定区| 资源县| 江安县| 修武县| 武汉市| 永修县| 海兴县| 沛县| 维西| 鹤峰县| 酒泉市| 于田县| 游戏| 芷江| 白河县|