在我的代码中,我使用Google API从JSON数据加载标记。它一次性加载所有地图图标并且大约需要它。 2分钟加载这是很多时间。有没有办法加载屏幕区域中的标记 1.如何快速加载标记 2.图标加载没有停止。 3.如何制作集群并一次性加载
@Override
protected List < Product > doInBackground(Void...params) {
HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
DefaultHttpClient client = new DefaultHttpClient();
SchemeRegistry registry = new SchemeRegistry();
SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
registry.register(new Scheme("http", socketFactory, 443));
SingleClientConnManager mgr = new SingleClientConnManager(client.getParams(), registry);
DefaultHttpClient httpClient = new DefaultHttpClient(mgr, client.getParams());
// Set verifier
HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
HttpPost httpPost = new HttpPost("https://towncitycards.com/webservice_action.php?action=all_products");
URLConnection urlConn = null;
BufferedReader bufferedReader = null;
try {
URL url = new URL(this.serverUrl);
urlConn = url.openConnection();
bufferedReader = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
StringBuffer stringBuffer = new StringBuffer();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line);
}
JSONObject response = new JSONObject(stringBuffer.toString());
List < Product > products = new ArrayList < > ();
HashMap < String, Bitmap > iconsMap = new HashMap < > ();
try {
JSONArray productsJSON = response.getJSONArray("all_products");
for (int ixProduct = 0; ixProduct < productsJSON.length(); ixProduct++) {
JSONObject productJSON = productsJSON.getJSONObject(ixProduct);
String mapIconStr = productJSON.getString("map_icon");
URI uri = new URI(mapIconStr);
String[] segments = uri.getPath().split("/");
String iconName = segments[segments.length - 1];
// percetn-encode URL
String mapIconPath = mapIconStr.substring(0, mapIconStr.indexOf(iconName));
String iconUrlString = mapIconPath + URLEncoder.encode(iconName, "UTF-8");
// replace "http:" with "https:"
iconUrlString = iconUrlString.replace("http:", "https:");
Log.d("Icon Load ", "doInBackground: " + iconUrlString);
Bitmap bmp;
if (!iconsMap.containsKey(iconUrlString)) {
bmp = getBitmapFromURL(iconUrlString);
// populate map with unique icons
iconsMap.put(iconUrlString, bmp);
} else {
bmp = iconsMap.get(iconUrlString);
}
if (bmp != null) {
try {
Product product = new Product();
product.id = productJSON.getString("ID");
product.name = productJSON.getString("post_title");
product.lat = productJSON.getDouble("latitude");
product.lon = productJSON.getDouble("longitude");
id = product.id;
System.out.println("my Id stored" + id);
bmp = Bitmap.createScaledBitmap(bmp, (int)(bmp.getWidth() * 2.5), (int)(bmp.getHeight() * 2.5), true);
product.icon = bmp;
products.add(product);
} catch (Exception ignore) {}
}
}
} catch (JSONException ex) {
Log.e("App", "Failure", ex);
}
return products;
} catch (Exception ex) {
Log.e("App", "yourDataTask", ex);
return null;
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Override
protected void onPostExecute(List < Product > products) {
if (products != null) {
for (final Product product: products) {
Marker marker = googlemap.addMarker(new MarkerOptions()
.position(new LatLng(product.lat, product.lon))
.title(product.name)
.icon(BitmapDescriptorFactory.fromBitmap(product.icon)));
markerIds.put(marker, product.id);
}
if (mDialog != null && mDialog.isShowing()) {
mDialog.dismiss();
}
}
}
}
答案 0 :(得分:0)
您的代码需要很长时间才能使用您用于标记的位图,只是为了证明从执行后删除位图并检查时间。像这样
@Override
protected void onPostExecute(List < Product > products) {
if (products != null) {
Log.e("startTime", System.currentTimeMillis());
for (final Product product: products) {
Marker marker = googlemap.addMarker(new MarkerOptions()
.position(new LatLng(product.lat, product.lon))
.title(product.name)
markerIds.put(marker, product.id);
}
Log.e("endTime", System.currentTimeMillis());
if (mDialog != null && mDialog.isShowing()) {
mDialog.dismiss();
}
}
您是否尝试过git库https://github.com/googlemaps/android-maps-utils 它可以非常有效地绘制所有市场,而不会使手机变得缓慢而有效。