如何在查询字符串中发送我的GPS位置(纬度,经度)?

时间:2014-03-31 14:46:14

标签: android gps query-string

我需要为Android制作一个简单的应用程序,每隔25秒将智能手机的位置发送到Web应用程序。我的网络应用程序在线,现在我可以像这样手动传递值:

  

http://mywebapp.com/coordinates/create?latitude=18.463108&longitude=-69.929117

我是Android开发的绝对初学者,所以请尝试一步一步地解释我。

2 个答案:

答案 0 :(得分:8)

经过深入研究后,我发现我认为最简单的方法是在查询字符串中发送GPS位置(纬度,经度)。也许网上没有比此更短的代码。所以我们将分两步完成:

  1. 首先将此权限添加到AndroidManifest.xml

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    
  2. 现在,您在MainActivity.java内的代码public class MainActivity extends Activity { }

    public class MainActivity extends Activity {
    
        public double latitude;
        public double longitude;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_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) {
                latitude = loc.getLatitude();
                longitude = loc.getLongitude();
                String Text = "My current Latitude = " + latitude  + " Longitude = " + longitude;
                Toast.makeText( getApplicationContext(),Text,Toast.LENGTH_SHORT).show();
    
                SendQueryString(); // for send the  Query String of latitude and logintude to the webapp.
            }
    
    
    
            @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 */
    
    
        public void SendQueryString() {
            new Thread() {  
                public void run() {
    
                    String url = "http://mywebapp.com/coordinates/create?latitude=" + latitude +"&longitude=" + longitude;
    
                    try {
                        HttpClient Client = new DefaultHttpClient();
                        HttpGet httpget = new HttpGet(url);
                        Client.execute(httpget);
                    }
                    catch(Exception ex) {
                        String fail = "Fail!";
                        Toast.makeText( getApplicationContext(),fail,Toast.LENGTH_SHORT).show();
                    }
                }
            }.start();
        }
    }
    
  3. 注意:正如您所看到的,我在函数new Thread() { public void run() {/*code here*/}}.start();中使用SendQueryString(),这是因为我们正在尝试向服务器发出请求,所以在Android无法向主体线程中的服务器发出请求,其中图形和主体进程是。实际上,如果您尝试在主线程中发出请求,它将返回错误。

答案 1 :(得分:0)

首先到达位置:

LocationManager locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locMan.requestLocationUpdates(
    LocationManager.GPS_PROVIDER, 0, 0, location listener);

第一个零表示位置更新的时间间隔 第二个代表更新的距离间隔..配置就像你一样。

在听众中:

public void onLocationChanged(Location location) {
if (location != null) {
    double lat = location.getLatitude();
    double long = location.getLongitude();
   }
}

您只需要使用该链接发出http请求,如下所示:

String url = "http://mywebapp.com/coordinates/createlatitude=" + lat +"&longitude=" + long;
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
httpClient.execute(httpGet);