0

我正在研究谷歌地图,我已经设法在地图上放置了大约 73 个来自 sqllite DB 的精确点,它们为当前用户城市的商店提供了一个地点。

现在我只需要向用户展示壁橱的五个地方,而不是所有 73 个地方。有没有人对此有想法或解决方案。

4

2 回答 2

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);
    }
}
于 2012-11-21T07:11:43.740 回答
0

首先,计算用户位置与所有地点之间的距离。

要获取用户位置,请阅读本指南位置策略

为您的位置创建一个类,并使用一种方法来计算它与用户位置之间的距离:

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方法被调用:阅读distanceTo

现在,创建Place[]数组并使用数据库中的所有位置对其进行初始化。对于每个地方调用calculateDistance方法。

之后,使用Arrays.sortArrays.sort)对数组进行排序

仅在地图上显示阵列中的前五个位置

完毕!

于 2012-11-21T07:40:33.633 回答