百度地图获取位置

function getMap(){
$('#allmap').show();
// 百度地图API功能
var map = new BMap.Map("allmap");
var point = new BMap.Point(116.331398,39.897445);
map.centerAndZoom(point,12);
// 创建地址解析器实例
var myGeo = new BMap.Geocoder();

//var selCountries_1 = $('#selCountries_1').val();
var selCountries_1 = $("#selCountries_1").find("option:selected").text();
//console.log(selCountries_1);

//var selProvinces_1 = $('#selProvinces_1').val();
var selProvinces_1 = $("#selProvinces_1").find("option:selected").text();
//console.log(selProvinces_1);

//var selCities_1 = $('#selCities_1').val();
var selCities_1 = $("#selCities_1").find("option:selected").text();
//console.log(selCities_1);

//var selDistricts_1 = $('#selDistricts_1').val();
var selDistricts_1 = $("#selDistricts_1").find("option:selected").text();
//console.log(selDistricts_1);

var address = $('#address').val();
//console.log(address);

var lat = $("#lat").val();
var lng = $("#lng").val();
var cname ='';


if(selCountries_1 !='中国'){
cname = selCountries_1;
}
if(selProvinces_1 !='省份'){
cname = selProvinces_1;
}
if(selCities_1 !='地级市'){
cname += selCities_1;
}
if(selDistricts_1 !='市、县级市'){
cname += selDistricts_1;
}
if(address !=''){
cname += address;
}
var cityname = $('#address').val();
if(cname !=''){
cityname = cname;
}
//cityname = "北京市朝阳区朗庭大厦A座";
//console.log(cityname);
//if(lat != "0.00000000000000000" && lng != "0.00000000000000000"){
if(lat != "0.000000" && lng != "0.000000"){
map.clearOverlays(); 
var new_point = new BMap.Point(lng,lat);
var marker = new BMap.Marker(new_point);  // 创建标注
map.addOverlay(marker);              // 将标注添加到地图中
map.panTo(new_point);      
}
myGeo.getPoint(cityname, function(point){
if (point) {
map.centerAndZoom(point, 16);
marker =new BMap.Marker(point);
//if(lat!= "0.00000000000000000" && lng!= "0.00000000000000000"){
if(lat!= "0.000000" && lng!= "0.000000"){
map.clearOverlays(); 
var new_point = new BMap.Point(lng,lat);
marker =new BMap.Marker(point);
var marker = new BMap.Marker(new_point);  // 创建标注
map.addOverlay(marker);              // 将标注添加到地图中
map.panTo(new_point);      
}else{
map.addOverlay(marker);
}
marker.enableDragging();
map.enableScrollWheelZoom();//启用地图滚轮放大缩小
        map.enableDoubleClickZoom();//启用鼠标双击放大,默认启用(可不写)
        map.enableKeyboard();//启用键盘上下左右键移动地图
//标注拖拽后的位置
        marker.addEventListener("dragend", function (e) {
        if(confirm("是否使用此位置作为商家的定位?")){
                $('#lng').val(e.point.lng);
        $('#lat').val(e.point.lat);
$('#allmap').hide();
              }
        });
}else{
alert("您选择地址没有解析到结果!");
}
}, "北京市");
}
阅读更多
个人分类: 后台
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭