您的位置:首页 > 编程语言 > Java开发

java操作地理位置信息

2018-03-28 13:01 351 查看

1.计算矩形

package com.kensure.forecast.util;

import java.util.HashMap;
import java.util.Map;

/**
*
* @author lcc
* @time    2018-03-28
* @description
*          处理有关地球地理信息的工具
*/
public class EarthMapUtils {

/**
*
* @param longitude    经度
* @param latitude    纬度
* @param distance    范围(米)
* @description
*      x代表经度,y代表纬度
*      假设给了一个点O(x1,y1),求距离这个点300米的方块四个角的坐标A,B,C,D点的经度纬度
*
*                        /|\ 纬度 Y
*                         |
*      A  -----------------|----------------B
*      |                  |   |            |
*      |                  |   |            |
*      |                  |   300米         |
*      |                  |   |            |
* ------------------------|-----------------------------------》 经度 X
*      |                  |O(X1,Y1)        |
*      |                  |                |
*      |                  |------300米------
*      |                  |                |
*      |                  |                |
*      |------------------|----------------D
*      C                  |
* @return
*      Map<String, double[纬度,经度]>
*/
public static Map<String, double[]> returnLLSquarePoint(double longitude,  double latitude, double distance) {
Map<String, double[]> squareMap = new HashMap<String, double[]>();
// 计算经度弧度,从弧度转换为角度
double dLongitude = 2 * (Math.asin(Math.sin(distance
/ (2 * 6378137))
/ Math.cos(Math.toRadians(latitude))));
dLongitude = Math.toDegrees(dLongitude);

4000
// 计算纬度角度
double dLatitude = distance / 6378137;
dLatitude = Math.toDegrees(dLatitude);
// 正方形
double[] leftTopPoint = { latitude + dLatitude, longitude - dLongitude };
double[] rightTopPoint = { latitude + dLatitude, longitude + dLongitude };
double[] leftBottomPoint = { latitude - dLatitude,longitude - dLongitude };
double[] rightBottomPoint = { latitude - dLatitude,longitude + dLongitude };

squareMap.put("leftTopPoint", leftTopPoint);
squareMap.put("rightTopPoint", rightTopPoint);
squareMap.put("leftBottomPoint", leftBottomPoint);
squareMap.put("rightBottomPoint", rightBottomPoint);
System.out.println("==========("+leftTopPoint[1]+","+leftTopPoint[0]+")"+"===============("+rightTopPoint[1]+","+rightTopPoint[0]+")");
System.out.println("==========                                                                          ");
System.out.println("==========                        ("+longitude+","+latitude+")                                   ");
System.out.println("==========                                                                          ");
System.out.println("==========("+leftBottomPoint[1]+","+leftBottomPoint[0]+")"+"===============("+rightBottomPoint[1]+","+rightBottomPoint[0]+")");
return squareMap;
}

/**
* 判断一个点(已知经度和纬度)是否在一个已知四个角经度和纬度的正方形内
* @param angle             正方形的四个角
* @param targetLongitude   要判断点是否在矩形内的经度
* @param targetLatitude    要判断点是否在矩形内的纬度
* @return
*/
public static boolean isHitAngle(Map<String, double[]> angle,double targetLongitude,double targetLatitude){
// 目标经度纬度
double x = targetLongitude;
double y = targetLatitude;

// 四个角
double[] aPoint = angle.get("leftTopPoint");
double[] bPoint = angle.get("rightTopPoint");
double[] cPoint = angle.get("leftBottomPoint");
double[] dPoint = angle.get("rightBottomPoint");

// 注意格式 【纬度】【经度】
boolean  flag1 = aPoint[1] <= x && x <= bPoint[1];//经度在范围内
boolean  flag2 = cPoint[0] <= y && y <= aPoint[0];//经度在范围内
boolean  flag3 = flag1 && flag2;//给定的点在坐标内

if(flag3){
return true;
}
return false;

}

public static void main(String[] args) {
Map<String, double[]> aa = EarthMapUtils.returnLLSquarePoint(119.21,-29.45,300);
double[] aPoint = aa.get("leftTopPoint");
double[] bPoint = aa.get("rightTopPoint");
double[] cPoint = aa.get("leftBottomPoint");
double[] dPoint = aa.get("rightBottomPoint");
System.out.println("==========("+aPoint[1]+","+aPoint[0]+")"+"===============("+bPoint[1]+","+bPoint[0]+")");
System.out.println("==========                                                                          ");
System.out.println("==========                        (119.21,-29.45)                                   ");
System.out.println("==========                                                                          ");
System.out.println("==========("+cPoint[1]+","+cPoint[0]+")"+"===============("+dPoint[1]+","+dPoint[0]+")");
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: