我的持久对象中存储了多种位置,包含纬度和经度,格式为双(43.7389,7.42577)。我需要能够获取用户的纬度和经度并选择其中的所有项目,例如1英里。步行距离。 我已经在PHP中完成了这个,所以我抓住了我的PHP代码并将其转移到Java,其中所有内容都插入正常,直到我发现J2ME不支持atan2(double,double)。因此,经过一些搜索,我发现了一小段代码,它应该是atan2的替代品。这是代码:
public double atan2(double y, double x) {
double coeff_1 = Math.PI / 4d;
double coeff_2 = 3d * coeff_1;
double abs_y = Math.abs(y)+ 1e-10f;
double r, angle;
if (x >= 0d) {
r = (x - abs_y) / (x + abs_y);
angle = coeff_1;
} else {
r = (x + abs_y) / (abs_y - x);
angle = coeff_2;
}
angle += (0.1963f * r * r - 0.9817f) * r;
return y < 0.0f ? -angle : angle;
}
我得到了奇怪的结果。我的最小和最大纬度和经度都回来了,数字非常低,不可能是正确的。就像0.003785746,当我期待更接近原始纬度和长值(43.7389,7.42577)时。
由于我不是高级数学的大师,我真的不知道在这里要找什么。也许其他人可能有答案。
这是我的完整代码:
package store_finder;
import java.util.Vector;
import javax.microedition.location.Criteria;
import javax.microedition.location.Location;
import javax.microedition.location.LocationException;
import javax.microedition.location.LocationListener;
import javax.microedition.location.LocationProvider;
import javax.microedition.location.QualifiedCoordinates;
import net.rim.blackberry.api.invoke.Invoke;
import net.rim.blackberry.api.invoke.MapsArguments;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.RichTextField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
public class nearBy extends MainScreen {
private HorizontalFieldManager _top;
private VerticalFieldManager _middle;
private int horizontalOffset;
private final static long animationTime = 300;
private long animationStart = 0;
private double latitude = 43.7389;
private double longitude = 7.42577;
private int _interval = -1;
private double max_lat;
private double min_lat;
private double max_lon;
private double min_lon;
private double latitude_in_degrees;
private double longitude_in_degrees;
public nearBy()
{
super();
horizontalOffset = Display.getWidth();
_top = new HorizontalFieldManager(Manager.USE_ALL_WIDTH | Field.FIELD_HCENTER)
{
public void paint(Graphics gr)
{
Bitmap bg = Bitmap.getBitmapResource("bg.png");
gr.drawBitmap(0, 0, Display.getWidth(), Display.getHeight(), bg, 0, 0);
subpaint(gr);
}
};
_middle = new VerticalFieldManager()
{
public void paint(Graphics graphics)
{
graphics.setBackgroundColor(0xFFFFFF);
graphics.setColor(Color.BLACK);
graphics.clear();
super.paint(graphics);
}
protected void sublayout(int maxWidth, int maxHeight)
{
int displayWidth = Display.getWidth();
int displayHeight = Display.getHeight();
super.sublayout( displayWidth, displayHeight);
setExtent( displayWidth, displayHeight);
}
};
add(_top);
add(_middle);
Bitmap lol = Bitmap.getBitmapResource("logo.png");
BitmapField lolfield = new BitmapField(lol);
_top.add(lolfield);
Criteria cr= new Criteria();
cr.setCostAllowed(true);
cr.setPreferredResponseTime(60);
cr.setHorizontalAccuracy(5000);
cr.setVerticalAccuracy(5000);
cr.setAltitudeRequired(true);
cr.isSpeedAndCourseRequired();
cr.isAddressInfoRequired();
try{
LocationProvider lp = LocationProvider.getInstance(cr);
if( lp!=null ){
lp.setLocationListener(new LocationListenerImpl(), _interval, 1, 1);
}
}
catch(LocationException le)
{
add(new RichTextField("Location exception "+le));
}
//_middle.add(new RichTextField("this is a map " + Double.toString(latitude) + " " + Double.toString(longitude)));
int lat = (int) (latitude * 100000);
int lon = (int) (longitude * 100000);
String document = "<location-document>" + "<location lon='" + lon + "' lat='" + lat + "' label='You are here' description='You' zoom='0' />" + "<location lon='742733' lat='4373930' label='Hotel de Paris' description='Hotel de Paris' address='Palace du Casino' postalCode='98000' phone='37798063000' zoom='0' />" + "</location-document>";
// Invoke.invokeApplication(Invoke.APP_TYPE_MAPS, new MapsArguments( MapsArguments.ARG_LOCATION_DOCUMENT, document));
_middle.add(new SeparatorField());
surroundingVenues();
_middle.add(new RichTextField("max lat: " + max_lat));
_middle.add(new RichTextField("min lat: " + min_lat));
_middle.add(new RichTextField("max lon: " + max_lon));
_middle.add(new RichTextField("min lon: " + min_lon));
}
private void surroundingVenues()
{
double point_1_latitude_in_degrees = latitude;
double point_1_longitude_in_degrees= longitude;
// diagonal distance + error margin
double distance_in_miles = (5 * 1.90359441) + 10;
getCords (point_1_latitude_in_degrees, point_1_longitude_in_degrees, distance_in_miles, 45);
double lat_limit_1 = latitude_in_degrees;
double lon_limit_1 = longitude_in_degrees;
getCords (point_1_latitude_in_degrees, point_1_longitude_in_degrees, distance_in_miles, 135);
double lat_limit_2 = latitude_in_degrees;
double lon_limit_2 = longitude_in_degrees;
getCords (point_1_latitude_in_degrees, point_1_longitude_in_degrees, distance_in_miles, -135);
double lat_limit_3 = latitude_in_degrees;
double lon_limit_3 = longitude_in_degrees;
getCords (point_1_latitude_in_degrees, point_1_longitude_in_degrees, distance_in_miles, -45);
double lat_limit_4 = latitude_in_degrees;
double lon_limit_4 = longitude_in_degrees;
double mx1 = Math.max(lat_limit_1, lat_limit_2);
double mx2 = Math.max(lat_limit_3, lat_limit_4);
max_lat = Math.max(mx1, mx2);
double mm1 = Math.min(lat_limit_1, lat_limit_2);
double mm2 = Math.min(lat_limit_3, lat_limit_4);
min_lat = Math.max(mm1, mm2);
double mlon1 = Math.max(lon_limit_1, lon_limit_2);
double mlon2 = Math.max(lon_limit_3, lon_limit_4);
max_lon = Math.max(mlon1, mlon2);
double minl1 = Math.min(lon_limit_1, lon_limit_2);
double minl2 = Math.min(lon_limit_3, lon_limit_4);
min_lon = Math.max(minl1, minl2);
//$qry = "SELECT DISTINCT zip.zipcode, zip.latitude, zip.longitude, sg_stores.* FROM zip JOIN store_finder AS sg_stores ON sg_stores.zip=zip.zipcode WHERE zip.latitude<=$lat_limit_max AND zip.latitude>=$lat_limit_min AND zip.longitude<=$lon_limit_max AND zip.longitude>=$lon_limit_min";
}
private void getCords(double point_1_latitude, double point_1_longitude, double distance, int degs)
{
double m_EquatorialRadiusInMeters = 6366564.86;
double m_Flattening=0;
double distance_in_meters = distance * 1609.344 ;
double direction_in_radians = Math.toRadians( degs );
double eps = 0.000000000000005;
double r = 1.0 - m_Flattening;
double point_1_latitude_in_radians = Math.toRadians( point_1_latitude );
double point_1_longitude_in_radians = Math.toRadians( point_1_longitude );
double tangent_u = (r * Math.sin( point_1_latitude_in_radians ) ) / Math.cos( point_1_latitude_in_radians );
double sine_of_direction = Math.sin( direction_in_radians );
double cosine_of_direction = Math.cos( direction_in_radians );
double heading_from_point_2_to_point_1_in_radians = 0.0;
if ( cosine_of_direction != 0.0 )
{
heading_from_point_2_to_point_1_in_radians = atan2( tangent_u, cosine_of_direction ) * 2.0;
}
double cu = 1.0 / Math.sqrt( ( tangent_u * tangent_u ) + 1.0 );
double su = tangent_u * cu;
double sa = cu * sine_of_direction;
double c2a = ( (-sa) * sa ) + 1.0;
double x= Math.sqrt( ( ( ( 1.0 /r /r ) - 1.0 ) * c2a ) + 1.0 ) + 1.0;
x= (x- 2.0 ) / x;
double c= 1.0 - x;
c= ( ( (x * x) / 4.0 ) + 1.0 ) / c;
double d= ( ( 0.375 * (x * x) ) -1.0 ) * x;
tangent_u = distance_in_meters /r / m_EquatorialRadiusInMeters /c;
double y= tangent_u;
boolean exit_loop = false;
double cosine_of_y = 0.0;
double cz = 0.0;
double e = 0.0;
double term_1 = 0.0;
double term_2 = 0.0;
double term_3 = 0.0;
double sine_of_y = 0.0;
while( exit_loop != true )
{
sine_of_y = Math.sin(y);
cosine_of_y = Math.cos(y);
cz = Math.cos( heading_from_point_2_to_point_1_in_radians + y);
e = (cz * cz * 2.0 ) - 1.0;
c = y;
x = e * cosine_of_y;
y = (e + e) - 1.0;
term_1 = ( sine_of_y * sine_of_y * 4.0 ) - 3.0;
term_2 = ( ( term_1 * y * cz * d) / 6.0 ) + x;
term_3 = ( ( term_2 * d) / 4.0 ) -cz;
y= ( term_3 * sine_of_y * d) + tangent_u;
if ( Math.abs(y - c) > eps )
{
exit_loop = false;
}
else
{
exit_loop = true;
}
}
heading_from_point_2_to_point_1_in_radians = ( cu * cosine_of_y * cosine_of_direction ) - ( su * sine_of_y );
c = r * Math.sqrt( ( sa * sa ) + ( heading_from_point_2_to_point_1_in_radians * heading_from_point_2_to_point_1_in_radians ) );
d = ( su * cosine_of_y ) + ( cu * sine_of_y * cosine_of_direction );
double point_2_latitude_in_radians = atan2(d, c);
c = ( cu * cosine_of_y ) - ( su * sine_of_y * cosine_of_direction );
x = atan2( sine_of_y * sine_of_direction, c);
c = ( ( ( ( ( -3.0 * c2a ) + 4.0 ) * m_Flattening ) + 4.0 ) * c2a * m_Flattening ) / 16.0;
d = ( ( ( (e * cosine_of_y * c) + cz ) * sine_of_y * c) + y) * sa;
double point_2_longitude_in_radians = ( point_1_longitude_in_radians + x) - ( ( 1.0 - c) * d * m_Flattening );
heading_from_point_2_to_point_1_in_radians = atan2( sa, heading_from_point_2_to_point_1_in_radians ) + Math.PI;
latitude_in_degrees = Math.toRadians( point_2_latitude_in_radians );
longitude_in_degrees = Math.toRadians( point_2_longitude_in_radians );
}
public double atan2(double y, double x) {
double coeff_1 = Math.PI / 4d;
double coeff_2 = 3d * coeff_1;
double abs_y = Math.abs(y)+ 1e-10f;
double r, angle;
if (x >= 0d) {
r = (x - abs_y) / (x + abs_y);
angle = coeff_1;
} else {
r = (x + abs_y) / (abs_y - x);
angle = coeff_2;
}
angle += (0.1963f * r * r - 0.9817f) * r;
return y < 0.0f ? -angle : angle;
}
private Vector fetchVenues(double max_lat, double min_lat, double max_lon, double min_lon)
{
return new Vector();
}
private class LocationListenerImpl implements LocationListener {
public void locationUpdated(LocationProvider provider, Location location) {
if(location.isValid()) {
nearBy.this.longitude = location.getQualifiedCoordinates().getLongitude();
nearBy.this.latitude = location.getQualifiedCoordinates().getLatitude();
//double altitude = location.getQualifiedCoordinates().getAltitude();
//float speed = location.getSpeed();
}
}
public void providerStateChanged(LocationProvider provider, int newState) {
// MUST implement this. Should probably do something useful with it as well.
}
}
}
请原谅这个烂摊子。我有用户lat长硬编码,因为我还没有GPS功能。您可以看到SQL查询已注释掉,以了解我计划如何使用最小和最大lat和long值。
感谢任何帮助。
由于
答案 0 :(得分:2)
如果您的目标是OS 4.6及更高版本,则会在MathUtilities类中添加atan2方法。否则,我建议只是谷歌搜索,直到你找到一个有效的实现。正如Matthew Flaschen所建议的那样,将结果与J2SE或PHP中标准atan2函数的结果进行比较。
答案 1 :(得分:0)
atan2并不难实现。定义中给出了基于2个参数返回的定义。从技术上讲,与技术上可实现的相比,自己进行划分可能会有一点点精确损失,但我认为它不会对rpactice产生影响。