ecshop手机端订单城市插件(基于gps定位)
2014-11-22 14:30
134 查看
以前曾看到有人说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文件中。
第二步,两种手机端都是一样的操作,先找到代码:$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;
}
如此,便大功告成了!
这里要先说明一下,如果手机端是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文件中。
第二步,两种手机端都是一样的操作,先找到代码:$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;
}
如此,便大功告成了!
相关文章推荐
- 基于jquery的城市选择插件
- Atitit. 订单管理 收银单持久化 功能设计 基于ecshop订单结构
- 2015 ecshop 微信支付插件v3版 含【PC端微信扫码支付+手机端微信支付】
- Atitit. 订单管理 收银单持久化 功能设计 基于ecshop订单结构
- Atitit. 订单管理 收银单持久化 功能设计 基于ecshop订单结构
- 微信小程序商城 - 基于ecshop插件接口文件
- 2015 ecshop 微信支付插件v3版 含【PC端微信扫码支付+手机端微信支付】
- 手机端 基于amazeui 日期插件 个人觉得还不错
- 基于IC卡技术的城市信息平台的建设
- 基于eclipse的ftp插件安装
- 基于.net框架下的插件系统的实现方法
- 基于GPS定位的嵌入式汽车监控器设计
- 发布基于Eclipse的数据库建模及ORM框架代码自动生成插件(支持Hibernate,IBatis等),100K行代码
- 基于需求驱动和OL-System的大规模城市建模
- 基于AJAX技术的WebGis系统实践 (3. 三维城市地图系统实体建设)
- 基于.net框架下的插件系统的实现方法(补充)
- 现在负责的项目,基于插件框架的Framework.Libra
- 基于Visual Studio 2003/2005的Office插件开发FAQ
- 基于插件的权限系统构想
- 基于eclipse的源代码搜索插件正式发布