我用自定义标记实现了android Mapview。我使用毕加索将图像加载到标记视图中。但是这些地图并没有向我展示我需要的标记。
这是我的代码
i = getIntent();
if(i.getBooleanExtra("maps", true)) {
mactionBar.setSubtitle(i.getStringExtra("nama"));
mMyMarkersArray.add(new MyMarker(i.getStringExtra("nama"), i.getStringExtra("deskripsi"), i.getStringExtra("foto"), i.getStringExtra("marker"), Double.parseDouble(i.getStringExtra("lat")), Double.parseDouble(i.getStringExtra("lng"))));
plotMarkers(mMyMarkersArray);
}
这里是加载图像标记的代码。我实施if(i.getBooleanExtra("maps", true))
因为有两个活动导致了这个地图视图。如果前一个活动设置了一个真值,那么它只会带来一个标记数据。但如果值为false,则会带来所有标记数据。 mapsview显示了我从前一个带来虚假值的活动中获得的所有标记。但是如果mapview来自带来真正价值的活动,那么它就不会给我看一个标记。
public void plotMarkers(ArrayList<MyMarker> markers) {
if(markers.size() > 0) {
for (MyMarker myMarker : markers)
{
markerOption = new MarkerOptions().position(new LatLng(myMarker.getmLatitude(), myMarker.getmLongitude()));
location_marker = mMap.addMarker(markerOption);
Target target = new PicassoMarker(location_marker);
targets.add(target);
Picasso.with(MapsActivity.this).load(myMarker.getmIcon()).resize(84, 125).into(target);
mMarkersHashMap.put(location_marker, myMarker);
i = getIntent();
if(i.getBooleanExtra("maps", true)) {
buttonNavigasi.setVisibility(View.VISIBLE);
location_marker.setTitle(i.getStringExtra("nama"));
dest = new LatLng(myMarker.getmLatitude(), myMarker.getmLongitude());
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(dest, 16));
}
else {
mMap.setInfoWindowAdapter(new MarkerInfoWindowAdapter());
}
}
}
}
以下是我如何调用MapsActivity的代码
buttonNavigasi = (AppCompatButton) findViewById(R.id.button2);
buttonNavigasi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(DetailActivity.this,MapsActivity.class);
i.putExtra("nama", daftarNama);
i.putExtra("deskripsi", daftarDeskripsi);
i.putExtra("foto", daftarFoto);
i.putExtra("marker", daftarMarker);
i.putExtra("lng", daftarLng);
i.putExtra("lat", daftarLat);
i.putExtra("maps", true);
startActivity(i);
}
});
这是plotMarkers的方法
public void plotMarkers(ArrayList<MyMarker> markers) {
if(markers.size() > 0) {
for (MyMarker myMarker : markers)
{
markerOption = new MarkerOptions().position(new LatLng(myMarker.getmLatitude(), myMarker.getmLongitude()));
location_marker = mMap.addMarker(markerOption);
Target target = new PicassoMarker(location_marker);
targets.add(target);
Picasso.with(MapsActivity.this).load(myMarker.getmIcon()).resize(84, 125).into(target);
mMarkersHashMap.put(location_marker, myMarker);
i = getIntent();
if(i.getBooleanExtra("maps", true)) {
buttonNavigasi.setVisibility(View.VISIBLE);
location_marker.setTitle(i.getStringExtra("nama"));
dest = new LatLng(myMarker.getmLatitude(), myMarker.getmLongitude());
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(dest, 16));
}
else {
mMap.setInfoWindowAdapter(new MarkerInfoWindowAdapter());
}
}
}
}
PicassoMarker课程
public class PicassoMarker implements Target {
Marker mMarker;
PicassoMarker(Marker marker) {
mMarker = marker;
}
@Override
public int hashCode() {
return mMarker.hashCode();
}
@Override
public boolean equals(Object o) {
if(o instanceof PicassoMarker) {
Marker marker = ((PicassoMarker) o).mMarker;
return mMarker.equals(marker);
} else {
return false;
}
}
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
mMarker.setIcon(BitmapDescriptorFactory.fromBitmap(bitmap));
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
}
这里有什么问题?
感谢。
答案 0 :(得分:0)
我使用Handler来显示标记有一些延迟。所以我改变了我的代码
i = getIntent();
if(i.getBooleanExtra("maps", true)) {
ActionBar mactionBar = getSupportActionBar();
mactionBar.setSubtitle(i.getStringExtra("nama"));
buttonNavigasi.setVisibility(View.VISIBLE);
mMyMarkersArray.add(new MyMarker(i.getStringExtra("nama"), i.getStringExtra("deskripsi"), i.getStringExtra("foto"), i.getStringExtra("marker"), Double.parseDouble(i.getStringExtra("lat")), Double.parseDouble(i.getStringExtra("lng"))));
Handler UI_HANDLER = new Handler();
UI_HANDLER.postDelayed(UI_UPDATE_RUNNABLE, 500);
}
这个
Runnable UI_UPDATE_RUNNABLE = new Runnable() {
@Override
public void run() {
plotMarkers(mMyMarkersArray);
}
};