android latiud and longitud
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.
You also need to convert loc.getLatitude from double to a string. Double.toString(loc.getLatitude());
精彩评论