Un peu d'un post étrange car c'est quelque chose que j'ai posté avant, pensé que c'était résolu mais pas!ItemizedOverlay Rotation du marqueur et crash
J'ai une situation où j'ai besoin d'utiliser un Drawable personnalisé pour chaque marqueur, l'effet dont j'ai besoin est de faire tourner le marqueur d'un certain nombre de degrés et je vais probablement le faire complètement à tort.
Pour commencer, j'utilise l'excellent Creative Commons car cela fonctionne et ne se casse qu'après mes ajouts!
https://github.com/commonsguy/cw-advandroid/tree/master/Maps/NooYawkAsync
Le post précédent et le code est ici
Android Maps NullPointerException ItemizedOverlay
Si quelqu'un peut recommander une meilleure et plus stable pour obtenir une rotation Drawable et me mettre sur la bonne voie qui serait sois super.
Comme vous pouvez le voir dans mon code, je ne mets pas le Drawable dans la superposition et je le fais dehors à l'intérieur d'un autre objet et je suspecte que c'est la raison pour laquelle il se casse!
Au moment où je mets mon Drawable en faisant cela ...
public Drawable getRotatedIcon(String string) {
Drawable marker = null;
Double tempHeading = Double.valueOf(string);
long intHeading = Math.round(tempHeading/10);
int resID = getResources().getIdentifier(
"icon_rotate_" + Long.toString(intHeading), "drawable",
"com.test.testapp");
marker = getResources().getDrawable(resID);
marker.setBounds(0, 0, marker.getIntrinsicWidth(),marker.getIntrinsicHeight());
return marker;
}
Et puis pour créer tout ce que j'utilise cette ...
if (sites != null){
sites.clearItems();
}else{
sites = new SitesOverlay();
}
for (int i = 0; i < getMainOverlayArray().size(); i++) {
tempOver = getMainOverlayArray().get(i);
tempOver.setMarker(getRotatedIcon(tempOver.getcourse()));
if (tempOver != null){
sites.addItem(tempOver);
}
}
sites.finishedLoading();
Et ma classe SitesOverlay ...
private class SitesOverlay extends ItemizedOverlay<pfOverlayItem> {
private List<pfOverlayItem> items = new ArrayList<pfOverlayItem>();
private PopupPanel panel=new PopupPanel(R.layout.popup);
private MapLocation selectedMapLocation;
private static final int CIRCLERADIUS = 2;
private ArrayList<pfOverlayItem> mOverlays = new ArrayList<pfOverlayItem>();
public SitesOverlay() {
super(null);
populate();
}
public void finishedLoading(){
populate();
}
@Override
protected pfOverlayItem createItem(int i) {
return (items.get(i));
}
public void addItem(OverlayItem overlay) {
overlay.setMarker(boundCenter(overlay.getMarker(0)));
items.add((pfOverlayItem) overlay);
}
public void clearItems(){
runOnUiThread(new Runnable() {
public void run() {
items.clear();
myMapView.invalidate();
}
});
}
public void clear() {
mOverlays.clear();
myMapView.removeAllViews();
setLastFocusedIndex(-1);
populate();
}
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
super.draw(canvas, mapView, false);
try {
if (getMainOverlayArray().size() != 0){
pfOverlayItem tempOver = null;
for (int i = 0; i < getMainOverlayArray().size(); i++) {
tempOver = getMainOverlayArray().get(i);
boolean isMatch = false;
//Log.i("Selected Name",selectedName);
if (tempOver.getTitle().equals(selectedName))
{
isMatch = true;
}
if (isMatch){
Projection projection = mapView.getProjection();
Point point = new Point();
projection.toPixels(tempOver.getPoint(), point);
Paint background = new Paint();
background.setColor(Color.WHITE);
background.setAlpha(150);
RectF rect = new RectF();
rect.set(point.x - 50, point.y +15,
point.x + 90, point.y + 50);
canvas.drawRoundRect(rect, 5, 5, background);
Paint text = new Paint();
text.setAntiAlias(true);
text.setColor(Color.BLUE);
text.setTextSize(14);
text.setTypeface(Typeface.MONOSPACE);
canvas.drawText(tempOver.getTitle() + " " + tempOver.getcallsign(), point.x -50 , point.y + 30, text);
canvas.drawText(tempOver.getdestination() + " " + tempOver.getdraft(), point.x -50 , point.y + 45, text);
}
}
}
}catch (Exception e){
Log.e("Error", "Problem drawing view", e);
e.printStackTrace();
}
}
@Override
protected boolean onTap(int i) {
pfOverlayItem item = getItem(i);
if (selectedName.equals(item.getTitle())){
try{
Intent myIntent = new Intent(TestApp.this, DetailActivity.class);
myIntent.putExtra("int", i);
myIntent.putExtra("string", selectedName);
PlaneFinderMain.this.startActivity(myIntent);
}catch (Exception e){
Log.e("Error", "Cannot launch", e);
}
}
currentadshex = item.getmmsi();
new GetRouteTask(item.getmmsi()).execute();
selectedItem = i;
selectedName = item.getTitle();
selectedPlanePoint = item.getPoint();
GeoPoint geo=item.getPoint();
Point pt=myMapView.getProjection().toPixels(geo, null);
View view=panel.getView();
((TextView)view.findViewById(R.id.reg)).setText(item.getTitle());
((TextView)view.findViewById(R.id.callsign)).setText(item.getcallsign());
((TextView)view.findViewById(R.id.dest)).setText(item.getdestination());
((TextView)view.findViewById(R.id.draft)).setText(item.getdraft());
return (true);
}
@Override
public boolean onTouchEvent(MotionEvent event, MapView mapView) {
if (event.getAction() == MotionEvent.ACTION_DOWN){
if (selectedPlanePoint != null){
Projection projection = mapView.getProjection();
Point point = new Point();
projection.toPixels(selectedPlanePoint, point);
Point pointHit = new Point();
pointHit.x=(int)event.getX();
pointHit.y=(int)event.getY();
if ((point.x - pointHit.x) >-100 && (point.x - pointHit.x) <70 && (point.y - pointHit.y) < -25 && (point.y - pointHit.y) > -95){
try{
Intent myIntent = new Intent(TestApp.this, DetailActivity.class);
myIntent.putExtra("int", selectedItem);
myIntent.putExtra("string", selectedName);
TestApp.this.startActivity(myIntent);
}catch (Exception e){
Log.e("Error", "Cannot launch", e);
}
}else{
}
}
}
return false;
}
@Override
public int size() {
return (items.size());
}
public void addOverlay(OverlayItem o){
setLastFocusedIndex(-1);
populate();
}
}
Cela semble très bien Reuben et un je pense que cela a du kilométrage en effet! Je suppose que le meilleur moyen pour chaque marqueur d'avoir un angle différent est de passer un paramètre supplémentaire dans this.marker = new RotateDrawable (marqueur); ?? –
Oh, exactement. Je les ai simplement fait pivoter de 5 degrés chaque image dans RotateDrawable.rotate() pour montrer que ça a marché ... vous n'avez pas besoin de garder cette fonction. Vous pouvez également perdre les champs mPivotX et mPivotY inutilisés btw ... ils ne sont pas nécessaires. –
OK merci, je suppose que je supprime aussi le run() car je n'ai pas besoin d'animer le roatation, ce sera une rotation unique de l'objet, aucune animation nécessaire du tout. –