从Android地图中的当前位置加载最近的商店

时间:2014-01-14 17:07:42

标签: android json google-maps google-maps-android-api-2 google-maps-api-2

我需要一些帮助来装载最近的商店。目前我实施了从json加载所有商店,其中包含lat&长。但现在我想从当前位置加载5公里范围内的所有商店。有些人建议我使用distanceTo(),但我无法实现它。请有人帮忙解决这个问题吗?

以下代码从json加载所有商店(工作正常):

for(final Shop shop : this.response.shops){
            for(int i = 0; i < shop.getShopLat().size(); i++){
                map.addMarker(new MarkerOptions().position(new LatLng(Double.parseDouble(shop.getShopLat().get(i)), Double.parseDouble(shop.getShopLng().get(i)))).title(shop.getName()));
                map.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {

                    @Override
                    public View getInfoContents(Marker marker) {
                        // TODO Auto-generated method stub
                        return null;
                    }

                    @Override
                    public View getInfoWindow(Marker marker) {
                        // TODO Auto-generated method stub
                        return null;
                    }

                });

            }
        }

编辑:

LocationManager service = (LocationManager)     getActivity().getSystemService(getActivity().LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String provider = service.getBestProvider(criteria, false);
    Location userLocation = service.getLastKnownLocation(provider);

2 个答案:

答案 0 :(得分:1)

你可以使用google api来实现这个目标

StringBuilder sb = new StringBuilder(
            "https://maps.googleapis.com/maps/api/place/nearbysearch/json?");
    sb.append("location=" + latitude + "," + longitude);
    sb.append("&radius=5000");
    sb.append("&types=" + placeType);
    sb.append("&sensor=true");
    sb.append("&key=Your Api Key");

here是一个很好的链接,你可以按照这个

答案 1 :(得分:0)

首先,您必须获取用户位置,有很多方法可以实现这一点,并且如果您想获得最佳和最准确的位置,它并不像看起来那么简单,请检查this主题,真的很有帮助,之后我认为你应该使用这样的东西:

float[] results = new float[3];
Location.distanceBetween(userLocation.getLatitude(), userLocation.getLongitude(), Double.parseDouble(shop.getShopLat().get(i), Double.parseDouble(shop.getShopLng().get(i)), results);
//results[0] = distance in meters

所以只需将一个if放在for语句中。

for(final Shop shop : this.response.shops){
   for(int i = 0; i < shop.getShopLat().size(); i++){
    float[] results = new float[3];
        Location.distanceBetween(userLocation.getLatitude(), userLocation.getLongitude(), Double.parseDouble(shop.getShopLat().get(i), Double.parseDouble(shop.getShopLng().get(i)), results);
        if(results[0]<=5000)
        {   
                    map.addMarker(new MarkerOptions().position(new LatLng(Double.parseDouble(shop.getShopLat().get(i)), Double.parseDouble(shop.getShopLng().get(i)))).title(shop.getName()));
                    map.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter()     
               {    
                    @Override
                    public View getInfoContents(Marker marker) {
                        // TODO Auto-generated method stub
                        return null;
                    }

                    @Override
                    public View getInfoWindow(Marker marker) {
                        // TODO Auto-generated method stub
                        return null;
                    }

                });

            }
          }
        }