Question

I have a set of points in a text file that I want to plot on a map (API v2) and draw a line through. Each of the points is a <Lat, Lng> and there are a total of 7253 such points in the text file. The code is as follows:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map_my_route_mock);

        //Step 0. Get google map instance.
        map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

        if(map == null) {
            Toast.makeText(getApplicationContext(), "Map is not available.", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(getApplicationContext(), "Map is available.", Toast.LENGTH_LONG).show();
        }

        //Step 0.a. Load a type of map.
        map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);

        //Step 0.b. Load your current location on the map.
        map.setMyLocationEnabled(true);
        if(po == null) {
            po = new PolylineOptions();
        }
        //Toast.makeText(getApplicationContext(), "Location lat = " + loc.getLatitude() + " and longitude = " + loc.getLongitude(), Toast.LENGTH_LONG).show();

        //Step 1. Set GPS to service provider.
        locMgr = (LocationManager) getSystemService(LOCATION_SERVICE);
        mocLocProvider = locMgr.GPS_PROVIDER;
        locMgr.addTestProvider(mocLocProvider, false, false, true, false, true, false, false, 0, 5);
        locMgr.setTestProviderEnabled(mocLocProvider, true);
        //locMgr.requestLocationUpdates(mocLocProvider, 0, 0, locLstnr);

        //Step 2. Open file for reading from.
        try {
            is = getAssets().open("locationLogs.txt");
            br = new BufferedReader(new InputStreamReader(is));
            String line = null;
            try {
                line = br.readLine();
                while(line != null) {
                //while((line = br.readLine()) != null) {
                    //there is still a line in the file. parse for gps coordinates etc.
                    Location l = new Location(LocationManager.GPS_PROVIDER);
                    String[] details = line.split(",");  //the array will contain date, time, lat, long, speed, altitude and accuracy.
                    l.setTime(System.currentTimeMillis());
                    l.setLatitude(Double.parseDouble(details[2]));
                    l.setLongitude(Double.parseDouble(details[3]));
                    l.setSpeed((float) Double.parseDouble(details[4]));
                    l.setAltitude(Double.parseDouble(details[5]));
                    l.setAccuracy((float) Double.parseDouble(details[6]));
                    //Toast.makeText(getApplicationContext(), l.getLatitude() + "," + l.getLongitude() + "," + l.getSpeed() + "," + l.getAltitude() + "," + l.getAccuracy() + "\n", Toast.LENGTH_SHORT).show();
                    locMgr.setTestProviderLocation(mocLocProvider, l);
                    po.add(new LatLng(l.getLatitude(), l.getLongitude()));
                    Log.v(this.toString(), "Number of po objects = " + po.getPoints().size());
                    //pl = map.addPolyline(po);
                    //Log.v(this.toString(), "number of polyline objects added = " + pl.getPoints().size());
                    line = br.readLine();
                }
            } catch(FileNotFoundException e) {
                Log.v(this.toString(), "File not found.");
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            Log.v(this.toString(), "Cannot open file for reading from.");
        }
}

After reading about 1300 points, the application collapses with an OutOfMemory exception. Though there are a whole host of threads dealing with memory leakage on Google Maps API v2, closer analysis with MAT reveals that the line: pl = map.addPolyline(po) is the culprit, hogging most of the memory. This turns out to be true, since after that line is commented out, the memory footprint of the above code is very small ~8MB for about 7k points read.

My question(s):
1. Is there anything wrong with the way Polylines object is being used to draw on the map? Once for every update?
2. If so, how can a line be drawn such that it does not take up too much memory? With Canvas and the like or drawing a line only after a certain number of points (say, 10 or so)?
3. Some developers who have used Maps API v2 in their application can maybe shed some light on the proper way to do this?

Was it helpful?

Solution

Call pl = map.addPolyline(po); after the loop to create one polyline and not to try to create 7000 polylines each one "one point longer" than the one before.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top