16 | * 百度地图坐标和火星坐标转换 17 | */ 18 | public class CoodinateCovertor { 19 | 20 | 21 | private static double x_pi = 3.14159265358979324 * 3000.0 / 180.0; 22 | 23 | /** 24 | * 对double类型数据保留小数点后多少位 25 | * 高德地图转码返回的就是 小数点后6位,为了统一封装一下 26 | * 27 | * @param digit 位数 28 | * @param in 输入 29 | * @return 保留小数位后的数 30 | */ 31 | static double dataDigit(int digit, double in) { 32 | return new BigDecimal(in).setScale(6, BigDecimal.ROUND_HALF_UP).doubleValue(); 33 | 34 | } 35 | 36 | /** 37 | * 将火星坐标转变成百度坐标 38 | * 39 | * @param lngLat_gd 火星坐标(高德、腾讯地图坐标等) 40 | * @return 百度坐标 41 | */ 42 | 43 | public static LngLat bd_encrypt(LngLat lngLat_gd) { 44 | double x = lngLat_gd.getLongitude(), y = lngLat_gd.getLantitude(); 45 | double z = sqrt(x * x + y * y) + 0.00002 * sin(y * x_pi); 46 | double theta = atan2(y, x) + 0.000003 * cos(x * x_pi); 47 | return new LngLat(dataDigit(6, z * cos(theta) + 0.0065), dataDigit(6, z * sin(theta) + 0.006)); 48 | 49 | } 50 | 51 | /** 52 | * 将百度坐标转变成火星坐标 53 | * 54 | * @param lngLat_bd 百度坐标(百度地图坐标) 55 | * @return 火星坐标(高德、腾讯地图等) 56 | */ 57 | static LngLat bd_decrypt(LngLat lngLat_bd) { 58 | double x = lngLat_bd.getLongitude() - 0.0065, y = lngLat_bd.getLantitude() - 0.006; 59 | double z = sqrt(x * x + y * y) - 0.00002 * sin(y * x_pi); 60 | double theta = atan2(y, x) - 0.000003 * cos(x * x_pi); 61 | return new LngLat(dataDigit(6, z * cos(theta)), dataDigit(6, z * sin(theta))); 62 | 63 | } 64 | 65 | //测试代码 66 | public static void main(String[] args) { 67 | LngLat lngLat_bd = new LngLat(120.153192, 30.25897); 68 | System.out.println(bd_decrypt(lngLat_bd)); 69 | } 70 | } -------------------------------------------------------------------------------- /openmap/src/main/java/com/sinfeeloo/openmap/LngLat.java: -------------------------------------------------------------------------------- 1 | package com.sinfeeloo.openmap; 2 | 3 | /** 4 | * LngLat 5 | * 6 | * @author: mhj 7 | * @date: 2018/4/3 15:28 8 | *
9 | * 经纬度点封装
10 | */
11 | public class LngLat {
12 | private double longitude;//经度
13 | private double lantitude;//维度
14 |
15 | public LngLat() {
16 | }
17 |
18 | public LngLat(double longitude, double lantitude) {
19 | this.longitude = longitude;
20 | this.lantitude = lantitude;
21 | }
22 |
23 | public double getLongitude() {
24 | return longitude;
25 | }
26 |
27 | public void setLongitude(double longitude) {
28 | this.longitude = longitude;
29 | }
30 |
31 | public double getLantitude() {
32 | return lantitude;
33 | }
34 |
35 | public void setLantitude(double lantitude) {
36 | this.lantitude = lantitude;
37 | }
38 |
39 | @Override
40 | public String toString() {
41 | return "LngLat{" +
42 | "longitude=" + longitude +
43 | ", lantitude=" + lantitude +
44 | '}';
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/openmap/src/main/java/com/sinfeeloo/openmap/OpenMapUtil.java:
--------------------------------------------------------------------------------
1 | package com.sinfeeloo.openmap;
2 |
3 | import android.app.Activity;
4 | import android.content.Intent;
5 | import android.graphics.drawable.ColorDrawable;
6 | import android.net.Uri;
7 | import android.util.Log;
8 | import android.view.Gravity;
9 | import android.view.View;
10 | import android.view.ViewGroup;
11 | import android.view.WindowManager;
12 | import android.widget.Button;
13 | import android.widget.PopupWindow;
14 |
15 | import java.net.URISyntaxException;
16 |
17 | /**
18 | * @author: mhj
19 | * @date: 2018/4/4 11:12
20 | * @desc:
21 | */
22 |
23 | public class OpenMapUtil {
24 |
25 |
26 | //打开外部地图
27 | public static void openMapPopupWindow(final Activity activity, View root, final String title, final double latitude, final double longitude) {
28 | View view = View.inflate(activity, R.layout.popu_open_map, null);
29 | final Button btn_openBaidu = view.findViewById(R.id.btn_open_baidu);
30 | Button btn_openGaode = view.findViewById(R.id.btn_open_gaode);
31 | Button btn_openTencent = view.findViewById(R.id.btn_open_tencent);
32 | Button btn_cancel = view.findViewById(R.id.btn_open_cancel);
33 |
34 |
35 | final PopupWindow mPopup = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
36 | mPopup.setFocusable(true);
37 | mPopup.setOutsideTouchable(true);
38 | mPopup.setBackgroundDrawable(new ColorDrawable());
39 | mPopup.setAnimationStyle(R.style.popupwindow_style);
40 |
41 | btn_openBaidu.setOnClickListener(new View.OnClickListener() {
42 | @Override
43 | public void onClick(View v) {
44 | openBaidu(activity, title, latitude, longitude);
45 | mPopup.dismiss();
46 | }
47 | });
48 |
49 | btn_openGaode.setOnClickListener(new View.OnClickListener() {
50 | @Override
51 | public void onClick(View v) {
52 | openGaode(activity, title, latitude, longitude);
53 | mPopup.dismiss();
54 | }
55 | });
56 |
57 | btn_openTencent.setOnClickListener(new View.OnClickListener() {
58 | @Override
59 | public void onClick(View v) {
60 | openTencent(activity, title, latitude, longitude);
61 | mPopup.dismiss();
62 | }
63 | });
64 |
65 | btn_cancel.setOnClickListener(new View.OnClickListener() {
66 | @Override
67 | public void onClick(View v) {
68 | mPopup.dismiss();
69 | }
70 | });
71 |
72 | mPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
73 | @Override
74 | public void onDismiss() {
75 | changeActivityBg(activity, 1f);
76 | }
77 | });
78 |
79 | mPopup.showAtLocation(root, Gravity.BOTTOM, 0, 0);
80 |
81 | changeActivityBg(activity, 0.7f);
82 | }
83 |
84 |
85 | /**
86 | * 打开腾讯地图
87 | * com.tencent.map
88 | *
89 | * @param latitude
90 | * @param longitude
91 | */
92 | private static void openTencent(Activity activity, String title, double latitude, double longitude) {
93 | Intent intent = new Intent();
94 | intent.setAction(Intent.ACTION_VIEW);
95 | intent.addCategory(Intent.CATEGORY_DEFAULT);
96 |
97 | //将功能Scheme以URI的方式传入data
98 | Uri uri = Uri.parse("qqmap://map/routeplan?type=drive&to=" + title + "&tocoord=" + latitude + "," + longitude);
99 | intent.setData(uri);
100 | if (intent.resolveActivity(activity.getPackageManager()) != null) {
101 | //启动该页面即可
102 | activity.startActivity(intent);
103 | } else {
104 | ToastUtils.showToast("您尚未安装腾讯地图");
105 | Uri uri2 = Uri.parse("market://details?id=com.tencent.map");
106 | Intent intent2 = new Intent(Intent.ACTION_VIEW, uri2);
107 | if (intent2.resolveActivity(activity.getPackageManager()) != null) {
108 | activity.startActivity(intent2);
109 | }
110 | }
111 | }
112 |
113 | /**
114 | * 打开高德
115 | *
116 | * @param latitude
117 | * @param longitude
118 | */
119 | private static void openGaode(Activity activity, String title, double latitude, double longitude) {
120 | if (AppUtils.isAvilible("com.autonavi.minimap")) {
121 | Intent intent = new Intent();
122 | intent.setAction(Intent.ACTION_VIEW);
123 | intent.addCategory(Intent.CATEGORY_DEFAULT);
124 |
125 | //将功能Scheme以URI的方式传入data
126 | Uri uri = Uri.parse("androidamap://route/plan/?dlat=" + latitude + "&dlon=" + longitude + "&dname=" + title + "&dev=0&t=0");
127 | intent.setData(uri);
128 |
129 | //启动该页面即可
130 | activity.startActivity(intent);
131 | } else {
132 | ToastUtils.showToast("您尚未安装高德地图");
133 | Uri uri = Uri.parse("market://details?id=com.autonavi.minimap");
134 | Intent intent = new Intent(Intent.ACTION_VIEW, uri);
135 | if (intent.resolveActivity(activity.getPackageManager()) != null) {
136 | activity.startActivity(intent);
137 | }
138 | }
139 | }
140 |
141 | /**
142 | * 打开百度地图
143 | *
144 | * @param latitude
145 | * @param longitude
146 | */
147 | private static void openBaidu(Activity activity, String title, double latitude, double longitude) {
148 | LngLat baiduLngLat = CoodinateCovertor.bd_encrypt(new LngLat(longitude, latitude));//将高德地图转换为百度坐标
149 | if (AppUtils.isAvilible("com.baidu.BaiduMap")) {//传入指定应用包名
150 | try {
151 |
152 | Intent intent = Intent.getIntent("intent://map/direction?" +
153 | "destination=latlng:" + baiduLngLat.getLantitude() + "," + baiduLngLat.getLongitude() + "|name:" + title + //终点
154 | "&mode=driving&" + //导航路线方式
155 | "&src=distribution#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end");
156 | activity.startActivity(intent); //启动调用
157 | } catch (URISyntaxException e) {
158 | Log.e("intent", e.getMessage());
159 | }
160 | } else {//未安装
161 | //market为路径,id为包名
162 | //显示手机上所有的market商店
163 | ToastUtils.showToast("您尚未安装百度地图");
164 | Uri uri = Uri.parse("market://details?id=com.baidu.BaiduMap");
165 | Intent intent = new Intent(Intent.ACTION_VIEW, uri);
166 | if (intent.resolveActivity(activity.getPackageManager()) != null) {
167 | activity.startActivity(intent);
168 | }
169 | }
170 | }
171 |
172 |
173 | //改变activity背景透明度
174 | private static void changeActivityBg(Activity activity, float f) {
175 | WindowManager.LayoutParams params = activity.getWindow().getAttributes();
176 | params.alpha = f;// 值越小透明度越暗
177 | activity.getWindow().setAttributes(params);
178 | }
179 | }
180 |
--------------------------------------------------------------------------------
/openmap/src/main/java/com/sinfeeloo/openmap/ToastUtils.java:
--------------------------------------------------------------------------------
1 | package com.sinfeeloo.openmap;
2 |
3 | import android.widget.Toast;
4 |
5 | /**
6 | * toast封装
7 | * Created by Administrator icon_btn_on 2016/9/5.
8 | */
9 | public class ToastUtils {
10 |
11 | private ToastUtils() {
12 | /* cannot be instantiated */
13 | throw new UnsupportedOperationException("cannot be instantiated");
14 | }
15 |
16 | public static boolean isShow = true;
17 |
18 |
19 | private static Toast toast;
20 |
21 | /**
22 | * 显示单例的吐司,能连续快速弹的吐司
23 | *
24 | * @param text
25 | */
26 | public static void showToast(String text) {
27 | if (isShow) {
28 | if (toast == null) {
29 | toast = Toast.makeText(App.instance(), text, Toast.LENGTH_SHORT);
30 | }
31 | toast.setText(text);
32 | toast.show();
33 | }
34 | }
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/openmap/src/main/res/anim/hide_popupwindow.xml:
--------------------------------------------------------------------------------
1 |
2 |