我正在开发一个将 Android 与 Web 应用程序相结合的项目。我想要做的是将GPS坐标从Android手机发送到一个显示位置的网络应用程序,请帮助我编码,因为我不擅长android编码
问问题
127 次
2 回答
0
为此使用位置侦听器
private LocationManager locationManager;
public LocationListener locationListener;
public LocationListener locationListener2;
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener();
locationListener2 = new MyLocationListener();
locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER, 0, 0, locationListener);
locationManager.requestLocationUpdates(locationManager.NETWORK_PROVIDER, 0, 0, locationListener2);
//Location lister
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location loc) {
mlongti = loc.getLongitude();
mlatiti = loc.getLatitude();
GeoPoint userLoc = new GeoPoint((int) (mlatiti * 1E6), (int) (mlongti * 1E6));
LItemizedOverlay itemizedoverlay = new LItemizedOverlay(selfImage,getParent(), "",userLoc,display);
try {
OverlayItem overlayitem = new OverlayItem(userLoc, "", "");
itemizedoverlay.addOverlay(overlayitem);
} catch (Exception e) {
e.printStackTrace();
}
List<Overlay> listOfOverlays = map.getOverlays();
listOfOverlays.add(itemizedoverlay);
MapController mc = map.getController();
mc.animateTo(userLoc);
mc.setZoom(10);
map.invalidate();
if(locationManager!=null && locationListener2 != null){
locationManager.removeUpdates(locationListener);
locationManager.removeUpdates(locationListener2);
locationManager= null;
}
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
于 2013-05-12T18:23:56.740 回答
0
请使用代码获取纬度和经度
公共类 MyLocationListener 扩展服务实现 LocationListener {
private static final String TAG = "MyLocationListener";
private Context context = null;
private Location location = null;
private LocationManager locationManager = null;
public static boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
boolean canGetLocation = false;
public double latitude = 0.0;
public double longitude = 0.0;
public String location_address=null;
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
public MyLocationListener(Context ctx) {
Log.v(TAG+".MyLocationListener", "MyLocationListener constructor called");
this.context = ctx;
getLocationValue();
}
public Location getLocationValue() {
Log.v(TAG+".getLocationValue", "getLocationValue method called");
try {
locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(isGPSEnabled){
this.canGetLocation = true;
Log.v(TAG+".getLocationValue", "GPS provider enabled");
//Toast.makeText(context, "Gps", 1).show();
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
Log.v(TAG,"Gps Co-ordinates are:" + latitude+ " " + longitude);
//Toast.makeText(context, "GPS Co-ordinates are: " + latitude + " "+ longitude, Toast.LENGTH_LONG).show();
//GetAddressFromCoordinates(location);
//setLocationDataToBean();
}
}
}
}
else if(isNetworkEnabled){
//Toast.makeText(context, "Net", 1).show();
Log.v(TAG+".getLocationValue", "Network provider enabled");
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if (locationManager != null)
{
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
Log.v(TAG,"Co-ordinates are: " + latitude + " "+ longitude);
//Toast.makeText(context, "Network Provider Co-ordinates are: " + latitude + " "+ longitude, Toast.LENGTH_LONG).show();
//GetAddressFromCoordinates(location);
//setLocationDataToBean();
}
}
}
else{
showSettingsAlert();
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
/**
* Stop using GPS listener Calling this function will stop using GPS in your
* app
* */
public void stopUsingGPS() {
if (locationManager != null) {
locationManager.removeUpdates(MyLocationListener.this);
}
}
public double getLatitude() {
if (location != null) {
latitude = location.getLatitude();
}
return latitude;
}
public double getLongitude() {
if (location != null) {
longitude = location.getLongitude();
}
return longitude;
}
/**
* Function to check GPS/wifi enabled
*
* @return boolean
* */
public boolean canGetLocation() {
return this.canGetLocation;
}
现在在第二部分将这个经纬度发送到服务器使用 asynctask
私有类 GetResult 扩展 AsyncTask {
@Override
protected String doInBackground(String... urls) {
Log.v(TAG + ".doInBackground", "doInBackground method call");
String response1 = null;
/* for (String url : urls) {
WebHelper webHelper = new WebHelper();
response = webHelper.getResult(url);
Log.v(TAG+".doInBackground", "json response is:" + response);
}*/
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("use web url");
// Log.d("response", "WORKING");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("lat", lat));
nameValuePairs.add(new BasicNameValuePair("long",long));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
InputStream is = response.getEntity().getContent();
WebHelper webHelper = new WebHelper();
response1 = webHelper.convertStreamToString(is);
Log.v(TAG+".doInBackground", "json response is:" + response1);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return response1;
}
@Override
protected void onPostExecute(String result) {
Log.v(TAG + ".onPostExecute", "onPostExecute method call");
dialog.dismiss();
Log.v(TAG+".onPostExecute", "json response is:" + result);
/*
if(msg != null){
// check status here for response
}
于 2013-05-12T18:29:54.073 回答