安卓latiud和longitud [英] android latiud and longitud

查看:129
本文介绍了安卓latiud和longitud的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要存储Latitud,Longitud在字符串中的GPS,所以我就可以每次更改位置发送给我的蟒蛇服务器与我的新位置,使用开放插座!因此任何帮助,以保存该latitud和longitud!这里是我的code

 包letget.h;

进口android.app.Activity;
进口android.content.Context;
进口android.location.Location;
进口android.location.LocationListener;
进口android.location.LocationManager;
进口android.os.Bundle;
进口android.widget.Toast;

公共类letget扩展活动
{
  / **第一次创建活动时调用。 * /
  @覆盖
  公共无效的onCreate(包savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);

    / *使用LocationManager类获取GPS位置* /
    LocationManager mlocManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
    LocationListener的mlocListener =新MyLocationListener();
    mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,mlocListener);
  }

  / *类我的位置监听* /
  公共类MyLocationListener实现LocationListener的
  {
    @覆盖
    公共无效onLocationChanged(位置LOC)
    {
      loc.getLatitude();
      loc.getLongitude();
      字符串文本=我目前的位置是:+
                    Latitud =+ loc.getLatitude()+
                    Longitud =+ loc.getLongitude();
      Toast.makeText(getApplicationContext(),
                      文本,
                      Toast.LENGTH_SHORT).show();
    }

    @覆盖
    公共无效onProviderDisabled(字符串提供商)
    {
      Toast.makeText(getApplicationContext(),
                      全球定位系统已禁用,
                      Toast.LENGTH_SHORT).show();
    }

    @覆盖
    公共无效onProviderEnabled(字符串提供商)
    {
       Toast.makeText(getApplicationContext(),
                       GPS功能的
                       Toast.LENGTH_SHORT).show();
    }

    @覆盖
    公共无效onStatusChanged(字符串商,INT地位,捆绑演员)
    {

    }

  } / *类MyLocationListener结束* /

} / *的UseGps活动结束* /
 

解决方案

您将需要

 <使用-权限的Andr​​oid:名称=android.permission.ACCESS_FINE_LOCATION/>
 

在你的Andr​​oidManifest.xml中获得访问GPS位置。

在code得到的字符串看起来正确的给我。将其发送给一个套接字,你要添加

 导入java.net。*
 

然后,假设你的Python服务器监听端口的命令,做一些像

 字符串CMD([该命令发送]);

InetSocketAddress地址=新的InetSocketAddress([YOUR_URL],[YOUR_PORT]);
DatagramPacket类要求=新的DatagramPacket(cmd.getBytes(),cmd.length(),地址);
DatagramSocket的插座=新的DatagramSocket();
socket.send(要求);
 

(捕捉插槽和IO例外)。该命令发送显然依靠什么在另一端的python脚本被监听。你也可以使用类似的HttpConnection和后的值了。

i want to store the Latitud , Longitud in the gps in a string so i will be able to send each change position to my python server with my new location using the open socket !! so any help to save this latitud and longitud !! here is my code

package letget.h;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;

public class letget extends Activity
{
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    /* Use the LocationManager class to obtain GPS locations */
    LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    LocationListener mlocListener = new MyLocationListener();
    mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
  }

  /* Class My Location Listener */
  public class MyLocationListener implements LocationListener
  {
    @Override
    public void onLocationChanged(Location loc)
    {
      loc.getLatitude();
      loc.getLongitude();
      String Text = "My current location is: " +
                    "Latitud ="+ loc.getLatitude() +
                    "Longitud =" + loc.getLongitude();
      Toast.makeText( getApplicationContext(),
                      Text,
                      Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onProviderDisabled(String provider)
    {
      Toast.makeText( getApplicationContext(),
                      "Gps Disabled",
                      Toast.LENGTH_SHORT ).show();
    }

    @Override
    public void onProviderEnabled(String provider)
    {
       Toast.makeText( getApplicationContext(),
                       "Gps Enabled",
                       Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras)
    {

    }

  }/* End of Class MyLocationListener */

}/* End of UseGps Activity*/

解决方案

You'll need

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

in your AndroidManifest.xml to get access to the GPS location.

The code to get the string looks correct to me. To send it to a socket, you'd want to add

import java.net.*

and then, assuming your python server is listening for a command on a port, do something like

String cmd("[THE COMMAND TO SEND]");

InetSocketAddress address = new InetSocketAddress("[YOUR_URL]", [YOUR_PORT]);
DatagramPacket request = new DatagramPacket(cmd.getBytes(), cmd.length(), address);
DatagramSocket socket = new DatagramSocket();
socket.send(request);

(catching Socket and IO exceptions). The command to send would obviously depend on what the python script at the other end is listening for. You could also use something like HTTPConnection and post the values to it.

这篇关于安卓latiud和longitud的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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