好的,我正在努力实现从服务器提取的数据的热图。我将它基于官方的Google热图api,并且我的代码基于他们的代码演示。但是,我的代码不起作用。有时它会显示热图,但通常不会显示任何内容。我已经仔细检查以确保数据进入,因此缺少数据不是问题。我可以很好地在地图上玩制造商,它似乎只是热图,或者可能是叠加,不能正常工作。 mOverlay和mProvider声明为私有变量,并在下面的方法中实例化。我不知道为什么它没有出现。下面是我获取和显示热图的完整方法。
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();
}
}