問題描述
我有一個應用程序,我想在其中找到設備的確切位置.就像你坐出租車一樣,我想找到出租車的確切位置.出租車司機將使用安卓設備.因此,我想通過該設備為其他想要查看出租車位置的用戶定位出租車位置.
I have an app where i wanted to locate the exact location of the device. Like if you take taxi, i wanted to locate where exactly the taxi. Taxi driver will be using android device. So from that device i wanted to locate the cab location for other users who wants to see where the cab is.
我嘗試使用 requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener)
將出租車司機的當前位置上傳到服務器,因為出租車移動了幾米,我已經完成了這個更新位置當駕駛室按下某個啟動按鈕或其他東西時,駕駛室的駕駛室會不斷發送設備的當前位置 X 米.
I have tried uploading current location of cab driver to server as cab moves for few meters by using requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener)
and I have done this updating location of cab when the cab press some start button or something it keeps sending the current location of the device for X meters.
其他用戶將從該更新位置調用服務并找到出租車的當前位置.
From that updated location other users will call the service and find the current location of cab.
在用戶端會有一張地圖,他可以在其中看到出租車的當前位置.用戶調用該服務以查看更新的位置.我通過在該活動上設置 TIMER 來調用服務,因此它會一次又一次地刷新并調用服務器.(設置定時器是最好的方法或任何其他方法).
At user side there will be a map where he can see the current location of cab. User calls the service to see updated location. I am calling the service by setting the TIMER on that activity so it refreshes and calls the server again and again.(Setting a timer is it best approach or any other ways of doing it).
但這種方法需要大量數據和電池,甚至不是實時的.有沒有最好的方法來定位 android 設備,使它看起來至少是實時的.
But this approach takes lot of data and battery and it is not even realtime. Is there any best approach to locate the android device so that it seems to be realtime atleast.
推薦答案
Is there any best approach to locate the android device
您可以使用GCM 服務.
how it will help you?
它可以在駕駛室移動時發送帶有當前latlng(每次位置更新)的推送消息.
It can send push messages with current latlng (for each location update) when the cab moves.
前:
@Override
public void onLocationChanged(Location location) {
String latitude = String.valueOf(location.getLatitude());
String longitude= String.valueOf(location.getLongitude());
//Send push messages with current location
// sendpushMessage(from_Number, to_Number/s, "Location "+ latitude +" "+ longitude;
}
或
您可以重復啟動 IntentService,然后是 AlarmManager,并在每 5/10 秒內發送消息(位置),并在您喜歡時停止(例如,當 cab 在用戶位置范圍內時或類似情況時).
You can start repetitively an IntentService followed by AlarmManager and send messages(location) in each 5/10 seconds and stop whenever you like (e.g when cab is in range of user's location or something like this).
在用戶的設備中,您可以收到有關出租車司機更改位置的消息.只需將這些更新的位置放在地圖中并顯示給用戶.所以用戶可以完美地看到正在運行的駕駛室.
In user's device you can get messages with cab driver's changing location. Just put these updated locations in map and show to the user. So the user can see the running cab perfectly.
在用戶的設備中.. EX:
In User's Device.. EX:
在 GCMIntentService 的 onMessage() 方法中獲取所有消息
,例如:
@Override
protected void onMessage(Context context, Intent intent) {
String message = intent.getExtras().getString("message");
//tokenize the string and get driver's location(latitude, longitude)
//then update each latlng and show cab marker in user's map with a static method
}
注意:我們總是從這里開始通知(onMessage()).所以請確保您沒有使用任何通知,否則它會通過帶有新位置消息的多個通知來干擾用戶.
這種方法對電池很有幫助.此外,您不必每次都檢查服務器以繼續更新.它會在您需要時自動將消息推送到用戶的設備,并且接收者可以完美地接收每條消息.
This approach is very helpful for battery. Also you don;t have to check the server each time for continue update. it will automatically push messages to user's device whenever you want and the receiver can able to receive each message perfectly.
這篇關于從另一臺設備實時定位安卓設備的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!