09.02.2017 Views

creez-des-applications-pour-android

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

6 Exploiter les fonctionnalités d’Android<br />

On implémente en général au moins deux métho<strong>des</strong>. La première est void draw(Canvas<br />

canvas, MapView mapView, boolean shadow) qui décrit le <strong>des</strong>sin à effectuer. On <strong>des</strong>sine<br />

sur le canvas et on projette le <strong>des</strong>sin du Canvas sur la vue mapView. En ce qui concerne shadow,<br />

c’est plus compliqué. En fait, cette méthode draw est appelée deux fois : une première fois<br />

où shadow vaut false <strong>pour</strong> <strong>des</strong>siner normalement et une seconde où shadow vaut true <strong>pour</strong><br />

rajouter <strong>des</strong> ombres à votre <strong>des</strong>sin.<br />

On a un problème d’interface entre le Canvas et la MapView : les coordonnées sur le Canvas<br />

sont en Point et les coordonnées sur la MapView sont en GeoPoint, il nous faut donc un moyen<br />

<strong>pour</strong> convertir nos Point en GeoPoint. Pour cela, on utilise une Projection avec la méthode<br />

Projection getProjection() sur la MapView. Pour obtenir un GeoPoint depuis un Point,<br />

on peut faire GeoPoint fromPixels(int x, int y) et, <strong>pour</strong> obtenir un Point depuis un<br />

GeoPoint, on peut faire Point toPixels(GeoPoint in, Point out). Par exemple :<br />

Point point = new Point(10, 10);<br />

// Obtenir un GeoPoint<br />

GeoPoint geo = projection.fromPixels(point.x, point.y);<br />

Point nouveauPoint = null ;<br />

// Obtenir un Point. On ignore le retour <strong>pour</strong> passer l'objet en paramètre <strong>pour</strong> <strong>des</strong> rais<br />

projection.toPixels(geo, nouveauPoint );<br />

Ainsi, <strong>pour</strong> <strong>des</strong>siner un point rouge à l’emplacement du bâtiment de Simple IT :<br />

import <strong>android</strong>.graphics.Canvas;<br />

import <strong>android</strong>.graphics.Paint;<br />

import <strong>android</strong>.graphics.Point;<br />

import <strong>android</strong>.graphics.RectF;<br />

import com.google.<strong>android</strong>.maps.GeoPoint;<br />

import com.google.<strong>android</strong>.maps.MapView;<br />

import com.google.<strong>android</strong>.maps.Overlay;<br />

import com.google.<strong>android</strong>.maps.Projection;<br />

public class PositionOverlay extends Overlay {<br />

private int mRadius = 5 ;<br />

// Coordonnées du bâtiment de Simple IT<br />

private Double mLatitutde = 48.872808*1E6 ;<br />

private Double mLongitude = 2.33517*1E6 ;<br />

public PositionOverlay() {<br />

}<br />

@Override<br />

public void draw(Canvas canvas, MapView mapView, boolean shadow) {<br />

Projection projection = mapView.getProjection();<br />

414

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!