• 百度地图API 拖拽或点击地图位置获取坐标


     1     function setPlace(map,myValue,callback){
     2       function getAddress(){
     3         var pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
     4         map.centerAndZoom(pp, 18);
     5         var marker = new BMap.Marker(pp);// 创建标注
     6         map.addOverlay(marker);             // 将标注添加到地图中
     7         marker.enableDragging();     // 可拖拽
     8 
     9 
    10         marker.addEventListener('dragend', function (e) {//拖动标注结束
    11           var pointNew = e.point;
    12           console.log("使用拖拽获取的百度坐标"+pointNew.lng+","+pointNew.lat);
    13           marker.setPosition(pointNew);//设置覆盖物位置
    14           //百度转GCJ-02
    15           var x_pi = 3.14159265358979324 * 3000.0 / 180.0;
    16           var x = pointNew.lng - 0.0065;
    17           var y = pointNew.lat - 0.006;
    18           var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
    19           var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
    20           var gg_lng = z * Math.cos(theta);
    21           var gg_lat = z * Math.sin(theta);
    22           //百度GCJ02转web墨卡托84
    23           var earthRad = 6378137.0;
    24           pointNew.lng = gg_lng * Math.PI / 180 * earthRad;
    25           var a = gg_lat * Math.PI / 180;
    26           pointNew.lat = earthRad / 2 * Math.log((1.0 + Math.sin(a)) / (1.0 - Math.sin(a)));
    27           console.log("使用拖拽获取的百度坐标转换的WebMercator坐标是("+pointNew.lng+","+pointNew.lat+")");
    28           return callback(pointNew);
    29         });
    30 
    31 
    32         map.addEventListener("click", function(e){
    33           console.log('换位置百度的经度:'+e.point.lng+' , 纬度: '+e.point.lat);
    34           var now_point =  new BMap.Point(e.point.lng, e.point.lat );
    35           marker.setPosition(now_point);//设置覆盖物位置
    36           var x_pi = 3.14159265358979324 * 3000.0 / 180.0;
    37           var x = now_point.lng - 0.0065;
    38           var y = now_point.lat - 0.006;
    39           var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
    40           var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
    41           var gg_lng = z * Math.cos(theta);
    42           var gg_lat = z * Math.sin(theta);
    43           //GCJ02转web墨卡托84
    44           var earthRad = 6378137.0;
    45           now_point.lng = gg_lng * Math.PI / 180 * earthRad;
    46           var a = gg_lat * Math.PI / 180;
    47           now_point.lat = earthRad / 2 * Math.log((1.0 + Math.sin(a)) / (1.0 - Math.sin(a)));
    48           console.log("WebMercator坐标是("+now_point.lng+","+now_point.lat+")");
    49           return callback(now_point);
    50         });
    51         //百度转墨卡托
    52         console.log("使用点击换位置获取的百度坐标百度地图坐标是("+pp.lng+","+pp.lat+")");
    53         //百度转GCJ-02
    54         var x_pi = 3.14159265358979324 * 3000.0 / 180.0;
    55         var x = pp.lng - 0.0065;
    56         var y = pp.lat - 0.006;
    57         var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
    58         var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
    59         var gg_lng = z * Math.cos(theta);
    60         var gg_lat = z * Math.sin(theta);
    61         //GCJ02转web墨卡托84
    62         var earthRad = 6378137.0;
    63         pp.lng = gg_lng * Math.PI / 180 * earthRad;
    64         var a = gg_lat * Math.PI / 180;
    65         pp.lat = earthRad / 2 * Math.log((1.0 + Math.sin(a)) / (1.0 - Math.sin(a)));
    66         console.log("使用点击换位置获取的百度坐标转换后的WebMercator坐标是("+pp.lng+","+pp.lat+")");
    67         return callback(pp);
    68       }
    69 
    70       var local = new BMap.LocalSearch(map, { onSearchComplete: getAddress});
    71       local.search(myValue);
    72     }
  • 相关阅读:
    Android笔记之interface
    Android笔记之数据库操作
    Android笔记之sharedPrefenses
    Android笔记之数组的使用
    Android笔记之TableLayout布局
    Android笔记之Progress进度条
    StringBuffer调整空间
    git创建与合并分支
    git版本回退
    github设置分支push权限
  • 原文地址:https://www.cnblogs.com/yangzhengier/p/10893319.html
Copyright © 2020-2023  润新知