我正在Google地图上工作,我已经设法从地图上的sqllite数据库中放置了大约73个精确点,它们为当前用户城市的商店提供了一个位置。
现在我只需要向用户展示五个位置而不是所有73个位置。任何人都有这个想法或解决方案。
答案 0 :(得分:0)
public Location geoLat(GeoPoint userGeopoint)
{
Location location = new Location("");
location.setLatitude(userGeopoint.getLatitudeE6()/1E6);
return location;
}
public Location geoLng(GeoPoint userGeopoint)
{
Location location = new Location("");
location.setLatitude(userGeopoint.getLongitudeE6()/1E6);
return location;
}
public GeoPoint userGeopoint(Location location)
{
Double userLat = location.getLatitude()*1E6;
Double userLng = location.getLongitude()*1E6;
GeoPoint point = new GeoPoint(userLat.intValue(), userLng.intValue());
return point;
}
private void nearRSU(Location location)
{
Location userlocation = new Location("point A");
Location objectlocation = new Location("point B");
userlocation.setLatitude(geoLat(userGeopoint(location())).getLatitude());
userlocation.setLongitude(geoLng(userGeopoint(location())).getLongitude());
double rangeconv = 0, ctrange = 0, sumrange = 0, alltimes = 0;
String rgtype = "", tmtype = "";
int a = 0;
String objectaddress = null;
GeoPoint pointB = null;
for (int i = 0 ; i < listLocRSU.size(); i++)
{
GeoPoint pointA = new GeoPoint((int) (listLocRSU.get(i).lat * 1E6),
(int) (listLocRSU.get(i).lng * 1E6));
objectlocation.setLatitude(pointA.getLatitudeE6()/1E6);
objectlocation.setLongitude(pointA.getLongitudeE6()/1E6);
double range = userlocation.distanceTo(objectlocation);
if (range >= 1000)
{
rangeconv = range / 1000;
rgtype = " km";
}
else
{
rangeconv = range;
rgtype = " m";
}
double times = rangeconv / 40;
if (times >= 1)
{
alltimes = times;
tmtype = " h";
}
else
{
alltimes = times * 60;
tmtype = " min";
}
if (ctrange > rangeconv)
{
ctrange = rangeconv;
sumrange = ctrange;
a = i;
pointB = new GeoPoint ((int) (listLocRSU.get(a).lat * 1E6),
(int) (listLocRSU.get(a).lng * 1E6));
}
else if (ctrange < rangeconv)
{
sumrange = ctrange;
a = a;
pointB = new GeoPoint ((int) (listLocRSU.get(a).lat * 1E6),
(int) (listLocRSU.get(a).lng * 1E6));
}
double objLat = listLocRSU.get(a).lat;
double objLng = listLocRSU.get(a).lng;
Geocoder objectgc = new Geocoder(this, Locale.getDefault());
try
{
List<Address> addresses = objectgc.getFromLocation(objLat, objLng, 1);
StringBuilder objaddress = new StringBuilder();
if (addresses.size() > 0)
{
Address address = addresses.get(0);
address.getMaxAddressLineIndex();
objaddress.append(address.getAddressLine(0)).append(", ");
objaddress.append(address.getLocality()).append(", ");
objaddress.append(address.getCountryName()).append(", ");
objaddress.append(address.getPostalCode());
}
objectaddress = objaddress.toString();
}
catch (IOException e){}
List<Overlay> overlays = map.getOverlays();
Drawable marker = this.getResources().getDrawable(R.drawable.marker);
MyItemizedOverlay itemizedOverlay = new MyItemizedOverlay(marker, this);
OverlayItem overlayitem = new OverlayItem(pointB, listLocRSU.get(a).locname,
"Address:\n" + objectaddress +
"\n\nLongitude:\n" + listLocRSU.get(a).lng +
"\n\nLatitude:\n" + listLocRSU.get(a).lat +
"\n\nDistance:\n" + sumrange + rgtype +
"\n\nTime Calculation (40 km/h):\n" + alltimes + tmtype);
itemizedOverlay.addItem(overlayitem);
overlays.add(itemizedOverlay);
}
}
答案 1 :(得分:0)
首先,计算用户位置与所有地点之间的距离。
要获取用户位置,请阅读本指南Location strategies
使用方法为您的地点创建一个类来计算它与用户位置之间的距离:
public class Place {
public Location location;
private float distanceToUser;
public Place(Location _loc){
location = _loc;
}
public void calculateDistance(Location userLocation){
distanceToUser = location.distanceTo(userLocation);
}
}
此处调用distanceTo
方法:阅读about distanceTo
现在,创建Place[]
数组并使用数据库中的所有位置对其进行初始化。对于每个地方,请致电calculateDistance
方法。
之后,使用Arrays.sort
(Arrays.sort)
仅在地图上显示数组中的前五位
完成!