【发布时间】:2014-05-23 18:07:09
【问题描述】:
我在我的 android 项目中使用 google Map V2。我在 Map 中使用了多个标记,并且我使用了 InfoWindowAdapter,对于每个标记,我都会从数据库中恢复标记的位置和图片。但问题是只出现了一张图片(与最后一个标记有关)。
这是我的代码的一部分:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_geolocalisation);
Intent i = getIntent();
String LesAgents = (String)i.getSerializableExtra("LesAgentAssocies");
try {
json = new JSONArray(LesAgents);
} catch (JSONException e) {
e.printStackTrace();
}
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
for (int j = 0; j < json.length(); j++) {
initImageLoader();
markers = new Hashtable<String, String>();
try {
JSONObject agent = json.getJSONObject(j);
String pseudo = agent.getString("pseudo");
double longitude =
Double.parseDouble(agent.getString("longitude"));
double latitude =
Double.parseDouble(agent.getString("latitude"));
String image = agent.getString("image");
String imagePath = image.substring(1, image.length());
markers = new Hashtable<String, String>();
imageLoader = ImageLoader.getInstance();
options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.ic_launcher)
.showImageForEmptyUri(R.drawable.ic_launcher)
.cacheInMemory()
.cacheOnDisc().bitmapConfig(Bitmap.Config.RGB_565).build();
if (map!=null){
map.setInfoWindowAdapter(new CustomInfoWindowAdapter());
Position = new LatLng(longitude, latitude);
marker = map.addMarker(new MarkerOptions().position(Position)
.title(pseudo));
markers.put(marker.getId(), "http://"+getString(R.string.ip)+imagePath);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(Position, 15));
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
private class CustomInfoWindowAdapter implements InfoWindowAdapter {
private View view;
public CustomInfoWindowAdapter() {
view =
getLayoutInflater().inflate(R.layout.custom_info_window,
null);
}
@Override
public View getInfoContents(Marker marker) {
if (GeolocalisationActivity.this.marker != null
&&
GeolocalisationActivity.this.marker.isInfoWindowShown()) {
GeolocalisationActivity.this.marker.hideInfoWindow();
GeolocalisationActivity.this.marker.showInfoWindow();
}
return null;
}
@Override
public View getInfoWindow(final Marker marker) {
GeolocalisationActivity.this.marker = marker;
String url = null;
if (marker.getId() != null && markers != null && markers.size() > 0) {
if ( markers.get(marker.getId()) != null &&
markers.get(marker.getId()) != null) {
url = markers.get(marker.getId());
}
}
final ImageView image = ((ImageView)
view.findViewById(R.id.badge));
if (url != null && !url.equalsIgnoreCase("null")
&& !url.equalsIgnoreCase("")) {
imageLoader.displayImage(url, image, options,
new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(String imageUri,
View view, Bitmap loadedImage) {
super.onLoadingComplete(imageUri, view,
loadedImage);
getInfoContents(marker);
}
});
} else {
image.setImageResource(R.drawable.ic_launcher);
}
final String title = marker.getTitle();
final TextView titleUi = ((TextView)
view.findViewById(R.id.title));
if (title != null) {
titleUi.setText(title);
} else {
titleUi.setText("");
}
final String snippet = marker.getSnippet();
final TextView snippetUi = ((TextView) view
.findViewById(R.id.snippet));
if (snippet != null) {
snippetUi.setText(snippet);
} else {
snippetUi.setText("");
}
return view;
}
}
private void initImageLoader() {
int memoryCacheSize;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR) {
int memClass = ((ActivityManager)
getSystemService(Context.ACTIVITY_SERVICE))
.getMemoryClass();
memoryCacheSize = (memClass / 8) * 1024 * 1024;
} else {
memoryCacheSize = 2 * 1024 * 1024;
}
final ImageLoaderConfiguration config = new
ImageLoaderConfiguration.Builder(
this).threadPoolSize(5)
.threadPriority(Thread.NORM_PRIORITY - 2)
.memoryCacheSize(memoryCacheSize)
.memoryCache(new FIFOLimitedMemoryCache(memoryCacheSize-
1000000))
.denyCacheImageMultipleSizesInMemory()
.discCacheFileNameGenerator(new Md5FileNameGenerator())
.tasksProcessingOrder(QueueProcessingType.LIFO).enableLogging()
.build();
ImageLoader.getInstance().init(config);
}
请帮忙!
【问题讨论】:
标签: android google-maps android-maps-v2