There are several functions you can employ programmatically to make an agent move in GIS space, including the moveTo()and jumpTo()functions.
If you have used GIS space markup elements to specify locations and routes for your model, you can move an agent using the function moveTo(node). For example, to move a truck agent toward a GIS Point named gisPointLondon, you could use the function truck.moveTo(gisPointLondon). Then at runtime, when the function is executed, the truck would move toward the GIS Point along the nearest GIS Route you have provided. Using this approach, you would not have to obtain route or location data from an online route service. However, if you have not used GIS Route elements to provide a custom transportation network, AnyLogic will download route data from an online route service.
You can make an agent move toward a location with given coordinates:
To move one agent toward another, you could use the function moveTo(agent), where agent is the name of the destination agent.
If you need to make an agent move toward the closest agent of another population, you could use the function moveToNearestAgent(population). For example, if you want a truck agent to move to the nearest gas station among the agent population gasStations, use the function truck.moveToNearestAgent(gasStations).
While an agent is moving toward a destination, you can control and probe its movement using the following functions. There are also functions to probe current locations and to calculate distance:
boolean isMoving() - Tests if the agent is currently moving. Returns true if the agent is moving and false otherwise.
double timeToArrival()- Returns the length of time remaining (in model time units) until the agent arrives at its destination. If the agent is not moving, returns 0.
double distanceTo(Agent other) - Returns the distance (in meters) from this agent to another agent specified as the argument.
double getGISHeading()- For an agent moving in continuous 2D GIS space, returns the agent’s current heading angle, measured in radians clockwise from the North.
double getLat() - Returns the latitude of the agent’s current location, measured in degrees (-90 ... (South) ... 0 ... (North) ... +90).
double getLon() - Returns the longitude of the agent’s current location, measured in degrees (-180 ... (West) ... 0 ... (East) ... +180).
double getX() - The same as getLat()
double getY() - The same as getLon()
double getTargetLat() - If the agent is moving, returns the latitude of the target location, otherwise returns the current latitude in GIS space, measured in degrees (-90 ... (South) ... 0 ... (North) ... +90)
double getTargetLon() - If the agent is moving, returns the longitude of the target location, otherwise returns the current longitude in GIS space, measured in degrees (-180 ... (West) ... 0 ... (East) ... +180)
double getTargetX() - The same as getTargetLat()
double getTargetY() - The same as getTargetLon()
You can instantly move an agent to a new location (i.e. relocate the agent) using the function jumpTo(). If this function is executed while the agent is already moving, it stops the movement and relocates the agent to the given location. The function jumpTo() is different from the function setLocation(). The function setLocation() places an agent at a specified location on a GIS map before starting the simulation. It is typically invoked from the On startup code of the model's top-level agent. By contrast, the jumpTo() function relocates an agent when it is executed at model runtime.
As with the moveTo() functions, AnyLogic supports alternative jumpTo() functions:
jumpTo(double latitude, double longitude)
- Immediately places the agent at the given location coordinates.
latitude - the location's latitude, measured in degrees (-90 ... (South) ... 0 ... (North) ... +90)
longitude - the location's longitude, measured in degrees (-180 ... (West) ... 0 ... (East) ... +180)
Example: truck.jumpTo(51.3 , 0.7)