0

我有一个片段,其中加载了谷歌地图上的所有商店位置。但现在我只想加载距离当前位置 5 英里范围内的商店。

这是我的代码:

public class NearMeFragment extends Fragment implements GooglePlayServicesClient.ConnectionCallbacks,
    GooglePlayServicesClient.OnConnectionFailedListener, LocationListener {

        GoogleMap map;
        LatLng latlng;
        private LocationRequest lr;
        private LocationClient lc;
        MapFragment mapFragment;
        //ImageView iv;
        private static View view;
        Response response;

    public NearMeFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        if (view != null) {
            ViewGroup parent = (ViewGroup) view.getParent();
            if (parent != null)
                parent.removeView(view);
        }


        File file = new File( Environment.getExternalStorageDirectory() + "/json.txt");
            if(file.exists()) {
                try{
                       Reader inputStreamReader = new InputStreamReader(new FileInputStream(file));

                       Gson gson = new Gson();
                       this.response = gson.fromJson(inputStreamReader, Response.class);
                    } catch (FileNotFoundException e) {
                       e.printStackTrace();
                    } catch (@SuppressWarnings("hiding") IOException e){
                       e.printStackTrace();
                    }
            }else{
                System.out.println("Error");
            }

        try {
            view = inflater.inflate(R.layout.map_near_me, container,
                    false);

            mapFragment = ((MapFragment) this.getActivity()
                    .getFragmentManager().findFragmentById(R.id.map));

            map = mapFragment.getMap();
            map.getUiSettings().setAllGesturesEnabled(true);
            map.getUiSettings().setMyLocationButtonEnabled(true);
            map.setMyLocationEnabled(true);
            map.getUiSettings().setZoomControlsEnabled(true);

            for(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()));
                }
            }
            MapsInitializer.initialize(this.getActivity());
        } catch (GooglePlayServicesNotAvailableException e) {
            Toast.makeText(getActivity(), "Google Play Services missing !",
                    Toast.LENGTH_LONG).show();
        } catch (InflateException e) {
            Toast.makeText(getActivity(), "Problems inflating the view !",
                    Toast.LENGTH_LONG).show();
        } catch (NullPointerException e) {
            Toast.makeText(getActivity(), "Google Play Services missing !",
                    Toast.LENGTH_LONG).show();
        }

        return view;
        }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        lr = LocationRequest.create();
        lr.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        lc = new LocationClient(this.getActivity().getApplicationContext(), this, this);
        lc.connect();
    }

    @Override
    public void onLocationChanged(Location l2) {
        CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(l2.getLatitude(), l2.getLongitude()), 15);
        map.animateCamera(cameraUpdate);
    }

    @Override
    public void onConnectionFailed(ConnectionResult arg0) {

    }

    @Override
    public void onConnected(Bundle connectionHint) {
        lc.requestLocationUpdates(lr, this);

    }

    @Override
    public void onDisconnected() {

    }
}

此代码运行良好,但需要时间加载。我认为我的问题可以通过使用一些 if 语句来解决。但想不通怎么弄。有人可以帮我解决吗?此外,每当我启动此活动/片段时,我都会看到非洲的其他一些国家。我如何更改为默认英国?

4

2 回答 2

0

我认为你可以使用 Location 类的 distanceTo() 或 distanceBetween() 方法来查找每个商店与当前位置之间的距离。在你的 if 语句中使用它 for Shop 循环。

于 2013-10-25T11:40:01.183 回答
0

这可能会帮助您:

http://mobile.tutsplus.com/tutorials/android/android-sdk-working-with-google-maps-displaying-places-of-interest/

从教程的第 3 页开始。另外,查看那个人实现的查询字符串:

String placesSearchStr = "https://maps.googleapis.com/maps/api/place/nearbysearch/" +
                "json?location="+lat+","+lng+
                "&radius=1000&sensor=true" +
                "&key=AIzaSyAXxxoI2aDYyXTUdu3eOTXayrOcWB6F-_I";//ADD KEY

更改半径以满足您的需要。请注意,半径值以 METRES 为单位。因此,您 5 英里的值将类似于 8046.72(根据谷歌,不确定小数位是否有效)。

我希望这会有所帮助。

于 2013-10-02T23:24:56.957 回答