123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- var id = mobile.getUrlVars('id');
- var index;
- var map = new BMap.Map("map");
- var myGeo = new BMap.Geocoder();
- mui.init();
- mui.ready(function() {
- mui('#scroll').scroll({
- indicators: true //是否显示滚动条
- });
- if(id){
- getById();
- $('#del').removeClass('mui-hidden');
- }
- // var cityPicker = new mui.PopPicker({
- // layer: 3
- // });
- // cityPicker.setData(cityData);
- // cityPicker.pickers[0].setSelectedValue(360000);
- // setTimeout(function(){
- // cityPicker.pickers[1].setSelectedValue(361100);
- // },100);
- // $('#to_address').on('tap', function(event){
- // cityPicker.show(function(items) {
- // var arr = [];
- // arr.push('<span id="province">'+(items[0] || {}).text+'</span> ');
- // arr.push('<span id="city">'+(items[1] || {}).text+'</span> ');
- // arr.push('<span id="district">'+(items[2] || {}).text+'</span> ');
- // $('#to_address').html(arr.join(''));
- // });
- // });
- $('#del').on('tap', function() {
- mobile.ajaxPost(base + '/userAddress/delete', {id:id}, function(json) {
- if(json.success){
- layer.open({content:'删除成功',time:1});
- setTimeout(function() {
- history.back();
- }, 1000);
- }else{
- layer.open({content:'删除失败',time:3});
- }
- });
- });
-
- $("#location").on("tap","li",function(){
- $('#province').html($(this).attr("province"));
- $('#city').html($(this).attr("city"));
- $('#district').html($(this).attr("district"));
- $('#longitude').val($(this).find(".longitude").html());
- $('#latitude').val($(this).find(".latitude").html());
- $('#post_code').val($(this).find(".postCode").html());
- $('#address').val($(this).find(".address").html());
- $('#select').removeClass('mui-active');
- });
-
- $('.mui-btn-primary').on('tap',function(){
- var param = new Object();
- if(id){
- param.id=id;
- }
- param.consignee = $.trim($('#consignee').val());
- param.mobile = $.trim($('#mobile').val());
- param.province = $.trim($('#province').html());
- param.city = $.trim($('#city').html());
- param.district = $.trim($('#district').html());;
- param.address = $.trim($('#address').val());
- param.house_num = $.trim($('#house_num').val());
- param.longitude = $.trim($('#longitude').val());
- param.latitude = $.trim($('#latitude').val());
- param.post_code = $.trim($('#post_code').val());
- if(document.querySelector('#is_default').checked){
- param.is_default = '1';
- }else{
- param.is_default = '0';
- }
- if(!param.consignee){
- layer.open({content:'请输入收货人',time:3});
- $('#consignee').focus();
- return;
- }
- if(!param.mobile){
- layer.open({content:'请输入手机号码',time:3});
- $('#mobile').focus();
- return;
- }
- if(!mobile.checkMobile(param.mobile)){
- layer.open({content:'请输入正确手机号码',time:3});
- return;
- }
- if(!param.address){
- layer.open({content:'请选择地址',time:3});
- $('#address').focus();
- return;
- }
- if(!param.house_num){
- layer.open({content:'请输入门牌号码',time:3});
- $('#house_num').focus();
- return;
- }
- mobile.ajaxPost(base + '/userAddress/save', param, function(json) {
- if(json.success){
- layer.open({content:json.msg,time:1});
- setTimeout(function() {
- history.back();
- }, 1000);
- }else{
- layer.open({content:json.msg,time:3});
- }
- });
- });
-
- $(".to-address").on("tap",function(){
- initmap();
- $('#select').addClass('mui-active');
- });
- $("#select").on("tap",'.mui-icon-left-nav',function(){
- $('#select').removeClass('mui-active');
- });
-
- //搜索
- $(".mui-search").on("keyup","input[type='search']",function(){
- var key = $.trim($(this).val());
- local.search(key);
- });
- //搜索
- $("#search").on("tap",function(){
- var key = $.trim($('.mui-indexed-list-search-input').val());
- local.search(key);
- });
- var options = {
- onSearchComplete: function(results){
- $('#location').html('');
- if (local.getStatus() == BMAP_STATUS_SUCCESS){
- for (var i = 0; i < results.getCurrentNumPois(); i ++){
- (function(i){
- var place = results.getPoi(i);
- var point = new BMap.Point(place.point.lng,place.point.lat);
- myGeo.getLocation(point, function(result){
- if (result){
- var addComp = result.addressComponents;
- var html = '<li class="mui-table-view-cell" province="'+place.province+'" city="'+place.city+'" district="'+addComp.district+'">' +
- '<p class="mui-ellipsis-2 color1 fs16 mb5">'+
- '<i class="fs16 color2 mui-icon iconfont icon-shouhuodizhi"></i><span class="address">' + place.title + '</span></p>' +
- '<p>' + place.address + '</p>' +
- '<span class="longitude" style="display: none;">' + place.point.lng + '</span>' +
- '<span class="latitude" style="display: none;">' + place.point.lat + '</span>' +
- '<span class="postCode" style="display: none;"></span>' +
- '</li>';
- $('#location').append(html);
- }
- });
- })(i);
- }
- }
- }
- };
- var local = new BMap.LocalSearch(map, options);
- });
- //获取地址
- function getById() {
- mobile.ajaxPost(base + '/userAddress/getById', {id:id}, function(json) {
- if(json.data){
- $.each(json.data,function(key,value){
- if($('#'+key)){
- $('#'+key).val(mobile.nullToSpace(value));
- }
- });
- var arr = [];
- arr.push('<span id="province">'+json.data.province+'</span> ');
- arr.push('<span id="city">'+json.data.city+'</span> ');
- arr.push('<span id="district">'+mobile.nullToSpace(json.data.district)+'</span> ');
- $('#to_address').html(arr.join(''));
- if(json.data.is_default=='1'){
- document.querySelector('#is_default').checked=true;
- }
- }
- });
- }
- //初始化地图
- function initmap() {
- //index = layer.open({type: 3, shadeClose:false, content: '定位中...'});
- map.centerAndZoom(new BMap.Point(117.950, 28.463), 16);
- map.enableScrollWheelZoom();
- map.enableDragging();
-
- wx.ready(function(){
- wx.getLocation({
- type: 'wgs84', // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
- success: function (res) {
- $.ajax({
- url:'http://api.map.baidu.com/geoconv/v1/?coords='+res.longitude+','+res.latitude+'&from=1&to=5&ak=3Q4laSROftZI4chFMPVUno90',
- dataType:'jsonp',
- processData: false,
- type:'get',
- success:function(data){
- if (data.status=='0'){
- var latitude=data.result[0].y;
- var longitude=data.result[0].x;
- var point = new BMap.Point(longitude,latitude);
- map.panTo(point);
-
- function PositionControl() {
- // 默认停靠位置和偏移量
- this.defaultAnchor = BMAP_ANCHOR_TOP_LEFT;
- this.defaultOffset = new BMap.Size($(document).width()/2 - 12, $('#map').height()/2 - 26); // 距离左上角位置
- }
- // 通过JavaScript的prototype属性继承于BMap.Control
- PositionControl.prototype = new BMap.Control();
- // 自定义控件必须实现自己的initialize方法,并且将控件的DOM元素返回
- // 在本方法中创建个div元素作为控件的容器,并将其添加到地图容器中
- PositionControl.prototype.initialize = function(map) {
- // 创建一个DOM元素,24X26
- var div = document.createElement("span");
- // 设置样式
- div.className = "mui-icon iconfont icon-wodeweizhi";
- div.style.color = 'red';
- // 绑定事件
- div.onclick = function(e) {
- }
- // 添加DOM元素到地图中
- map.getContainer().appendChild(div);
- // 将DOM元素返回
- return div;
- }
- // 创建控件
- var myZoomCtrl = new PositionControl();
- // 添加到地图当中
- map.addControl(myZoomCtrl);
-
- map.addEventListener('moveend', function() {
- var pos = map.getCenter();
- getAddress(pos.lat, pos.lng);
- });
-
- getAddress(point.lat, point.lng);
- }else{
- layer.msg('坐标转换失败');
- }
- },
- error:function(XMLHttpRequest, textStatus, errorThrown) {
- layer.msg('坐标转换失败');
- }
- });
- }
- });
- });
- }
- function getAddress(baiduPointLat, baiduPointLng) {
- $('#location').html('');
-
- var url = 'http://api.map.baidu.com/geocoder/v2/?ak=3Q4laSROftZI4chFMPVUno90' +
- '&callback=renderReverse' +
- '&location=' + baiduPointLat + ',' + baiduPointLng +
- '&output=json' +
- '&pois=1';
-
- $.ajax({
- type: 'GET',
- dataType: 'jsonp',
- url: url,
- beforeSend: function() {
- //index = layer.open({type: 3, shadeClose:false, content: '加载中...'});
- },
- success: function(json) {
- layer.close(index);
- if (json == null || typeof (json) == 'undefined') {
- return;
- }
- if (json.status != '0') {
- return;
- }
-
- var result = json.result;
- var province = result.addressComponent.province;
- var city = result.addressComponent.city;
- var district = result.addressComponent.district;
-
- for (var i = 0; i < json.result.pois.length; i++) {
- var poi = json.result.pois[i];
- var html = '<li class="mui-table-view-cell" province="'+province+'" city="'+city+'" district="'+district+'">' +
- '<p class="mui-ellipsis-2 color1 fs16 mb5">'+
- '<i class="fs16 color2 mui-icon iconfont icon-shouhuodizhi"></i><span class="address">' + poi.name + '</span></p>' +
- '<p>' + poi.addr + '</p>' +
- '<span class="longitude" style="display: none;">' + poi.point.x + '</span>' +
- '<span class="latitude" style="display: none;">' + poi.point.y + '</span>' +
- '<span class="postCode" style="display: none;">' + poi.zip + '</span>' +
- '</li>';
- $('#location').append(html);
- }
- },
- error: function (XMLHttpRequest, textStatus, errorThrown) {
- layer.close(index);
- layer.open({content:'地址位置获取失败', time:3});
- $('#refreshLocation').show();
- }
- });
- }
|