如何使用Java
在城市中生成随机位置(经度和纬度)?
我想测试一个城市内的随机位置(目前只有一个城市)的应用功能。
答案 0 :(得分:1)
您可能想要使用Google地图"反向地理编码"帮助查看一个随机位置是否在一个城市内。
考虑以下部分代码演示:
TestRandomCityLocations Main
package com.example.mapping;
import java.util.Random;
public class TestRandomCityLocation {
// Class fields
private static TestRandomCityLocation me;
private static final String[] defaultArguments = { "" };
// Object fields
public static void main(String[] args) {
String[] arguments;
me = new TestRandomCityLocation();
if (args == null || args.length == 0) {
arguments = defaultArguments;
} else {
arguments = args;
}
me.doWork(arguments);
}
private void doWork(String[] arguments) {
int maximumPoints = 100;
GeographicPoint newLocation;
GeographicSquare mockCityGeo = new GeographicSquare(
new GeographicPoint(38.7, -77.0), new GeographicPoint(38.9,
-76.9));
String cityName = "Google Maps city name";
Random randomGenerator = new Random();
double deltaLong = mockCityGeo.getDeltaLongitude();
double deltaLat = mockCityGeo.getDeltaLatitude();
for (int i = 0; i < maximumPoints; i++) {
newLocation = new GeographicPoint(
mockCityGeo.getLowerLeftLatitude() + deltaLat
* randomGenerator.nextDouble(),
mockCityGeo.getLowerLeftLongitude() + deltaLong
* randomGenerator.nextDouble());
// TODO test to see of newLocation is in the city
if (GoogleMapInfo.isWithinCity(cityName, newLocation)) {
// TODO Do something
}
}
}
}
GeographicSquare类
package com.example.mapping;
public class GeographicSquare {
// Object fields
private GeographicPoint lowerLeft;
private GeographicPoint upperRight;
public GeographicSquare (GeographicPoint lowerLeft, GeographicPoint upperRight) {
this.lowerLeft = lowerLeft;
this.upperRight = upperRight;
}
public double getDeltaLongitude() {
return lowerLeft.getLongitudeDifference(upperRight);
}
public double getDeltaLatitude() {
return lowerLeft.getLatitudeDifference(upperRight);
}
public double getLowerLeftLongitude() {
return lowerLeft.getLongitude();
}
public double getLowerLeftLatitude() {
return lowerLeft.getLatitude();
}
}
GeographicPoint类
package com.example.mapping;
public class GeographicPoint {
// Object fields
private double latitude;
private double longitude;
public GeographicPoint(double latitudeDegrees, double longitudeDegrees) {
this.latitude = latitudeDegrees;
this.longitude = longitudeDegrees;
}
public double getLatitude() {
return latitude;
}
public double getLongitude() {
return longitude;
}
/** Distance calculation using Haversine formula to calculate a great-circle distance
* between two points of the surface of the earth.
*
* formula: a = sin²(Δφ/2) + cos φ1 ⋅ cos φ2 ⋅ sin²(Δλ/2)
* c = 2 ⋅ atan2( √a, √(1−a) )
* d = R ⋅ c
where φ is latitude, λ is longitude, R is earth’s radius
(mean radius = 3958,756 miles (6,371km));
Note bene: angles must be in radians to pass to trig functions!
* @param location
* @return
*/
public double milesFrom(GeographicPoint location) {
double rInMiles = 3958.756;
double lat1Radians = radiansFromDegrees(latitude);
double lat2Radians = radiansFromDegrees(location.getLatitude());
double deltaLat = radiansFromDegrees(location.getLatitude()-latitude);
double deltaLong = radiansFromDegrees(location.getLongitude()-longitude);
double a = Math.sin(deltaLat/2.0) * Math.sin(deltaLat/2.0) +
Math.cos(lat1Radians) * Math.cos(lat2Radians) * Math.sin(deltaLong/2.0)*Math.sin(deltaLong/2.0);
double c = 2.0 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double d = rInMiles * c;
return d;
}
public double getLatitudeDifference(GeographicPoint location) {
return Math.abs(latitude - location.getLatitude());
}
public double getLongitudeDifference(GeographicPoint location) {
return Math.abs(longitude - location.getLongitude());
}
public GeographicPoint getOffsetPoint(double latitudeOffset, double longitudeOffset) {
return new GeographicPoint(latitude+latitudeOffset, longitude+longitudeOffset);
}
// Internal methods
private double radiansFromDegrees(double degrees) {
return (degrees * Math.PI) / 180.0;
}
}
模拟GoogleMapInfo类
package com.example.mapping;
public class GoogleMapInfo {
// Consider using Reverse Geocoding in Google Maps api and look at locality political
// see: [https://developers.google.com/maps/documentation/geocoding/#ReverseGeocoding][1]
public static boolean isWithinCity(String cityName,
GeographicPoint newLocation) {
// Get governmental info for location and see if the city name matches
return true;
}
}