高德地图绘制标记点,点击弹出弹框进入第三方地图软件
程序员文章站
2022-05-03 23:42:25
需求:根据经纬度绘制标记点,点击标记点弹出弹框和底部按钮,点击顶部弹框进入二级界面,点击底部按钮弹出第三方地图软件选择页,实现跨进程跳转。项目是公司项目,只放出重要部分代码。绘制标记点:final Marker marker1 = aMap.addMarker(new MarkerOptions().position(latLng2).title(“地址标题111111”).snippet(“地址内容1111”));final Marker marker2 = aMap.addMarker(n...
需求:根据经纬度绘制标记点,点击标记点弹出弹框和底部按钮,点击顶部弹框进入二级界面,点击底部按钮弹出第三方地图软件选择页,实现跨进程跳转。
效果图:
项目是公司项目,只放出重要部分代码。
final Marker marker1 = aMap.addMarker(new MarkerOptions()
.position(latLng2)
.title("地址标题111111")
.snippet("地址内容1111"));
final Marker marker2 = aMap.addMarker(new MarkerOptions()
.position(latLng3)
.title("地址标题22222")
.snippet("地址内容2222"));
绘制标记点
//标记点击事件
AMap.OnMarkerClickListener markerClickListener = new AMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
position = marker.getPosition();
title = marker.getTitle();
marker.showInfoWindow();
nav.setVisibility(View.VISIBLE);
//导航按钮点击
nav.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showAnimation();
}
});
return true;
}
};
点击标记后获取到当前标记的经纬度和名称,显示底部按钮,点击按钮弹出第三方地图选择页。
//标记上部弹框点击
AMap.OnInfoWindowClickListener listener = new AMap.OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker arg0) {
Toast.makeText(FangchanMapActivity.this, "点击了弹框1", Toast.LENGTH_SHORT).show();
}
};
点击标记顶部弹框进入二级界面,需要什么值就取什么值,后台还在出接口,我这边没有做操作
//地图点击
AMap.OnMapClickListener mapClickListener = new AMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
marker1.hideInfoWindow();
marker2.hideInfoWindow();
nav.setVisibility(View.GONE);
}
};
这里只做了显示隐藏的操作
//设置图标
marker1.setIcon(BitmapDescriptorFactory.fromResource(R.mipmap.marker_icon));
//绑定点击事件
aMap.setOnInfoWindowClickListener(listener);
aMap.setOnMapClickListener(mapClickListener);
aMap.setOnMarkerClickListener(markerClickListener);
千万千万千万记得绑定点击事件
private void showAnimation() {
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View vPopupWindow = inflater.inflate(R.layout.layout_popupwindow, null, false);//引入弹窗布局
popupWindow = new PopupWindow(vPopupWindow, ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT, true);
TextView gaode = vPopupWindow.findViewById(R.id.gaode);
TextView baidu = vPopupWindow.findViewById(R.id.baidu);
TextView tengxun = vPopupWindow.findViewById(R.id.tengxun);
TextView clean_text = vPopupWindow.findViewById(R.id.clean_text);
gaode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (MapUtil.isGdMapInstalled()) {
MapUtil.openGaoDeNavi(FangchanMapActivity.this, 0, 0, null, position.latitude, position.longitude, title);
} else {
//这里必须要写逻辑,不然如果手机没安装该应用,程序会闪退,这里可以实现下载安装该地图应用
Toast.makeText(FangchanMapActivity.this, "尚未安装高德地图", Toast.LENGTH_SHORT).show();
}
}
});
baidu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (MapUtil.isBaiduMapInstalled()){
MapUtil.openBaiDuNavi(FangchanMapActivity.this, 0, 0, null, position.latitude, position.longitude, title);
} else {
Toast.makeText(FangchanMapActivity.this, "尚未安装百度地图", Toast.LENGTH_SHORT).show();
}
}
});
tengxun.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (MapUtil.isTencentMapInstalled()){
MapUtil.openTencentMap(FangchanMapActivity.this, 0, 0, null, position.latitude, position.longitude, title);
} else {
Toast.makeText(FangchanMapActivity.this, "尚未安装腾讯地图", Toast.LENGTH_SHORT).show();
}
}
});
clean_text.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
popupWindow.dismiss();
}
});
//设置背景透明
addBackground();
//设置进出动画
popupWindow.setAnimationStyle(R.style.PopupWindowAnimation);
//引入依附的布局
View parentView = LayoutInflater.from(FangchanMapActivity.this).inflate(R.layout.layout_popupwindow, null);
//相对于父控件的位置(例如正*Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移
popupWindow.showAtLocation(parentView, Gravity.BOTTOM, 0, 0);
}
private void addBackground() {
// 设置背景颜色变暗
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = 0.7f;//调节透明度
getWindow().setAttributes(lp);
//dismiss时恢复原样
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = 1f;
getWindow().setAttributes(lp);
}
});
}
底部弹框,popupwindow实现
public class MapUtil {
public static final String PN_GAODE_MAP = "com.autonavi.minimap";// 高德地图包名
public static final String PN_BAIDU_MAP = "com.baidu.BaiduMap"; // 百度地图包名
public static final String PN_TENCENT_MAP = "com.tencent.map"; // 腾讯地图包名
/**
* 检查地图应用是否安装
* @return
*/
public static boolean isGdMapInstalled(){
return isInstallPackage(PN_GAODE_MAP);
}
public static boolean isBaiduMapInstalled(){
return isInstallPackage(PN_BAIDU_MAP);
}
public static boolean isTencentMapInstalled(){
return isInstallPackage(PN_TENCENT_MAP);
}
private static boolean isInstallPackage(String packageName) {
return new File("/data/data/" + packageName).exists();
}
/**
* 百度转高德
* @param bd_lat
* @param bd_lon
* @return
*/
public static double[] bdToGaoDe(double bd_lat, double bd_lon) {
double[] gd_lat_lon = new double[2];
double PI = 3.14159265358979324 * 3000.0 / 180.0;
double x = bd_lon - 0.0065, y = bd_lat - 0.006;
double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * PI);
double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * PI);
gd_lat_lon[0] = z * Math.cos(theta);
gd_lat_lon[1] = z * Math.sin(theta);
return gd_lat_lon;
}
/**
* 高德、腾讯转百度
* @param gd_lon
* @param gd_lat
* @return
*/
private static double[] gaoDeToBaidu(double gd_lon, double gd_lat) {
double[] bd_lat_lon = new double[2];
double PI = 3.14159265358979324 * 3000.0 / 180.0;
double x = gd_lon, y = gd_lat;
double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * PI);
double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * PI);
bd_lat_lon[0] = z * Math.cos(theta) + 0.0065;
bd_lat_lon[1] = z * Math.sin(theta) + 0.006;
return bd_lat_lon;
}
/**
* 打开高德地图导航功能
* @param context
* @param slat 起点纬度
* @param slon 起点经度
* @param sname 起点名称 可不填(0,0,null)
* @param dlat 终点纬度
* @param dlon 终点经度
* @param dname 终点名称 必填
*/
public static void openGaoDeNavi(Context context,double slat, double slon, String sname, double dlat, double dlon, String dname){
String uriString = null;
StringBuilder builder = new StringBuilder("amapuri://route/plan?sourceApplication=maxuslife");
if (slat != 0) {
builder.append("&sname=").append(sname)
.append("&slat=").append(slat)
.append("&slon=").append(slon);
}
builder.append("&dlat=").append(dlat)
.append("&dlon=").append(dlon)
.append("&dname=").append(dname)
.append("&dev=0")
.append("&t=0");
uriString = builder.toString();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setPackage(PN_GAODE_MAP);
intent.setData(Uri.parse(uriString));
context.startActivity(intent);
}
/**
* 打开腾讯地图
* params 参考http://lbs.qq.com/uri_v1/guide-route.html
*
* @param context
* @param slat 起点纬度
* @param slon 起点经度
* @param sname 起点名称 可不填(0,0,null)
* @param dlat 终点纬度
* @param dlon 终点经度
* @param dname 终点名称 必填
* 驾车:type=drive,policy有以下取值
0:较快捷
1:无高速
2:距离
policy的取值缺省为0
* &from=" + dqAddress + "&fromcoord=" + dqLatitude + "," + dqLongitude + "
*/
public static void openTencentMap(Context context, double slat, double slon, String sname, double dlat, double dlon, String dname) {
String uriString = null;
StringBuilder builder = new StringBuilder("qqmap://map/routeplan?type=drive&policy=0&referer=zhongshuo");
if (slat != 0) {
builder.append("&from=").append(sname)
.append("&fromcoord=").append(slat)
.append(",")
.append(slon);
}
builder.append("&to=").append(dname)
.append("&tocoord=").append(dlat)
.append(",")
.append(dlon);
uriString = builder.toString();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setPackage(PN_TENCENT_MAP);
intent.setData(Uri.parse(uriString));
context.startActivity(intent);
}
/**
* 打开百度地图导航功能(默认坐标点是高德地图,需要转换)
* @param context
* @param slat 起点纬度
* @param slon 起点经度
* @param sname 起点名称 可不填(0,0,null)
* @param dlat 终点纬度
* @param dlon 终点经度
* @param dname 终点名称 必填
*/
public static void openBaiDuNavi(Context context,double slat, double slon, String sname, double dlat, double dlon, String dname){
String uriString = null;
//终点坐标转换
// 此方法需要百度地图的BaiduLBS_Android.jar包
// LatLng destination = new LatLng(dlat,dlon);
// LatLng destinationLatLng = GCJ02ToBD09(destination);
// dlat = destinationLatLng.latitude;
// dlon = destinationLatLng.longitude;
double destination[] = gaoDeToBaidu(dlat, dlon);
dlat = destination[0];
dlon = destination[1];
StringBuilder builder = new StringBuilder("baidumap://map/direction?mode=driving&");
if (slat != 0){
//起点坐标转换
// LatLng origin = new LatLng(slat,slon);
// LatLng originLatLng = GCJ02ToBD09(origin);
// slat = originLatLng.latitude;
// slon = originLatLng.longitude;
double[] origin = gaoDeToBaidu(slat, slon);
slat = origin[0];
slon = origin[1];
builder.append("origin=latlng:")
.append(slat)
.append(",")
.append(slon)
.append("|name:")
.append(sname);
}
builder.append("&destination=latlng:")
.append(dlat)
.append(",")
.append(dlon)
.append("|name:")
.append(dname);
uriString = builder.toString();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setPackage(PN_BAIDU_MAP);
intent.setData(Uri.parse(uriString));
context.startActivity(intent);
}
}
跳转第三方地图软件的工具类,参考这位老哥写的MapUtil。
结束!芜湖~
本文地址:https://blog.csdn.net/ytw737006109/article/details/108868012