Local Server

Licensing

Move graphics

Description

This sample shows how to add graphics to a map and move them in the display by updating the graphics' positions in the graphics layer. The graphics in this case are 'taxis', which are symbolized using a picture marker symbol, and represented as point geometries. The taxis move along pre-defined routes, read in from files, and are randomly assigned to routes as the user adds more taxis to the map. The taxi graphics are updated regularly on a Swing Timer using their graphic ID and the position to move to. This application was built for the Esri 2012 Developer Summit as a demo for the Java technical session.

/* Copyright 2014 Esri
All rights reserved under the copyright laws of the United States
and applicable international laws, treaties, and conventions.
You may freely redistribute and use this sample code, with or
without modification, provided you include the original copyright
notice and use restrictions.
See the use restrictions.*/
package com.esri.client.samples.graphics;
import com.esri.core.geometry.Point;
/**
* A class representing one taxi. A taxi keeps track of what route it is
* following, where it is along the route, its current {@link Point} location
* on the map, as well as its own unique graphic identifier, with which you can
* refer to this specific graphic/taxi within the application.
*/
public class Taxi {
private TaxiRoute followingRoute;
private int location;
private int graphicIdentifier;
private Point currentPosition;
private boolean goSlow = false;
private boolean stopAtJunction = false;
private int countDown = 0; // used to slow taxi or stop it for a while
// slow and stop properties
private final int slowCountDown = 2;
private final int stopCountDown = 200;
public Taxi(TaxiRoute route, int positionOnRoute, int graphicID) {
followingRoute = route;
graphicIdentifier = graphicID;
setLocation(positionOnRoute);
currentPosition = new Point();
}
public boolean MoveTaxi() {
boolean haveWeMoved = false;
// have we come to a slowdown point?
if ((followingRoute.getSlowFlag().get(location)).intValue() == 1) {
// 2 ticks per move now
goSlow = true;
countDown = slowCountDown;
// shift to the next location along the route
location++;
}
// have we come to a stop point?
if ((followingRoute.getStopFlag().get(location)).intValue() == 1) {
// stop for a few ticks
stopAtJunction = true;
countDown = stopCountDown;
// shift to the next location along the route
location++;
}
// if we are in countdown (slow or stop at junction), decrement count down
if (countDown > 0) {
countDown--;
}
// are we okay to move forward?
if (countDown == 0) {
//update the current position geometry
currentPosition.setX((followingRoute.getXPos().get(location)).intValue());
currentPosition.setY((followingRoute.getYPos().get(location)).intValue());
haveWeMoved = true;
// were we in stop mode?
if (stopAtJunction == true) {
// move ahead at full speed
stopAtJunction = false;
}
// were we in show mode?
if (goSlow == true) {
// keep going slow
countDown = slowCountDown;
}
// increment the location making sure we have not dropped off the end of the of route
if (location++ >= (followingRoute.getXPos().size()-1)) {
// move back to the start of the route
location = 0;
}
}
return haveWeMoved;
}
public Point getCurrentPosition() {
return currentPosition;
}
public int getLocation() {
return location;
}
public void setLocation(int location) {
this.location = location;
}
public int getGraphicIdentifier() {
return graphicIdentifier;
}
public TaxiRoute getFollowingRoute() {
return followingRoute;
}
public void setGraphicIdentifier(int graphicIdentifier) {
this.graphicIdentifier = graphicIdentifier;
}
}