diff options
Diffstat (limited to 'src/org/traccar/GeocoderHandler.java')
-rw-r--r-- | src/org/traccar/GeocoderHandler.java | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/src/org/traccar/GeocoderHandler.java b/src/org/traccar/GeocoderHandler.java index 8933cadac..c5f4d2f9a 100644 --- a/src/org/traccar/GeocoderHandler.java +++ b/src/org/traccar/GeocoderHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 - 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2012 - 2017 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,6 +22,7 @@ import org.jboss.netty.channel.Channels; import org.jboss.netty.channel.MessageEvent; import org.traccar.geocoder.AddressFormat; import org.traccar.geocoder.Geocoder; +import org.traccar.helper.DistanceCalculator; import org.traccar.helper.Log; import org.traccar.model.Position; @@ -30,6 +31,7 @@ public class GeocoderHandler implements ChannelUpstreamHandler { private final Geocoder geocoder; private final boolean processInvalidPositions; private final AddressFormat addressFormat; + private final int geocoderReuseDistance; public GeocoderHandler(Geocoder geocoder, boolean processInvalidPositions) { this.geocoder = geocoder; @@ -41,6 +43,8 @@ public class GeocoderHandler implements ChannelUpstreamHandler { } else { addressFormat = new AddressFormat(); } + + geocoderReuseDistance = Context.getConfig().getInteger("geocoder.reuseDistance", 0); } @Override @@ -55,6 +59,22 @@ public class GeocoderHandler implements ChannelUpstreamHandler { if (message instanceof Position) { final Position position = (Position) message; if (processInvalidPositions || position.getValid()) { + if (geocoderReuseDistance != 0) { + Position lastPosition = Context.getIdentityManager().getLastPosition(position.getDeviceId()); + if (lastPosition != null && lastPosition.getAddress() != null) { + double distance = DistanceCalculator.distance( + position.getLatitude(), position.getLongitude(), + lastPosition.getLatitude(), lastPosition.getLongitude()); + if (distance <= geocoderReuseDistance) { + position.setAddress(lastPosition.getAddress()); + Channels.fireMessageReceived(ctx, position, event.getRemoteAddress()); + return; + } + } + } + + Context.getStatisticsManager().registerGeocoderRequest(); + geocoder.getAddress(addressFormat, position.getLatitude(), position.getLongitude(), new Geocoder.ReverseGeocoderCallback() { @Override |