我移动时如何改变标记的颜色(在500米范围内)

时间:2017-10-13 00:01:26

标签: android google-maps google-maps-markers

我是开发Android应用的初学者。

我通过网络服务检索数据并在地图上放置标记,现在我想知道如何将标记(蓝色标记:“BitmapDescriptorFactory.fromResource(R.drawable.blue_marker)”更改为橙色标记:“BitmapDescriptorFactory .fromResource .drawable.orange_marker)“)当我移动时,如果这些标记在半径小于500米的范围内。我的第一个代码工作但我不得不离开活动,然后回来让更改生效。我希望它是动态的。

    ArrayList<StopModel> stopList;
    ArrayList<StopModel> a;
    Marker m;
    ArrayList<Marker> mList;
    Marker locationMarker;
    Location mLastLocation;

    public class Stuff extends AppCompatActivity implements OnMapReadyCallback,
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener, LocationListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        /*
        * lots of stuff here
        */


        mList = new ArrayList<>();
        a = new ArrayList<>();


        try {

            //retrieving markers
            a = new GetStops() {
            }.execute().get();

        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
    }


        /*
        * lots of stuff here
        */


    @Override
    public void onLocationChanged(Location location) {

        mLastLocation = location;

        mGoogleMap.clear();
        LatLng ll = new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude());

        for (int i = 0; i < a.size(); i++) {
            m = createMarker(a.get(i).getPosition(), a.get(i).getArretCode(), a.get(i).getQuartier());
            mList.add(m);
        }

        //I'm trying to use the mList to change markers color when I'm nearby
        changeMarkerColor(ll, mList);


    }


    private class GetStops extends AsyncTask<Void, Void, ArrayList<StopModel>> {

        @Override
        protected ArrayList<StopModel> doInBackground(Void... voids) {

            HttpHandler httpHandler = new HttpHandler();
            String jsonString = null;


            String url = myWebServiceURL;
            jsonString = httpHandler.makeServiceCall(url);

            try {

                JSONArray jsonArray = new JSONArray(jsonString);

                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);

                    StopModel stopModel = new StopModel();

                    stopId = jsonObject.getString("stopId");
                    stopCode = jsonObject.getString("stopCode");
                    stopPosition = new LatLng(jsonObject.getDouble("stopLat"), jsonObject.getDouble("stopLong"));

                    stopModel.setId(stopId);
                    stopModel.setCode(stopCode);
                    stopModel.setPosition(stopPosition);


                    stopList.add(stopModel);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return stopList;
        }

        @Override
        protected void onPostExecute(ArrayList<stopModel> stopModels) {
            super.onPostExecute(stopModels);

        }
    }


    protected Marker createMarker(LatLng latLng, String title, String snippet) {


        locationMarker = mGoogleMap.addMarker(new MarkerOptions()
                .position(latLng)
                .anchor(0.5f, 0.5f)
                .title(title)
                .snippet(snippet)
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.blue_marker)));

        return locationMarker;
    }

    void changeMarkerColor(LatLng myPos, ArrayList<Marker> markerList) {
        for (Marker m : markerList)
            if (SphericalUtil.computeDistanceBetween(myPos, m.getPosition()) < 500) {
                m.setTitle(getDistance(myPos, m.getPosition()));
                showToast(getDistance(myPos, m.getPosition()));
                m.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.orange_marker));

            }

    }

 }

0 个答案:

没有答案