如何在需要时在服务器上发送设备的位置 [英] How to send location of the device on server when needed

查看:21
本文介绍了如何在需要时在服务器上发送设备的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,其中我需要设备的当前纬度和经度.获取纬度/经度的时间范围将由服务器决定.每当需要获取 lat/lang 时,服务器都会发送通知.当设备收到通知时服务器,同时设备将其位置发送给服务器.

I am developing a application in which i need current lat and long of the device. Time frame to get the lat/long will be decided by the server. Server will send notification whenever lat/lang is required to be fetched. When device get notified from the server,at the same time device send its location to server.

一些条件:

  • 即使设备没有 GPS,我也想要纬度/经度

  • I want lat/long even if device don't have gps

如果用户没有触摸或点击通知,那么我也想获取纬度/经度,即即使设备处于睡眠模式.

if user doesn't touch or click the notification,then also i want to fetch lat/long, i.e. even if the device is in sleep mode.

此外,我不想整天运行后台服务,因为它会消耗设备的电池.

Also i don't want to run a background service for the whole day as it consumes battery of the device.

推荐答案

我绝对建议在此处使用 Google Cloud Messaging.这样做的好处是,一旦设备运行您的应用程序,您就可以让它在 GCM 上注册,并且您可以向他们发送一些消息,让他们将自己的坐标注册到您的数据库中.

I would definitely suggest using Google Cloud Messaging here. This has the benefit that once a device runs your app, you can make it register against GCM and you can send them some message that would make them register their coordinates into your database.

这种方法需要你实现一些服务器(例如,一个带有 PHP 的网络服务器)并让用户与之通信,所以流程是:

This approach would need that you have to implement some server (for instance, a web server with PHP) and make the users communicate with it, so the flow would be:

  1. 您的服务器向所有已注册的设备发送一条广播消息,告诉它们必须针对 GCM 服务进行注册.

设备收到消息,然后您实现 BroadcastReceiver 以获取纬度和经度并将其发送到您的远程服务器,例如 http://www.mysuperserver.com/getlonglat.php,通过 POST 请求.

The devices get the message, and you implement the BroadcastReceiver to get both latitude and longitude and send it to your remote server, say http://www.mysuperserver.com/getlonglat.php, via a POST request.

您的服务器接受这两个参数,您可以保存它们或执行任何您需要的操作.

Your server takes both parameters and you save them or do whatever you need.

如果您想遵循这种方法,以下是您必须遵循的步骤:

If you want to follow this approach, this are the steps you have to follow:

  1. 转到 https://console.developers.google.com.使用您的 Google 帐户注册一个新项目.这将为您提供 API 密钥和发件人 ID.

  1. Go to https://console.developers.google.com. With your Google account, register a new project. This will provide you an API key and a Sender ID.

您需要在 GCM 中针对您的项目注册设备.您可以通过在您的项目中导入 Google Play 服务 来实现这一点,完成后,执行类似的操作:

You'll need to make the device register against your project in GCM. You can achieve this by importing Google Play Services within your project, and once done, do something alike to this:

GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
final String regid = gcm.register("YOUR_SENDER_ID");

  • 此时GCM服务器已经知道这个用户已经在你的项目中注册了,并且给了他一个regid.

  • At this time, the GCM server already knows that this user has registered whithin your project and has given him a regid.

    下一步是通知您自己的远程服务器用户已经这样做了,并将它所提供的 regid 保存在某处,以便您稍后可以向他们发送消息.因此,在客户端,对您的服务器发出 HTTP POST 请求,发送该 regid 并使用如下方式处理它:

    The next step is informing your own remote server that the user has done so, and save somewhere the regid that it has been given, so you can later send them messages. So in the client side, make a HTTP POST request against your server sending that regid and process it with somethat like this:

    $regid = $_POST['regid'];
    
    if (($regId) && ($ipAddr))
      sql_query("INSERT INTO gcm_subscribers(regid) VALUES($regid')");
    

  • 一旦完成,您就会知道谁已经注册了您的数据库.您可以SELECT 所有用户并向他们发送信号以将他们的坐标发回给您.在以下示例中,参数首先是一组注册 ID,其次是要发送的消息(例如,$messageData = "sendMeYourCoords!").

  • Once done, you know who have already registered against your database. You can SELECT all the users and send them a signal to send you back their coordinates. In the following example, the parameters would be, firstly, an array of registration IDs and secondly the message to send (for instance, $messageData = "sendMeYourCoords!").

    function sendNotification($registrationIdsArray, $messageData) {
      $apiKey = "YOUR_API_KEY";
    
      $headers = array("Content-Type:" . "application/json", "Authorization:" . "key=" . $apiKey);
      $data = array(
        'data' => $messageData,
        'registration_ids' => $registrationIdsArray
      );
    
      $ch = curl_init();
    
      curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
      curl_setopt( $ch, CURLOPT_URL, "https://android.googleapis.com/gcm/send" );
      curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
      curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
      curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
      curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode($data) );
    
      $response = curl_exec($ch);
      curl_close($ch);
    
      return $response;
    }
    

  • 一旦进入您的客户端,只需处理该 GCM 消息并将另一个 POST 发送到远程服务器上的另一个 URL 并通过它是 longitudelatitude.为了处理这些消息,我在下面添加了几个链接.

  • Once in your client, just process that GCM message and make another POST to another URL on your remote server and pass it the longitude and latitude. To process the messages I'm including a few links below.

    为了在没有 GPS 的情况下获取坐标似乎没有太多解决方案,但有一些.这将是一个例子:如何获得纬度Android 中不使用 GPS 的经度值?

    And for getting the coordinates without GPS seems there aren't too much solutions, but there are some. This would be an example: How to get Latitute Longitude value without using GPS in Android?

    更多相关信息:

    使用 Google Cloud 的 Android 推送通知消息 GCM - Android 示例

    使用 PHP 的 Google 云消息传递

    PHP(服务器)和 Android(客户端)之间使用 HTTP 和 JSON 的连接

    通知推送 Android:谷歌云消息传递 (GCM).实施客户(新版)(西班牙语)

    这篇关于如何在需要时在服务器上发送设备的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆