package com.up72.football.utils;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.support.annotation.DrawableRes;
import android.view.View;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.CircleOptions;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.Stroke;
import com.baidu.mapapi.map.TextOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.poi.PoiCitySearchOption;
import com.baidu.mapapi.search.poi.PoiNearbySearchOption;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.baidu.mapapi.search.poi.PoiSortType;
import com.baidu.mapapi.search.route.DrivingRoutePlanOption;
import com.baidu.mapapi.search.route.PlanNode;
import com.baidu.mapapi.search.route.RoutePlanSearch;
import java.util.ArrayList;
/**
* 地图 工具类
*/
public class MapUtil {
/**
* 位图缩放
**/
private static Bitmap big(Bitmap bitmap) {
Matrix matrix = new Matrix();
matrix.postScale(1.2f, 1.2f); //长和宽放大缩小的比例
Bitmap resizeBmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
return resizeBmp;
}
/**
* 添加标注覆盖物
*
* @param x 纬度
* @param y 经度
* @param picId 标记图片id
* @param isDragable 是否可以拖曳
*/
/* public static void addMapCover(BaiduMap mBaiduMap, double x, double y, int picId, boolean isDragable) {
//定义Maker 坐标点
LatLng point = new LatLng(x, y);
//构建Maker 图标
BitmapDescriptor descriptor = BitmapDescriptorFactory.fromResource(picId);
//构建MakerOptions ,用于在地图上添加“标记”
OverlayOptions options = new MarkerOptions()
.position(point)
.icon(descriptor)
.zIndex(9)
.draggable(isDragable);
mBaiduMap.addOverlay(options);
}*/
/**
* 添加标注覆盖物
*
* @param x 纬度
* @param y 经度
* @param picId 标记图片id
* @param isDragable 是否可以拖曳
*/
public static Marker addMapCover(Context context, BaiduMap mBaiduMap, double x, double y, @DrawableRes int picId, boolean isDragable) {
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), picId);
Bitmap bigBitmap = big(bitmap);
int width = bigBitmap.getWidth();
int height = bigBitmap.getHeight();
//定义Maker 坐标点
LatLng point = new LatLng(x, y);
//构建Maker 图标
BitmapDescriptor descriptor = BitmapDescriptorFactory.fromBitmap(bigBitmap);
//构建MakerOptions ,用于在地图上添加“标记”
OverlayOptions options = new MarkerOptions()
.position(point)
.icon(descriptor)
.anchor(-width / 2, -height)
.zIndex(9)
.draggable(isDragable);
return (Marker) (mBaiduMap.addOverlay(options));
}
/**
* 添加标注覆盖物
*
* @param x 纬度
* @param y 经度
* @param bitmap 标记图片Bitmap
* @param isDragable 是否可以拖曳
*/
public static Marker addMapCover(Context context, BaiduMap mBaiduMap, double x, double y, Bitmap bitmap, boolean isDragable) {
Bitmap bigBitmap = big(bitmap);
int width = bigBitmap.getWidth();
int height = bigBitmap.getHeight();
//定义Maker 坐标点
LatLng point = new LatLng(x, y);
//构建Maker 图标
BitmapDescriptor descriptor = BitmapDescriptorFactory.fromBitmap(bigBitmap);
//构建MakerOptions ,用于在地图上添加“标记”
OverlayOptions options = new MarkerOptions()
.position(point)
.icon(descriptor)
.anchor(-width / 2, -height)
.zIndex(9)
.draggable(isDragable);
return (Marker) (mBaiduMap.addOverlay(options));
}
/**
* 添加标注覆盖物
*
* @param x 纬度
* @param y 经度
* @param picId 标记图片id
* @param isDragable 是否可以拖曳
*/
public static void addMapCoverCurrent(Context context, BaiduMap mBaiduMap, double x, double y, @DrawableRes int picId, boolean isDragable) {
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), picId);
int width = bitmap.getWidth();
int height = bitmap.getHeight();
mBaiduMap.clear();
//定义Maker 坐标点
LatLng point = new LatLng(x, y);
//构建Maker 图标
BitmapDescriptor descriptor = BitmapDescriptorFactory.fromBitmap(bitmap);
//构建MakerOptions ,用于在地图上添加“标记”
OverlayOptions options = new MarkerOptions()
.position(point)
.icon(descriptor)
.anchor(-width / 2, -height)
.zIndex(10)
.draggable(isDragable);
mBaiduMap.addOverlay(options);
}
/**
* 添加弹出窗覆盖物
*
* @param context
* @param mBaiduMap
* @param imgPath
* @param name
* @param latitude
* @param longitude
* @param position
*/
// public void addInfoWindow(Context context, BaiduMap mBaiduMap, String imgPath, String name, double latitude, double longitude, final int position) {
// View view = LayoutInflater.from(context).inflate(R.layout.map_cover_bg, null);
// TextView tvAddress = (TextView) view.findViewById(R.id.txtAddress);
// ImageView ivPileLogo = (ImageView) view.findViewById(R.id.ivPileLogo);
// tvAddress.setText(name);
// Glide.with(context).load(imgPath).placeholder(R.drawable.ic_default_img).error(R.drawable.ic_default_img).crossFade().into(ivPileLogo);
// Glide.with(MapChargePileActivity.this).load(imgPath).centerCrop().placeholder(R.drawable.ic_default_img).crossFade().into(ivPileLogo);
// LatLng latLng = new LatLng(latitude, longitude);
// BitmapDescriptor descriptor = BitmapDescriptorFactory.fromView(view);
// InfoWindow mInfoWindow = new InfoWindow(descriptor, latLng, -47, new InfoWindow.OnInfoWindowClickListener() {
// @Override
// public void onInfoWindowClick() {
// if (type.equals("ChargePileListInfo")) {
// RouteManager.getInstance().toStationInfo(context, stationModels.get(position));
// }
// }
// });
// mBaiduMap.showInfoWindow(mInfoWindow);
// }
/**
* 添加标注覆盖物
*
* @param point 经纬度参数
* @param picId 标记图片id
* @param isDragable 是否可以拖曳
*/
public static void addMapCover(Context context, BaiduMap mBaiduMap, LatLng point, @DrawableRes int picId, boolean isDragable) {
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), picId);
int width = bitmap.getWidth();
int height = bitmap.getHeight();
//构建Maker 图标
BitmapDescriptor descriptor = BitmapDescriptorFactory.fromBitmap(bitmap);
//构建MakerOptions ,用于在地图上添加“标记”
OverlayOptions options = new MarkerOptions()
.position(point)
.icon(descriptor)
.anchor(-width / 2, -height)
.zIndex(9)
.draggable(isDragable);
mBaiduMap.addOverlay(options);
}
/**
* 自定义地图覆盖物Marker
*
* @param addViewContent 自定义覆盖物的View,注:最外层layout必须是LinearLayout
* @return bitmap
*/
public static Bitmap customMarker(View addViewContent) {
addViewContent.setDrawingCacheEnabled(true);
addViewContent.measure(
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
addViewContent.layout(0, 0,
addViewContent.getMeasuredWidth(),
addViewContent.getMeasuredHeight());
addViewContent.buildDrawingCache();
/* Bitmap cacheBitmap = addViewContent.getDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
return bitmap;*/
return addViewContent.getDrawingCache();
}
/**
* 添加具有动画的标记
* 一组动画图片资源
*
* @param x 纬度
* @param y 经度
* @param picResourceId
* @param layer 所在层级
* @param isDragable 是否可以拖曳
* @param period 多少帧刷新一次图片资源
*/
public static Marker addMarkerAnimation(BaiduMap mBaiduMap, double x, double y, int[] picResourceId, int layer, boolean isDragable, int period) {
ArrayList gifList = new ArrayList();
for (int i = 0; i < picResourceId.length; i++) {
BitmapDescriptor descriptor = BitmapDescriptorFactory.fromResource(picResourceId[i]);
gifList.add(descriptor);
}
LatLng point = new LatLng(x, y);
OverlayOptions ooD = new MarkerOptions()
.position(point)
.icons(gifList)
.zIndex(layer)
.draggable(isDragable)
.period(period);
return (Marker) (mBaiduMap.addOverlay(ooD));
}
/**
* 添加文字标记
*
* @param x 纬度
* @param y 经度
* @param bgColor 背景色
* @param fontSize 字体大小
* @param fontColor 文字颜色
* @param title 文字内容
* @param rotate 旋转角度
* @return OverlayOptions
*/
public static OverlayOptions addTextMarker(double x, double y, int bgColor, int fontSize, int fontColor, String title, float rotate) {
OverlayOptions txtOptions = null;
LatLng txtPosition = new LatLng(x, y);
txtOptions = new TextOptions()
.bgColor(bgColor)
.fontSize(fontSize)
.fontColor(fontColor)
.text(title)
.rotate(rotate)
.position(txtPosition);
return txtOptions;
}
/**
* 定位到当前所在位置
*
* @param x 纬度
* @param y 经度
* @return MapStatusUpdate
*/
public static void locationToCurrentLocation(BaiduMap mBaiduMap, double x, double y) {
//设置中心点坐标
LatLng cenPt = new LatLng(x, y);
//定义地图状态
MapStatus mMapStatus = new MapStatus.Builder()
.target(cenPt)
.zoom(13)
.build();
//定义MapStatusUpdate 对象,以便描述地图状态将要发生变化
MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
mBaiduMap.setMapStatus(mMapStatusUpdate);
}
/**
* 定位到当前省位置
*
* @param x 纬度
* @param y 经度
* @return MapStatusUpdate
*/
public static void locationToProvinceLocation(BaiduMap mBaiduMap, double x, double y) {
//设置中心点坐标
LatLng cenPt = new LatLng(x, y);
//定义地图状态
MapStatus mMapStatus = new MapStatus.Builder()
.target(cenPt)
.zoom(6.5f)
.build();
//定义MapStatusUpdate 对象,以便描述地图状态将要发生变化
MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
mBaiduMap.setMapStatus(mMapStatusUpdate);
}
/**
* 定位到中国位置
*
* @return MapStatusUpdate
*/
public static void locationToChinaLocation(BaiduMap mBaiduMap) {
//定义地图状态
MapStatus mMapStatus = new MapStatus.Builder()
.target(new LatLng(37.462209, 102.846898))
.zoom(4.8f)
.build();
//定义MapStatusUpdate 对象,以便描述地图状态将要发生变化
MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
mBaiduMap.setMapStatus(mMapStatusUpdate);
}
/**
* 定位到当前所在位置
*
* @param x 纬度
* @param y 经度
* @return MapStatusUpdate
*/
public static void locationTozoom(BaiduMap mBaiduMap, double x, double y, int zoom) {
//设置中心点坐标
LatLng cenPt = new LatLng(x, y);
//定义地图状态
MapStatus mMapStatus = new MapStatus.Builder()
.target(cenPt)
.zoom(zoom)
.build();
//定义MapStatusUpdate 对象,以便描述地图状态将要发生变化
MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
mBaiduMap.setMapStatus(mMapStatusUpdate);
}
/**
* 修改中心点
*
* @param x 纬度
* @param y 经度
* @return MapStatusUpdate
*/
public static void toCenterPoint(BaiduMap mBaiduMap, double x, double y) {
//设置中心点坐标
LatLng cenPt = new LatLng(x, y);
//定义地图状态
MapStatus mMapStatus = new MapStatus.Builder()
.target(cenPt)
.build();
//定义MapStatusUpdate 对象,以便描述地图状态将要发生变化
MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
mBaiduMap.setMapStatus(mMapStatusUpdate);
}
//配置定位参数
public static LocationClientOption setLocationParamter() {
LocationClientOption option = new LocationClientOption();
//设置定位模式---高精度定位模式
option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
option.setCoorType("bd09ll");
int span = 1000;
option.setScanSpan(span);
option.setIsNeedAddress(true); //是否需要地址信息,默认不需要
option.setOpenGps(true);
option.setLocationNotify(true);
option.setIsNeedLocationDescribe(true);
option.setIsNeedLocationPoiList(true);
option.setIgnoreKillProcess(false);
option.setEnableSimulateGps(true);
return option;
}
/**
* 创建圆形覆盖物
*
* @param baiduMap
* @param latitude 纬度
* @param lontitude 经度
* @param radius //覆盖范围(半径) 单位(米)
*
* 39.93923, 116.357428
*/
public static void createCircleArea(BaiduMap baiduMap, double latitude, double lontitude, int radius) {
LatLng center = new LatLng(latitude, lontitude);
Stroke stroke = new Stroke(2, 0xAA64B5F6);
OverlayOptions circleOptions = new CircleOptions()
.center(center)
.radius(radius)
.stroke(stroke)
.fillColor(0xAAE1EEF7);
baiduMap.addOverlay(circleOptions);
}
/**
* 在这个radius内,检索keyword的所有poi点
*
* @param poiSearch
* @param latitude
* @param lontitude
* @param keyword 搜索关键字
* @param radius 搜索范围
*/
public static void getBoundsPOI(PoiSearch poiSearch, double latitude, double lontitude, String keyword, int radius) {
LatLng cenLatLng = new LatLng(latitude, lontitude);
PoiNearbySearchOption nearbySearch = new PoiNearbySearchOption();
nearbySearch.location(cenLatLng);
nearbySearch.keyword(keyword);
nearbySearch.radius(radius);
nearbySearch.sortType(PoiSortType.distance_from_near_to_far);
poiSearch.searchNearby(nearbySearch);
}
/**
* 搜索某个城市内的所有keyword 的POI点
*
* @param poiSearch
* @param city
* @param keyword
*/
public static void getCityPOI(PoiSearch poiSearch, String city, String keyword) {
poiSearch.searchInCity(new PoiCitySearchOption()
.city(city)
.keyword(keyword));
}
/**
* 线路规划
*
* @param baiduMap
* @param mSearch
* @param fromLatLng 始发地经纬度
* @param toLatLng 目的地经纬度
*/
public static void routePlan(BaiduMap baiduMap, RoutePlanSearch mSearch, LatLng fromLatLng, LatLng toLatLng) {
//重置浏览节点的路线数据
baiduMap.clear();
PlanNode startNode = PlanNode.withLocation(fromLatLng);
PlanNode endNode = PlanNode.withLocation(toLatLng);
mSearch.drivingSearch((new DrivingRoutePlanOption())
.from(startNode)
.to(endNode));
}
public static LatLng bd09_To_Gcj02(double bd_lat, double bd_lon) {
double pi = 3.1415926535897932384626;
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);
double gg_lon = z * Math.cos(theta);
double gg_lat = z * Math.sin(theta);
return new LatLng(gg_lat, gg_lon);
}
// /**
// * 调用百度地图客户端
// *
// * @param startLat
// * @param startLon
// * @param endLat
// * @param endLng
// * @param context
// */
// public static void getBaiduClientNavi(double startLat, double startLon, double endLat, double endLng, Context context) {
// // 构建 导航参数
// LatLng pt1 = new LatLng(startLat, startLon);
// LatLng pt2 = new LatLng(endLat, endLng);
//
// NaviParaOption para = new NaviParaOption()
// .startPoint(pt1).endPoint(pt2);
//
// try {
// BaiduMapNavigation.setSupportWebNavi(true);
// // 调起百度地图导航
// boolean b = BaiduMapNavigation.openBaiduMapNavi(para, context);
// Log.e("百度地图导航", b + "");
// } catch (BaiduMapAppNotSupportNaviException e) {
// e.printStackTrace();
// Log.e("百度地图导航", "您尚未安装百度地图app或app版本过低");
// Toast.makeText(context.getApplicationContext(), "您尚未安装百度地图app或app版本过低", Toast.LENGTH_SHORT).show();
// } catch (IllegalNaviArgumentException e) {
// e.printStackTrace();
// Log.e("百度地图导航", "非法导航参数");
// Toast.makeText(context.getApplicationContext(), "非法导航参数", Toast.LENGTH_SHORT).show();
// } catch (Exception e) {
// e.printStackTrace();
// Log.e("百度地图导航", "其它异常");
// Toast.makeText(context.getApplicationContext(), "其它异常", Toast.LENGTH_SHORT).show();
// }
// }
}