user1281921
user1281921

Reputation: 163

Maps android v2 - Issues with clear() method

I have created an android app that utilizes Google Maps and i am trying to optimize it because it runs really slow. My app works my loading a map at a default location and then adding markers of the places nearby. Whenever the user swipes the screen, the ParseJSON method is called and new markers of the user viewable region are added to the map. Before the new markers are added, the map is cleared by calling the .clear() method.

This part of the program works but when i click on a marker to display the info for that place it appears only for a second because the CameraChangeListener is triggered and the map is refreshed again.

I was wondering if there was a way to stop animations from triggering the CameraChangeListener or a way to store all of the places in a Hashmap. The API i'm using only lets you get the nearby places of 1 map coordinate.

public GoogleMap.OnCameraChangeListener getCameraChangeListener()
{
    return new GoogleMap.OnCameraChangeListener()
    {
        @Override
        public void onCameraChange(CameraPosition cameraPosition)
        {
            double lat = cameraPosition.target.latitude;
            double lng = cameraPosition.target.longitude;

            String url = "http://www.mywebsite.com/location/?lat=" + lat + "&lng=" + lng;
            new ParseJSON().execute(url);                   
           }
      }
 };


public class ParseJSON extends AsyncTask<String, Void, Void>
{
    @Override
    protected Void doInBackground(String... params)
    {
        try
        {
            //Parse JSON of places and put it into a Hashmap
        }
        catch (JSONException e)
        {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result)
    {
        mMap.clear(); //Clear map before placing new markers

        //Current user viewable region of the map
        LatLngBounds bounds = mMap.getProjection().getVisibleRegion().latLngBounds;

        //Loop through hash map of Places
        for(Places place : placesHash.values()) 
        {
            //If the Place is within the bounds of the screen
            if (bounds.contains(new LatLng(place.lat, place.lng)))
            {
                MarkerOptions markerOptions = new MarkerOptions();

                LatLng latLng = new LatLng(place.lat, place.lng);
                markerOptions.position(latLng);
                markerOptions.title(place.name);

                mMap.addMarker(markerOptions);
                System.out.println("marker added " + place.name);
            }
    }

Upvotes: 0

Views: 265

Answers (1)

Dandalf
Dandalf

Reputation: 2425

Instead of trying to stop the animation, you could just not clear the map.

Here's how you can implement a visible cache instead of clearing the map:

Keep track of which places are currently shown on the map along with the marker in a dictionary. If the place is in bounds, only add a new marker if the place doesn't exist in the dictionary. If the place is out of bounds, check the dictionary to see if it has a marker and if so, call the remove method on the marker and remove that place from the dictionary.

Then you'll get the nice animations and the info window will work as expected.

Upvotes: 1

Related Questions