我已经给出了纬度和经度的坐标,我想用它们创建一个位置对象。但是没有一个构造函数可以接受两个double类型的参数来创建位置,所以我尝试了以下方法:
Location l = null;
l.setLatitude(lat);
l.setLongitude(lon);
但是它会崩溃。还有其他的想法吗?
我已经给出了纬度和经度的坐标,我想用它们创建一个位置对象。但是没有一个构造函数可以接受两个double类型的参数来创建位置,所以我尝试了以下方法:
Location l = null;
l.setLatitude(lat);
l.setLongitude(lon);
只需使用 new Location("reverseGeocoded"); 创建一个新的位置,就像这样:
GeoPoint newCurrent = new GeoPoint(59529200, 18071400);
Location current = new Location("reverseGeocoded");
current.setLatitude(newCurrent.getLatitudeE6() / 1e6);
current.setLongitude(newCurrent.getLongitudeE6() / 1e6);
current.setAccuracy(3333);
current.setBearing(333);
这个错误是因为你不能在不存在的对象上调用方法。
我猜你是在谈论android.location.Location?
通常,这是由Android的各种定位服务返回的。你想用它做什么?
你想对它进行反向地理编码吗?也就是为那个地理坐标找到一个地址?
还是你想将其作为“虚假”位置并提供给其他应用程序使用?
顺便说一下,有两个构造函数。一个接受定位服务的名称,另一个是复制构造函数,接受现有的位置。
所以你可以像这样创建一个位置:
Location l = new Location("network");
Location l = new Location("any string");
l.setLatitude(lat);
l.setLongitude(lon);
public double lat;
public double lon;
public void onLocationChanged(Location loc)
{
lat=loc.getLatitude();
lon=loc.getLongitude();
Toast.makeText(getBaseContext(),"Latitude:" + lat +Longitude"+lon,Toast.LENGTH_LONG).show();
}