I have an 8 directional joystick (consisting of a knob and pad),
after setting an onTouchListener to the pad, in MotionEvent.ACTION_MOVE, I'm calculating position_x, position_y, distance, and angle, to get a direction[1-8]. Given a direction[1-8], I'd like to continuously execute a specific movement(); but MotionEvent.ACTION_MOVE only executes while finger is moving..
How can I execute a movement of the lat lng continuously?
--Lat/Lng Movement (d=direction)--
private void movement(int d, double lat, double lng) {
if (d==1) { //up
lat = lat + 0.0000002;}
else if (d==2){ //upright
lat = lat + 0.0000001;
lng = lng + 0.0000001;
}
else if (d==3) { //right
lng = lng + 0.0000002;
}
else if (d==4) { //downright
lng = lng + 0.0000001;
lat = lat - 0.0000001;
}
else if (d==5) { //down
lat = lat - 0.0000002;
}
else if (d==6) { //downleft
lat = lat - 0.0000001;
lng = lng - 0.0000001;
}
else if (d==7) { //left
lng = lng - 0.0000002;
}
else if (d==8) { //upleft
lat = lat + 0.0000001;
lng = lng - 0.0000001;
}
}
--action_move, getangle, direction--
case MotionEvent.ACTION_MOVE: {
position_x = (int) (pad.getX() + pad.getWidth() / 2 - knob.getWidth() / 2 * -1 - knob.getX() - pad.getPivotX());
position_y = (int) (pad.getY() + pad.getHeight() / 2 - knob.getHeight() / 2 * -1 - knob.getY() - pad.getPivotY());
distance = (float) Math.sqrt(Math.pow(position_x, 2) + Math.pow(position_y, 2));
angle = (float) getangle(position_x, position_y);
knob.setX(event.getX() + pad.getX() - knob.getWidth() / 2);
knob.setY(event.getY() + pad.getY() - knob.getHeight() / 2);
direction();
movement(direction(), lat, lng);
private double getangle(float x, float y) {
if (x >= 0 && y >= 0) return Math.toDegrees(Math.atan(y / x));
else if (x < 0 && y >= 0) return Math.toDegrees(Math.atan(y / x)) + 180;
else if (x < 0 && y < 0) return Math.toDegrees(Math.atan(y / x)) + 180;
else if (x >= 0 && y < 0) return Math.toDegrees(Math.atan(y / x)) + 360;
return 0;
}
private int direction() {
if (distance > 50) {
if (angle >= 67.5 && angle < 112.5) return 1;
else if (angle >= 112.5 && angle < 157.5) return 2;
else if (angle >= 157.5 && angle < 202.5) return 3;
else if (angle >= 202.5 && angle < 247.5) return 4;
else if (angle >= 247.5 && angle < 292.5) return 5;
else if (angle >= 292.5 && angle < 337.5) return 6;
else if (angle >= 337.5 || angle < 22.5) return 7;
else if (angle >= 22.5 && angle < 67.5) return 8;
} else if (distance <= 50) { //knob at rest in middle
return 0;
}
return 0;
}
You can check the following question's top answer and apply the same logic of using threads and have a while loop in your ui thread.
Continuous "Action_DOWN" in Android
Related
I want to move and rotate marker as per direction, move marker is working perfectly but when I adding marker rotation it hide marker, I am using following code for marker move animation and rotation when I comment setRotation() method it work perfectly but when i uncomment setRotation() line it hide marker on location update
public void animateMarker(final LatLng toPosition,final LatLng neLatLong,
final boolean hideMarker) {
final Handler handler = new Handler();
final long start = SystemClock.uptimeMillis();
final long duration = 200;
final Interpolator interpolator = new LinearInterpolator();
handler.post(new Runnable() {
#Override
public void run() {
long elapsed = SystemClock.uptimeMillis() - start;
float t = interpolator.getInterpolation((float) elapsed
/ duration);
curMarker.setPosition(toPosition);
curMarker.setRotation(getBearing(toPosition,neLatLong));
if (t < 1.0) {
// Post again 16ms later.
handler.postDelayed(this, 16);
} else {
if (hideMarker) {
curMarker.setVisible(false);
} else {
curMarker.setVisible(true);
}
}
}
});
}
private float getBearing(LatLng begin, LatLng end) {
double lat = Math.abs(begin.latitude - end.latitude);
double lng = Math.abs(begin.longitude - end.longitude);
if (begin.latitude < end.latitude && begin.longitude < end.longitude)
return (float) (Math.toDegrees(Math.atan(lng / lat)));
else if (begin.latitude >= end.latitude && begin.longitude < end.longitude)
return (float) ((90 - Math.toDegrees(Math.atan(lng / lat))) + 90);
else if (begin.latitude >= end.latitude && begin.longitude >= end.longitude)
return (float) (Math.toDegrees(Math.atan(lng / lat)) + 180);
else if (begin.latitude < end.latitude && begin.longitude >= end.longitude)
return (float) ((90 - Math.toDegrees(Math.atan(lng / lat))) + 270);
return -1;
}
Make sure that the .anchor(0.5f, 0.5f) set for your marker and better use SphericalUtil.computeHeading() from Google Maps Android API Utility Library instead of your private float getBearing(LatLng begin, LatLng end)
I'm drawing Polygon on city in order to check if current position is inside this polygon or not, and i'm doing that with below code:-
ArrayList<LatLng> polyLoc = new ArrayList<LatLng>();
polyLoc.add(new LatLng(24.643932, 46.297718));
polyLoc.add(new LatLng(24.695098, 46.555897));
polyLoc.add(new LatLng(24.921971, 46.476246));
polyLoc.add(new LatLng(25.147185, 46.366383));
polyLoc.add(new LatLng(25.155886, 47.249409));
polyLoc.add(new LatLng(24.929444, 47.346913));
polyLoc.add(new LatLng(24.691355, 47.106587));
polyLoc.add(new LatLng(24.449060, 47.219197));
polyLoc.add(new LatLng(24.293947, 46.973377));
polyLoc.add(new LatLng(24.641436, 46.299092));
And i checking if the current position is inside this polygon or not by this way :-
if (hasPermission() && gpsTracker.canGetLocation()) {
if (isPointInPolygon(new LatLng(gpsTracker.getLatitude(), gpsTracker.getLongitude()), polyLoc)) {
cash.setVisibility(View.VISIBLE);
cashIcon.setVisibility(View.VISIBLE);
} else {
cash.setVisibility(View.GONE);
cashIcon.setVisibility(View.GONE);
}
Log.d(TAG, "reservationDialog: " + gpsTracker.getLatitude() + gpsTracker.getLongitude());
}
here is my isPointInPolygon method :
private boolean isPointInPolygon(LatLng tap, ArrayList<LatLng> vertices) {
int intersectCount = 0;
for (int j = 0; j < vertices.size() - 1; j++) {
if (rayCastIntersect(tap, vertices.get(j), vertices.get(j + 1))) {
intersectCount++;
}
}
return ((intersectCount % 2) == 1); // odd = inside, even = outside;
}
private boolean rayCastIntersect(LatLng tap, LatLng vertA, LatLng vertB) {
double aY = vertA.latitude;
double bY = vertB.latitude;
double aX = vertA.longitude;
double bX = vertB.longitude;
double pY = tap.latitude;
double pX = tap.longitude;
if ((aY > pY && bY > pY) || (aY < pY && bY < pY)
|| (aX < pX && bX < pX)) {
return false; // a and b can't both be above or below pt.y, and a or
// b must be east of pt.x
}
double m = (aY - bY) / (aX - bX); // Rise over run
double bee = (-aX) * m + aY; // y = mx + b
double x = (pY - bee) / m; // algebra is neat!
return x > pX;
}
I don't know why it's not working, what i'v missed here?
I don't know why your code didn't work.
But you can use PolyUtil.containsLocation (new LatLng (latitude, longitude), polyLoc, true)
This would return false if the position outside the polygon.
In my project, I'm doing this calculation on the server. I'm using geolib library to do this. If you need to calculate in your app. You can get the logic from the library.
Library:
https://www.npmjs.com/package/geolib
Git: https://github.com/manuelbieh/Geolib
How to create Polygon Geofence from multiple geo locations(long,lat values) . Also how to track user is entering into this geofence region or exiting from this region on android.
A geofence is simply an array of lat/long points that form a polygon. Once you have a list of lat/long points, you can use a point-inside-polygon check to see if a location is within the polygon.
This is code I have used in my own projects to perform point-in-polygon checks for very large concave polygons (20K+ vertices):
public class PolygonTest
{
class LatLng
{
double Latitude;
double Longitude;
LatLng(double lat, double lon)
{
Latitude = lat;
Longitude = lon;
}
}
bool PointIsInRegion(double x, double y, LatLng[] thePath)
{
int crossings = 0;
LatLng point = new LatLng (x, y);
int count = thePath.length;
// for each edge
for (var i=0; i < count; i++)
{
var a = thePath [i];
var j = i + 1;
if (j >= count)
{
j = 0;
}
var b = thePath [j];
if (RayCrossesSegment(point, a, b))
{
crossings++;
}
}
// odd number of crossings?
return (crossings % 2 == 1);
}
bool RayCrossesSegment(LatLng point, LatLng a, LatLng b)
{
var px = point.Longitude;
var py = point.Latitude;
var ax = a.Longitude;
var ay = a.Latitude;
var bx = b.Longitude;
var by = b.Latitude;
if (ay > by)
{
ax = b.Longitude;
ay = b.Latitude;
bx = a.Longitude;
by = a.Latitude;
}
// alter longitude to cater for 180 degree crossings
if (px < 0) { px += 360; };
if (ax < 0) { ax += 360; };
if (bx < 0) { bx += 360; };
if (py == ay || py == by) py += 0.00000001;
if ((py > by || py < ay) || (px > Math.max(ax, bx))) return false;
if (px < Math.min(ax, bx)) return true;
var red = (ax != bx) ? ((by - ay) / (bx - ax)) : float.MAX_VALUE;
var blue = (ax != px) ? ((py - ay) / (px - ax)) : float.MAX_VALUE;
return (blue >= red);
}
}
In terms of program flow, you will want a background service to do location updates and then perform this check against your lat/long polygon data to see if the location is inside.
In case people are still looking for a Polygon Geofencing check, you can complete this with the GoogleMaps.Util.PolyUtil containsLocation method.
I am doing an edit to a driver file that uses Multi-Touch protocol.
My goal is to bind a swipe gesture from (x1,y1) to (x2,y2) that follows a line.
To do this i execute this function inside the key pressure detection function:
static void do_swipe(struct kp *kp, long xstart, long ystart, long xend, long yend,
int id) {
printk("SWIPE! X1 %d Y1 %d X2 %d Y2 %d ID %d \n",xstart,ystart,xend,yend,id);
int sx, sy;
int dx = abs(xend - xstart);
int dy = abs(yend - ystart);
if (xstart < xend)
sx = 1;
else
sx = -1;
if (ystart < yend)
sy = 1;
else
sy = -1;
int err = dx - dy;
long tempx = xstart;
long tempy = ystart;
while (1) {
key_report(kp, tempx, tempy, id);
//Touch is now detected with input_sync
input_sync(kp->input);
if(tempx == xend && tempy == yend) break;
int e2 = 2 * err;
if (e2 > (-1)*dy) {
err = err - dy;
tempx = tempx + sx;
} else if (e2 < dx) {
err = err + dx;
tempy = tempy + sy;
}
}
printk("END! X %d Y %d \n",tempx, tempy);
}
This function is an edited Bresenham's line algorhitm that instead of drawing a line calls key_report to simulate a touch at the given coordinates.
static void key_report(struct kp *kp, long x, long y, int id) {
if (x == 0 && y == 0) {
//printk("---------- zero point --------------\n");
;
} else {
input_report_key(kp->input, BTN_TOUCH, 1);
input_report_abs(kp->input, ABS_MT_TRACKING_ID, id);
input_report_abs(kp->input, ABS_MT_TOUCH_MAJOR, 20);
input_report_abs(kp->input, ABS_MT_WIDTH_MAJOR, 20);
input_report_abs(kp->input, ABS_MT_POSITION_X, x);
input_report_abs(kp->input, ABS_MT_POSITION_Y, y);
input_mt_sync(kp->input);
}
release = 1;
}
Where kp-> input is so defined
struct input_dev *input;
Edit: I updated the code and I'm also attaching the generic code of a key pressure, because I'd like the driver to perform a swipe only once for each key pressure, to avoid swipe looping.
static void kp_work(struct kp *kp) {
int i, code, value;
kp_search_key(kp);
if (key_param[0] == 3) {
if (kp->key_valid[4] == 1) {
value = kp->key_value[4];
kp->flagchan4 = 0;
if (value >= 0 && value <= (9 + 40)) {
if (key_param[99] == 1 ) {
do_swipe(kp, key_param[25], key_param[26], key_param[97],
key_param[98], 12);
} else
key_report(kp, key_param[25], key_param[26], 12);
} else if (value >= 392 - 40 && value <= (392 + 40)) {
if (key_param[102] == 1) {
do_swipe(kp, key_param[23], key_param[24], key_param[100],
key_param[101], 12);
} else
key_report(kp, key_param[23], key_param[24], 12);
}
kp->flagchan4 = 0;
} else {
kp->flagchan4 = 1;
}
}
input_sync(kp->input);
}
Maybe i could set a check on kp->flagchan4...
Edit: Setting a check on flagchan4 did the trick.
i m working on Android Maps and i need to Add Compass functionality in it that is When I click on Compass Button it shows separate compass.
Can any one guide me the Right direction or refrence me any tutorial. I need to add Compass seperatly
thanks in Advance for reading and Replying
The Scenario in My Cass is To Use Separately use The Compass When click on Compass Button
it Will Show
Distance from Current Location(My Location) to the Destination location
Compass
Compass Head Towards North Direction
So to achieve all these I've used the following approach
hope this will help anyone who has the same scenario as I have
The Code Snippet of my Class That will get Distance between two points around the globe and show image as Compass with head towards north direction
public class MapController extends MapActivity implements SensorEventListener {
public static GeoPoint Mypoint, MapPoint;
double km, meter;
public static int kmInDec, meterInDec;
private SensorManager mSensorManager;
private Sensor mAccelerometer, mField;
Location myLoc, mapLoc;
double myLat, myLong, mapLat, mapLong;
public static double lat, lng;
public static double mylat = Double.parseDouble(Constants.DEVICE_OBJ.lat);//31.567615d;
public static double mylon = Double.parseDouble(Constants.DEVICE_OBJ.lng);//74.360962d;
com.google.android.maps.MapController mapController;
public static boolean enabled = false;
#Override
protected void onCreate(Bundle icicle) {
// TODO Auto-generated method stub
super.onCreate(icicle);
setContentView(R.layout.map_layout);
Log.v(Constants.TAG, "In Map Controller");
fetchUI();
}
public void fetchUI() {
myLoc = new Location("Test");
mapLoc = new Location("Test");
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mField = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
myLat = (31.567615 * 1e6) / 1E6;
myLong = (74.360962d * 1e6) / 1E6;
mapLat = (MapPoint.getLatitudeE6()) / 1E6;
mapLong = (MapPoint.getLongitudeE6()) / 1E6;
myLoc.setLatitude(31.567615);
myLoc.setLongitude(74.360962);
mapLoc.setLatitude(mapLat);
mapLoc.setLongitude(mapLong);
}
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_UI);
mSensorManager.registerListener(this, mField, SensorManager.SENSOR_DELAY_UI);
}
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
///Method to calculate the Distance between 2 Geo Points aroud the Globe
public double CalculationByDistance(GeoPoint StartP, GeoPoint EndP) {
double lat1 = StartP.getLatitudeE6() / 1E6;
double lat2 = EndP.getLatitudeE6() / 1E6;
double lon1 = StartP.getLongitudeE6() / 1E6;
double lon2 = EndP.getLongitudeE6() / 1E6;
double dLat = Math.toRadians(lat2 - lat1);
double dLon = Math.toRadians(lon2 - lon1);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
Math.sin(dLon / 2) * Math.sin(dLon / 2);
double c = 2 * Math.asin(Math.sqrt(a));
double valueResult = Radius * c;
double km = valueResult / 1;
DecimalFormat newFormat = new DecimalFormat("####");
kmInDec = Integer.valueOf(newFormat.format(km));
meter = valueResult % 1000;
meterInDec = Integer.valueOf(newFormat.format(meter));
Log.i("Radius Value", "" + valueResult + " KM " + kmInDec + " Meter " + meterInDec);
return Radius * c;
}
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
#Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
if (myLoc == null) return;
float azimuth = event.values[0];
float baseAzimuth = azimuth;
GeomagneticField geoField = new GeomagneticField(Double
.valueOf(myLoc.getLatitude()).floatValue(), Double
.valueOf(myLoc.getLongitude()).floatValue(),
Double.valueOf(myLoc.getAltitude()).floatValue(),
System.currentTimeMillis());
azimuth -= geoField.getDeclination(); // converts magnetic north into true north
// Store the bearingTo in the bearTo variable
float bearTo = myLoc.bearingTo(mapLoc);
// If the bearTo is smaller than 0, add 360 to get the rotation clockwise.
if (bearTo < 0) {
bearTo = bearTo + 360;
}
//This is where we choose to point it
float direction = bearTo - azimuth;
// If the direction is smaller than 0, add 360 to get the rotation clockwise.
if (direction < 0) {
direction = direction + 360;
}
rotateImageView(compasView, R.drawable.app_icon, direction);
//Set the field
String bearingText = "N";
if ((360 >= baseAzimuth && baseAzimuth >= 337.5) || (0 <= baseAzimuth && baseAzimuth <= 22.5))
bearingText = "N";
else if (baseAzimuth > 22.5 && baseAzimuth < 67.5) bearingText = "NE";
else if (baseAzimuth >= 67.5 && baseAzimuth <= 112.5) bearingText = "E";
else if (baseAzimuth > 112.5 && baseAzimuth < 157.5) bearingText = "SE";
else if (baseAzimuth >= 157.5 && baseAzimuth <= 202.5) bearingText = "S";
else if (baseAzimuth > 202.5 && baseAzimuth < 247.5) bearingText = "SW";
else if (baseAzimuth >= 247.5 && baseAzimuth <= 292.5) bearingText = "W";
else if (baseAzimuth > 292.5 && baseAzimuth < 337.5) bearingText = "NW";
else bearingText = "?";
Distance.setText(kmInDec + " Km " + " " + meterInDec + " m");
}
////This is the method to Show the Image as Compass
private void rotateImageView(ImageView imageView, int drawable, float rotate) {
// Decode the drawable into a bitmap
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
drawable);
// Get the width/height of the drawable
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = bitmapOrg.getWidth(), height = bitmapOrg.getHeight();
// Initialize a new Matrix
Matrix matrix = new Matrix();
// Decide on how much to rotate
rotate = rotate % 360;
// Actually rotate the image
matrix.postRotate(rotate, width, height);
// recreate the new Bitmap via a couple conditions
Bitmap rotatedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width, height, matrix, true);
//BitmapDrawable bmd = new BitmapDrawable( rotatedBitmap );
//imageView.setImageBitmap( rotatedBitmap );
imageView.setImageDrawable(new BitmapDrawable(getResources(), rotatedBitmap));
imageView.setScaleType(ScaleType.CENTER);
}
}
use this guide:
Google Maps Tutorial
search for compass, try launching a new activity with your needs when pressing on the compass (an overlay)
anyway, the tutorial is very comprehensive, I believe it will solve most of your needs =).