<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=crUGHxgrhp3DGwWPu1iFalfa"></script>
<script>
function getCookie(name) {
var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
if(arr=document.cookie.match(reg))
return unescape(arr[2]);
else
return null;
}
var loclng = getCookie('lng');
var loclat = getCookie('lat');
if(loclng == null || loclng == '0' || loclat==null || loclat == '0'){
getCurrentLocation();
}
function getCurrentLocation(){
//获取GPS坐标
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showMap, handleError, { enableHighAccuracy: true, maximumAge: 1000 });
} else {
alert("您的浏览器不支持使用HTML 5来获取地理位置服务");
}
}
function showMap(value) {
var longitude = value.coords.longitude;
var latitude = value.coords.latitude;
// alert(longitude);
// alert(latitude);
gpsPoint = new BMap.Point(longitude, latitude); // 创建点坐标
// map = new BMap.Map("map");
BMap.Convertor.translate(gpsPoint, 0, translateCallback);
}
translateCallback = function (point) {
//alert(point.lng);
//alert(point.lat);
$.ajax({
url: '/ajax_location.php?act=getCurrentLocation',
type: 'POST',
dataType: 'json',
data: point,
})
.done(function(res){
if(res.error > 0){
alert("获取当前位置失败");
}else{
var currentData = res.data;
document.cookie="lng="+currentData.lng;
document.cookie="lat="+currentData.lat;
// alert(currentData.baidu_name+currentData.baidu_address);
document.cookie="baidu_name="+escape(currentData.baidu_name);
// alert(currentData.baidu_name+currentData.baidu_address);
document.cookie="baidu_address="+escape(currentData.baidu_address);
window.location.href = "index.php";
}
})
}
function handleError(value) {
switch (value.code) {
case 1:
alert("位置服务被拒绝!!");
break;
case 2:
alert("暂时获取不到位置信息.");
break;
case 3:
alert("获取信息超时.");
break;
case 4:
alert("未知错误.");
break;
}
}
</script>
<div id="map" style="display: none"></div>
---------------------------------------------
if ($_REQUEST['act'] == 'getCurrentLocation')
{
$lng = empty($_POST['lng']) ? 0 : trim($_POST['lng']);
$lat = empty($_POST['lat']) ? 0 : trim($_POST['lat']);
if(empty($lng) || empty($lat)){
echo json_encode(array('error' => 1));
exit();
}else{
$appkey = 'wsXjAhPhgdkDyDdMozLkZsi9yR6nr9Gp';
$url = 'http://api.map.baidu.com/geocoder/v2/?';
$url .= 'ak=' . $appkey;
$url .= '&location=' . $lat . ',' . $lng;
$url .= '&output=json';
$url .= '&pois=1';
/*{"status":0,"result":{"location":{"lng":123.25787762827992,"lat":41.28326672403472},"formatted_address":"辽宁省辽阳市太子河区中华大街东段","business":"","addressComponent":{"adcode":"211011","city":"辽阳市","country":"中国","direction":"","distance":"","district":"太子河区","province":"辽宁省","street":"中华大街东段","street_number":"","country_code":0},"pois":[{"addr":"文圣区中华大街东段万家饭店西北80米","cp":" ","direction":"东南","distance":"281","name":"辽阳市众驰汽车服务有限公司","poiType":"汽车服务","point":{"x":123.25564561402071,"y":41.284164095541949},"tag":"汽车服务;汽车维修","tel":"","uid":"7903e6420265e9ccdf1e52e0","zip":""},{"addr":"辽阳市太子河区","cp":" ","direction":"西南","distance":"332","name":"辽阳源通汽车贸易有限公司","poiType":"汽车服务","point":{"x":123.25941849716262,"y":41.28519407906366},"tag":"汽车服务;汽车销售","tel":"","uid":"76cbe1ac6b95f3131ce999f6","zip":""},{"addr":"辽阳市太子河区","cp":" ","direction":"东南","distance":"553","name":"地号","poiType":"行政地标","point":{"x":123.25357951134777,"y":41.28514664597185},"tag":"行政地标;村庄","tel":"","uid":"f2f15ffb30bcaa88631c673a","zip":""},{"addr":"辽阳市太子河区","cp":" ","direction":"东","distance":"677","name":"兴农村","poiType":"行政地标","point":{"x":123.251800866438,"y":41.28353390017764},"tag":"行政地标;村庄","tel":"","uid":"5ffb1816749e675f6f47603a","zip":""},{"addr":"辽阳市太子河区","cp":" ","direction":"东北","distance":"880","name":"辽阳技师学院","poiType":"教育培训","point":{"x":123.25154934089521,"y":41.27969160867179},"tag":"教育培训;中学","tel":"","uid":"df0efb1ca27f15d5e507385e","zip":""},{"addr":"辽宁省辽阳市太子河区","cp":" ","direction":"东北","distance":"885","name":"河东新城","poiType":"房地产","point":{"x":123.2516301883911,"y":41.27954929720128},"tag":"房地产;住宅区","tel":"","uid":"915dc73b25f7aa0f88fc0622","zip":""},{"addr":"中华大街南段","cp":" ","direction":"东北","distance":"975","name":"河东新城开发建设管委会","poiType":"政府机构","point":{"x":123.25143256117892,"y":41.27879029741093},"tag":"政府机构;各级政府","tel":"","uid":"f66609596610c811b72655fc","zip":""}],"poiRegions":[],"sematic_description":"辽阳市众驰汽车服务有限公司东南281米","cityCode":351}}*/
$res = json_decode(file_get_contents($url), true);
$data['baidu_address'] = $res['result']['pois'][0]['addr'];
$data['baidu_name'] = $res['result']['pois'][0]['name'];
$data['lng'] = $res['result']['pois'][0]['point']['x'];
$data['lat'] = $res['result']['pois'][0]['point']['y'];
echo json_encode(array('error' => 0, 'data'=> $data));
exit();
}
}
-------------------------------------------------------------------------------
后台拖动定位
<script type="text/javascript" src="http://api.map.baidu.com/api?v=1.3"></script>
<script type="text/javascript">
var map = new BMap.Map("container");
map.centerAndZoom("北京", 12);
map.enableScrollWheelZoom(); //启用滚轮放大缩小,默认禁用
map.enableContinuousZoom(); //启用地图惯性拖拽,默认禁用
map.addControl(new BMap.MapTypeControl()); //调用卫星地图
map.addControl(new BMap.NavigationControl()); //添加默认缩放平移控件
map.addControl(new BMap.OverviewMapControl()); //添加默认缩略地图控件
//map.addControl(new BMap.OverviewMapControl({ isOpen: true, anchor: BMAP_ANCHOR_TOP_RIGHT})); //右上角,打开
var localSearch = new BMap.LocalSearch(map);
localSearch.enableAutoViewport(); //允许自动调节窗体大小
function searchByStationName() {
map.clearOverlays();//清空原来的标注
var keyword = document.getElementById("text_").value;
localSearch.setSearchCompleteCallback(function (searchResult) {
var poi = searchResult.getPoi(0);
//document.getElementById("result_").value = poi.point.lng + "," + poi.point.lat;
map.centerAndZoom(poi.point, 13);
var marker = new BMap.Marker(new BMap.Point(poi.point.lng, poi.point.lat)); // 创建标注,为要查询的地址对应的经纬度
//map.addOverlay(marker); var content = document.getElementById("text_").value + "<br/><br/>经度:" + poi.point.lng + "<br/>纬度:" + poi.point.lat; var infoWindow = new BMap.InfoWindow("<p style='font-size:14px;'>" + content + "</p>"); marker.addEventListener("click", function () { this.openInfoWindow(infoWindow); });
//marker.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画
map.addOverlay(marker);
marker.enableDragging(); //可拖拽
marker.addEventListener("dragend", function(e){
//document.getElementById("r-result").innerHTML = e.point.lng + ", " + e.point.lat;//打印拖动结束坐标
//document.getElementById("r-result").innerHTML = "经度:" + e.point.lng;//打印拖动结束坐标
//document.getElementById("r-result2").innerHTML = "纬度:" + e.point.lat;//打印拖动结束坐标
//document.getElementById("jingdu").value=document.getElementById("r-result").innerHTML;
//document.getElementById("weidu").value=document.getElementById("r-result2").innerHTML;
document.getElementById("lng").value= e.point.lng;
document.getElementById("lat").value= e.point.lat;
});
});
localSearch.search(keyword);
}
</script>
<div style="width:850px;margin:auto;">
请输入要查询的地址(北京):<input id="text_" type="text" value="北京" style="min-width:150px;"/>
<input type="button" value="查询" onclick="searchByStationName();"/><br>
<font color=gray>请启用滚轮放大地图然后将标注拖动到具体位置可显示其经纬度</font>
<div id="container"
style="position: absolute;
margin-left:450px;
width: 500px;
height: 400px;
top: 98px;
border: 1px solid gray;
overflow:hidden;">
</div>
</div>
<div id="r-result" style="float:left;width:100px;" ></div>
<div id="r-result2" style="float:left;width:100px;"></div><br>
<input type="text" name="lng" id='lng' />
<input type="text" name="lat" id='lat' />