Commit 0570a58d authored by Administrator's avatar Administrator

udpate

parent 6dcb33ce
......@@ -9,6 +9,7 @@ import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
......@@ -45,6 +46,7 @@ import top.littlerich.virtuallocation.listener.GeoCoderListener;
import top.littlerich.virtuallocation.listener.MapClickListener;
import top.littlerich.virtuallocation.listener.MarkerDragListener;
import top.littlerich.virtuallocation.model.Gps;
import top.littlerich.virtuallocation.util.MapDistanceUtil;
import top.littlerich.virtuallocation.util.PermissionUtil;
import top.littlerich.virtuallocation.view.TopBanner;
......@@ -106,7 +108,7 @@ public class MainActivity extends BaseActivity implements View.OnClickListener {
== PackageManager.PERMISSION_GRANTED) {
// 在使用 SDK 各组间之前初始化 context 信息,传入 ApplicationContext
SDKInitializer.initialize(getApplicationContext());
SDKInitializer.setCoordType(CoordType.BD09LL);
SDKInitializer.setCoordType(CoordType.GCJ02);
} else {
Toast.makeText(this, "没有权限,无法工作", Toast.LENGTH_SHORT).show();
}
......@@ -133,6 +135,7 @@ public class MainActivity extends BaseActivity implements View.OnClickListener {
mBaiduMap = mMapView.getMap();
// 开启定位图层
mBaiduMap.setMyLocationEnabled(true);
//隐藏地图比例尺
mMapView.showScaleControl(false);
//关闭缩放放大控件
......@@ -230,7 +233,7 @@ public class MainActivity extends BaseActivity implements View.OnClickListener {
private void iniMap() {
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true);// 打开gps
option.setCoorType("bd09ll"); // 设置坐标类型
option.setCoorType("gcj02"); // 设置坐标类型
option.setScanSpan(3000);
mCurrentMode = MyLocationConfiguration.LocationMode.NORMAL;
......@@ -268,10 +271,21 @@ public class MainActivity extends BaseActivity implements View.OnClickListener {
MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
mBaiduMap.animateMapStatus(u);
Log.i("virjar", "移动坐标: " + ll + " " + ll.longitude + "," + ll.latitude);
MapDistanceUtil.GeoPoint geoPoint = MapDistanceUtil.gcj02ToGps84(arg0.latitude, arg0.longitude);
Log.i("virjar", "bd09:" + geoPoint.getLon() + "," + geoPoint.getLat());
LatLng reverseGeoRequestLatLng = new LatLng(geoPoint.getLat(), geoPoint.getLon());
// 根据经纬度坐标 找到实地信息,会在接口onGetReverseGeoCodeResult中呈现结果
mSearch.reverseGeoCode(new ReverseGeoCodeOption().location(arg0));
mSearch.reverseGeoCode(
new ReverseGeoCodeOption()
.newVersion(0)
.pageNum(0)
.location(reverseGeoRequestLatLng)
.radius(150)
);
}
@Override
protected void thisFinish() {
AlertDialog.Builder build = new AlertDialog.Builder(this);
......
......@@ -43,7 +43,7 @@ public class AppApplication extends Application {
== PackageManager.PERMISSION_GRANTED) {
// 在使用 SDK 各组间之前初始化 context 信息,传入 ApplicationContext
SDKInitializer.initialize(this);
SDKInitializer.setCoordType(CoordType.BD09LL);
SDKInitializer.setCoordType(CoordType.GCJ02);
}
......
package top.littlerich.virtuallocation.listener;
import android.content.Context;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
......@@ -26,6 +27,7 @@ public class GeoCoderListener implements OnGetGeoCoderResultListener {
/**
* 搜索(根据实地信息-->经纬坐标)
*
* @param geoCodeResult
*/
@Override
......@@ -35,6 +37,7 @@ public class GeoCoderListener implements OnGetGeoCoderResultListener {
/**
* 搜索(根据坐标-->实地信息)
*
* @param result
*/
@Override
......@@ -43,6 +46,7 @@ public class GeoCoderListener implements OnGetGeoCoderResultListener {
Toast.makeText(mContext, "抱歉,未能找到结果", Toast.LENGTH_LONG).show();
return;
}
Log.i("virjar", "逆地理:" + result);
mLocationTip.setText(String.format("伪造位置:%s", result.getAddress()));
}
}
......@@ -23,6 +23,7 @@ public class MapClickListener implements BaiduMap.OnMapClickListener {
/**
* 地图单击事件
*
* @param latLng 点击的地理坐标
*/
@Override
......@@ -35,11 +36,14 @@ public class MapClickListener implements BaiduMap.OnMapClickListener {
/**
* 地图内 Poi 单击事件
* @param mapPoi 点击的 poi 信息
*
* @param mapPoi 点击的 poi 信息
* @return
*/
@Override
public void onMapPoiClick(MapPoi mapPoi) {
// return false;
// return false;
LatLng position = mapPoi.getPosition();
this.onMapClick(position);
}
}
package top.littlerich.virtuallocation.util;
/**
* MapDistanceUtil
*
* @author binghao.huang
* @date 2020-03-04
*/
public class MapDistanceUtil {
private static double PI = 3.14159265;
public static double A = 6378245.0;
public static double EE = 0.00669342162296594323;
public static void main(String[] args) {
// 116.456298,39.923568
GeoPoint gcj02 = bd09ToGcj02(39.923568, 116.456298);
GeoPoint gps = bd09ToGps84(39.923568, 116.456298);
System.out.println(gcj02);
System.out.println(gps);
// 116.397036,39.917834
GeoPoint bd = gcj02ToBd09(39.917834, 116.397036);
gps = gcj02ToGps84(39.917834, 116.397036);
System.out.println(bd);
System.out.println(gps);
}
/**
* gps84 to gcj02
*
* @param lat 纬度
* @param lon 经度
* @return gcj02坐标
*/
public static GeoPoint gps84ToGcj02(double lat, double lon) {
if (outOfChina(lat, lon)) {
return null;
}
double dLat = transformLat(lon - 105.0, lat - 35.0);
double dLon = transformLon(lon - 105.0, lat - 35.0);
double radLat = lat / 180.0 * PI;
double magic = Math.sin(radLat);
magic = 1 - EE * magic * magic;
double sqrtMagic = Math.sqrt(magic);
dLat = (dLat * 180.0) / ((A * (1 - EE)) / (magic * sqrtMagic) * PI);
dLon = (dLon * 180.0) / (A / sqrtMagic * Math.cos(radLat) * PI);
double mgLat = lat + dLat;
double mgLon = lon + dLon;
return new GeoPoint(mgLat, mgLon);
}
/**
* gps84 to bd09
*
* @param lat 纬度
* @param lon 经度
* @return
*/
public static GeoPoint gps84ToBd09(double lat, double lon) {
GeoPoint gps = gps84ToGcj02(lat, lon);
return gcj02ToBd09(gps.getLat(), gps.getLon());
}
/**
* gcj02 to gps84
*
* @param lat 纬度
* @param lon 经度
* @return
*/
public static GeoPoint gcj02ToGps84(double lat, double lon) {
double[] gps = transform(lat, lon);
double longitude = lon * 2 - gps[1];
double latitude = lat * 2 - gps[0];
return new GeoPoint(latitude, longitude);
}
/**
* gcj02 to bd09
*
* @param lat 纬度
* @param lon 经度
* @return
*/
public static GeoPoint gcj02ToBd09(double lat, double lon) {
double x = lon, y = 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);
double bdLon = z * Math.cos(theta) + 0.0065;
double bdLat = z * Math.sin(theta) + 0.006;
return new GeoPoint(bdLat, bdLon);
}
/**
* bd04 to gps84
*
* @param lat 纬度
* @param lon 经度
* @return
*/
public static GeoPoint bd09ToGps84(double lat, double lon) {
GeoPoint gcj = bd09ToGcj02(lat, lon);
return gcj02ToGps84(gcj.getLat(), gcj.getLon());
}
/**
* bd09 to gcj02
*
* @param lat 纬度
* @param lon 经度
* @return
*/
public static GeoPoint bd09ToGcj02(double lat, double lon) {
double x = lon - 0.0065, y = lat - 0.006;
double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * Math.PI);
double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * Math.PI);
double tempLng = z * Math.cos(theta);
double tempLat = z * Math.sin(theta);
return new GeoPoint(tempLat, tempLng);
}
/**
* 计算两点之间距离
*
* @param lng1
* @param lat1
* @param lng2
* @param lat2
* @return
*/
public static long getDistance(double lng1, double lat1, double lng2, double lat2) {
double RAD = Math.PI / 180.0;
double radLat1 = lat1 * RAD;
double radLat2 = lat2 * RAD;
double a = radLat1 - radLat2;
double b = (lng1 - lng2) * RAD;
double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
s = s * 6378137;
return Math.round(s * 10000) / 10000;
}
public static double[] transform(double lat, double lon) {
double dLat = transformLat(lon - 105.0, lat - 35.0);
double dLon = transformLon(lon - 105.0, lat - 35.0);
double radLat = lat / 180.0 * PI;
double magic = Math.sin(radLat);
magic = 1 - EE * magic * magic;
double sqrtMagic = Math.sqrt(magic);
dLat = (dLat * 180.0) / ((A * (1 - EE)) / (magic * sqrtMagic) * PI);
dLon = (dLon * 180.0) / (A / sqrtMagic * Math.cos(radLat) * PI);
double mgLat = lat + dLat;
double mgLon = lon + dLon;
return new double[]{mgLat, mgLon};
}
public static double transformLat(double x, double y) {
double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y
+ 0.2 * Math.sqrt(Math.abs(x));
ret += (20.0 * Math.sin(6.0 * x * PI) + 20.0 * Math.sin(2.0 * x * PI)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(y * PI) + 40.0 * Math.sin(y / 3.0 * PI)) * 2.0 / 3.0;
ret += (160.0 * Math.sin(y / 12.0 * PI) + 320 * Math.sin(y * PI / 30.0)) * 2.0 / 3.0;
return ret;
}
public static double transformLon(double x, double y) {
double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1
* Math.sqrt(Math.abs(x));
ret += (20.0 * Math.sin(6.0 * x * PI) + 20.0 * Math.sin(2.0 * x * PI)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(x * PI) + 40.0 * Math.sin(x / 3.0 * PI)) * 2.0 / 3.0;
ret += (150.0 * Math.sin(x / 12.0 * PI) + 300.0 * Math.sin(x / 30.0
* PI)) * 2.0 / 3.0;
return ret;
}
/**
* is or not outOfChina
*
* @param lat
* @param lon
* @return
*/
public static boolean outOfChina(double lat, double lon) {
if (lon < 72.004 || lon > 137.8347) {
return true;
}
if (lat < 0.8293 || lat > 55.8271) {
return true;
}
return false;
}
public static class GeoPoint {
private double lat;
private double lon;
public GeoPoint(double lat, double lon) {
this.lat = lat;
this.lon = lon;
}
public double getLat() {
return lat;
}
public double getLon() {
return lon;
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment