Android : GPS tracking system for find each other

2019-06-14 18:26发布

问题:

I have to develop an Android app, what this app is supposed to do is help people find each other in a close area without wasting too much power. Both people can use the app with the phone in their palm facing up to find each other. It can use gps to give an initial direction to travel(Forward, left, right, backwards). It can then use bluetooth and wifi strength to give the users a general idea of whether they are getting closer or farther. It does not need to give the specific distance between the phones.

I've followed this(http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/) tutorial and it works fine. But I have no idea how to use this in my app.

I am a trainee of developing Android applications and I am new to GPS tracking systems. Actually I have no clear idea about how to start this app.

Here is my attempt.

My xml file:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/button1"
    android:layout_alignBottom="@+id/button1"
    android:layout_alignParentRight="true"
    android:onClick="stopNewService"
    android:text="Stop Service" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="23dp"
    android:onClick="startNewService"
    android:text="Start Service" />

This is my MyService.java class

package com.example.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

public class MyService extends Service {

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onCreate() {
    Toast.makeText(this, "The new Service was Created", Toast.LENGTH_LONG)
            .show();

}

@Override
public void onStart(Intent intent, int startId) {
    // For time consuming an long tasks you can launch a new thread here...
    Toast.makeText(this, " Service Started", Toast.LENGTH_LONG).show();

}

@Override
public void onDestroy() {
    Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();

}

}

And here is my MainActivity.java class

package com.example.service;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity implements View.OnClickListener {
private Button bStart, bStop;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

// Start the service
public void startNewService(View view) {

    startService(new Intent(this, MyService.class));
}

// Stop the service
public void stopNewService(View view) {

    stopService(new Intent(this, MyService.class));
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.button1:
        startNewService(v);

        break;
    case R.id.button2:
        stopNewService(v);

        break;

    default:
        break;
    }
}

}

I've added the service into manifest file.

<service android:name=".MyService"></service>

Furthermore I can get the locations of each devices but the problem is how should I send and receive those data between two devices.

I would be much thankful if anyone please be so kind enough to help me to clarify this.

Thanks in advance.

回答1:

You can use the following code if you have a server:

  1. You can have a service running periodically (once in 15 mins or so ).This service can send the location of devices to the server as well fetch locations of friend's device .Since the service will run in periodically in background it will be efficient and will not consume much power.

  2. This service will check every time if any friend is nearby.If so a notification can be generated.You can use the following to check this :

    public static float distBetweenTwoPoints(float lat1, float lng1,
            float lat2, float lng2) {
        // returns distance (in meters) between two points
        double earthRadius = 3958.75;
        double dLat = Math.toRadians(lat2 - lat1);
        double dLng = Math.toRadians(lng2 - lng1);
        double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
                + Math.cos(Math.toRadians(lat1))
                * Math.cos(Math.toRadians(lat2)) * Math.sin(dLng / 2)
                * Math.sin(dLng / 2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        double dist = earthRadius * c;
    
        int meterConversion = 1609;
    
        return new Float(dist * meterConversion).floatValue();
    }
    
  3. You will have to store these locations in sqlite database.Here is a tutorial on sqlite.

  4. For gps tracking you can refer to this tutorial .

Note: But please make sure both devices have gps enabled .

EDIT

It can then use bluetooth and wifi strength to give the users a general idea of whether they are getting closer or farther

As soon as your service checks that two friends are nearby(within approx bluetooth range) .You can programmatically switch on bluetooth and implement what you wish to do.



回答2:

Basic Idea : 1) Setup database (MySQL,SQL etc) in your server which have users table and location table which have thr locations data

2) In Android Create Service which will fire once in 15 minutes requesting current location.

3) Create SharedPreference / SQliteDB which holds the last Coordinates of the device which was also updated to the Server database.

4) If the current location matches the Last Location retrieved from the SharedPreference and/or around within proximity (depends on how much u give 30ft or more) then user is in the same place so don't upload the coordinates to the Server Database.

5) If the user isn't within proximity or last location doesn't match current location then upload the coordinates to the Server Database.

6) After uploading coordinates to the server, update SharedPreference too..

7) After uploaded coordinates, The Server must return the User details from its database who are within your proximity. Your app then knows who are near you and send notification to you saying "Your buddy XXX is nearby!"

8) In Server, after uploading your coordinates, The server will also send notification to the user who are nearby you using "Google Cloud Messaging".

9) Tips : Run a cron job in your server which will execute every minute and checks "Who is near to Who" and send notification to them.

Hope it helps somebody :)



标签: java android gps