0

I'm using google maps to plot markers on a map. I can save the data for ALL these points (it's over 17000 rows with 3 columns: shopId,shopName,lat,long).

I can also send JSON queries specifying my lat/long and the radius at what shops around I want data about. Then I'll receive the data back. This works, but when I create the markers (with AsyncTask) freezing occurs in the app (and it is noticeable).

This is the code I'm using to generate the custom markers on Google maps:

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            try {
                JSONArray jsonArray = new JSONArray(result);
                String finalReturn[] = result.split("\\r?\\n");
                if(jsonArray.get(0).toString().equals("4")) {

                    for (int i = 1; i < finalReturn.length; i++) {
                        jsonArray = new JSONArray(finalReturn[i]);
                       IconGenerator iconGenerator = new IconGenerator(getApplicationContext());
                       iconGenerator.setStyle(IconGenerator.STYLE_RED);
                        iconGenerator.setRotation(90);
                        iconGenerator.setContentRotation(-90);
                        Bitmap iconBitmap = iconGenerator.makeIcon(jsonArray.get(5).toString());

                        Marker marker = mMap.addMarker(new MarkerOptions()
                                .position(new LatLng(jsonArray.getDouble(6), jsonArray.getDouble(7)))
                                .icon(BitmapDescriptorFactory.fromBitmap(iconBitmap)));
                        marker.setTitle(jsonArray.getString(1));
                        marker.setSnippet(jsonArray.getString(2) + " " + jsonArray.getString(8));
                    }
                }
            } catch (JSONException e) {

            }

My question is, what is the best solution here, store the points in a MySQL server and generate nearest shops from that area (SQlite Getting nearest locations (with latitude and longitude) something like this), or always query the server for the data. Or maybe a hybrid of both (query the server, then save the data in an SQLite db.)

I'm only a beginner in Android so sorry if this question is simple.

Community
  • 1
  • 1

1 Answers1

0

The fastest way should be to save the data in an SQLite db and query it from there, but if you only need the few shops that are near the user, it should be fine to simply call the web service every time.

Other than that, the freezing that occurs in your app is most likely due to the onPostExecute Method being called in the UI-Thread and you doing heavy work in this method.

You should not parse your JSON there, but rather in the doInBackground method and for each parsed element call publishProgress that calls the onProgressUpdate Method (which is also executed in the UI-Thread.

Like this, you can handle setting one single marker on the map at a time and that way, the time between the single onProgressUpdate calls can be used by the system to update the UI and so the freezing should no longer occur.

It should look somewhat like this:

 protected Void doInBackground(...) {
     String result = getResult();
     try {
            JSONArray jsonArray = new JSONArray(result);
            String finalReturn[] = result.split("\\r?\\n");
            if(jsonArray.get(0).toString().equals("4")) {

                for (int i = 1; i < finalReturn.length; i++) {
                    jsonArray = new JSONArray(finalReturn[i]);
                    publishProgress(jsonArray);
                }
            }
     } catch (JSONException e) {
          //handle error
        }
 }

 @Override
 protected void onProgressUpdate(JSONArray... progress) {
     JSONArray array = progress[0];
     IconGenerator iconGenerator = new IconGenerator(getApplicationContext());
     iconGenerator.setStyle(IconGenerator.STYLE_RED);
     iconGenerator.setRotation(90);
     iconGenerator.setContentRotation(-90);
     Bitmap iconBitmap = iconGenerator.makeIcon(jsonArray.get(5).toString());

     Marker marker = mMap.addMarker(new MarkerOptions()
                      .position(new LatLng(jsonArray.getDouble(6), jsonArray.getDouble(7)))
                      .icon(BitmapDescriptorFactory.fromBitmap(iconBitmap)));
     marker.setTitle(jsonArray.getString(1));
     marker.setSnippet(jsonArray.getString(2) + " " + jsonArray.getString(8));
 }
tknell
  • 9,007
  • 3
  • 24
  • 28