【发布时间】:2014-11-26 22:13:41
【问题描述】:
我正在将标记加载到谷歌地图,它的性能非常糟糕:加载地图和 40 个标记大约需要 5 秒。(在加载地图和标记之前,屏幕是空白的)
这是我的 xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
</RelativeLayout>
以下代码为地图添加标记:
1) 迭代所有 Restaurant 对象,获取每个餐厅的地址。
2) 将地址转换为 LatLng 对象
3) 添加到标记
//partial of the method
for (Restaurant rest : rests) {
LatLng ll = getLatLng(rest.getAddress());//converts address to LatLng
MarkerOptions marker = new MarkerOptions()
.title(rest.getName())
// .snippet("Briefly description: " + i++)
.position(ll)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker))
.anchor(0.0f, 1.0f);
myMap.addMarker(marker);
}
public LatLng getLatLng(String address) {
try {
ArrayList<Address> addresses = (ArrayList<Address>) geocoder.getFromLocationName(address, MAX_ADDRESS_ALLOWDED);
for (Address add : addresses) {
if (true) {//Controls to ensure it is right address such as country etc.
longitude = add.getLongitude();
latitude = add.getLatitude();
}
}
} catch (Exception e) {
Toast.makeText(myContext, e.getMessage(), Toast.LENGTH_SHORT).show();
}
return new LatLng(latitude, longitude);
}
我正在使用AsyncTask 的onPostExecute 方法添加标记,有人可以帮我吗?
【问题讨论】:
-
我建议在查找每个地址后缓存它们的纬度和经度,以防止每次都加载它们。地理编码器请求将被阻止并且可能需要很长时间。
标签: android google-maps-android-api-2