Google Maps API v2 HeatMap Won't Reliably Display

不想你离开。 提交于 2019-12-11 03:38:59

问题


Ok so I'm working on implementing a heatmap off of data pulled from a server. I'm basing it on the official Google heatmap api and I've based my code off of their code demos. However, my code doesn't work. Sometimes it displays the heatmaps but more often than not it displays nothing. I've double checked to make sure the data is coming in, so lack of data isn't a problem. I can play makers on the map fine, it just seems that heatmaps, or maybe overlays, dont work properly. mOverlay and mProvider are declared as private variables and are instantiated in the method below. I'm at a loss for ideas as to why it isn't showing up. Included below is my entire method for getting and displaying heatmaps.

 private class sendBox extends AsyncTask<String, Void, HttpResponse> {

    @Override
    protected HttpResponse doInBackground(String... urls) {
        try {
            DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(urls[0]);
            for(String s : Constants.COOKIES){
                httppost.addHeader("Cookie", s);
            }

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);


            //top_lat bot_lat left_long right_long
            nameValuePairs.add(new BasicNameValuePair("top_lat", Constants.TOP_LAT+""));
            nameValuePairs.add(new BasicNameValuePair("bot_lat", Constants.BOT_LAT+""));
            nameValuePairs.add(new BasicNameValuePair("left_long", Constants.LEFT_LONG+""));
            nameValuePairs.add(new BasicNameValuePair("right_long", Constants.RIGHT_LONG+""));
            //possibly change that to latlng variable on top

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            // Execute HTTP Post Request
            httpclient = new DefaultHttpClient();
            return httpclient.execute(httppost);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("HTTP error ");

        }
        return null;
    }
    protected void onPostExecute(HttpResponse result) {
        InputStream is = null;
        JSONObject jObj = null;
        String json = "";

        if(result !=null){ //checks if there actually is a response
            HttpEntity httpEntity = result.getEntity();

            System.out.println(result.getStatusLine().getStatusCode());
            try {
                is = httpEntity.getContent();
            }catch (Exception e) {
                Log.e("IS", "Error converting result " + e.toString());
            }
        }
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close(); //reads the json object as string
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try to parse the string to a JSON object
        try {
            jObj = new JSONObject(json); //converts string to a json object, mad errors if it isn't json object
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }
        try {
            JSONArray data =(JSONArray) jObj.get("data");
            ArrayList<WeightedLatLng> list = new ArrayList<WeightedLatLng>();
            for(int i = 0; i < data.length(); i++) {
                JSONObject temp = (JSONObject) data.get(i);
                Log.d("data", temp.get("latitude") + ","+temp.get("longitude")+" " + temp.get("weight").toString());
                WeightedLatLng lat = new WeightedLatLng(new LatLng(Double.parseDouble(temp.get("latitude").toString()),
                        Double.parseDouble(temp.get("longitude").toString())), Double.parseDouble(temp.get("weight").toString()));
                list.add(lat);
            }

            // Gradient whatnot
            int[] colors = {
                    Color.rgb(102, 0, 255), // green
                    Color.rgb(255, 0, 0)    // red
            };
            float[] startPoints = {
                    0.2f, 1f
            };
            Gradient gradient = new Gradient(colors, startPoints);
            // Create the tile provider.
            if(list.size()>0) {
                if (mProvider != null) {
                    mProvider.setWeightedData(list);
                    mOverlay.clearTileCache();
                    Log.d("Is Null", "False");
                } else {
                    mProvider = new HeatmapTileProvider.Builder()
                            .weightedData(list)
                            .gradient(gradient)
                            .build();
                    mProvider.setRadius(50);
                    mOverlay = map.addTileOverlay(new TileOverlayOptions().tileProvider(mProvider));
                    Log.d("Is Null", "True");
                }
            }
            canRefresh=true;
        } catch (Exception e) {

            e.printStackTrace();
        }
    }

来源:https://stackoverflow.com/questions/23035863/google-maps-api-v2-heatmap-wont-reliably-display

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!