Android - 如何向服务器发送连续的经度和纬度?

时间:2016-02-13 07:19:29

标签: gps server location latitude-longitude

我不断获取当前位置,现在我想每隔1到5秒将此连续位置发送到服务器。我该怎么做?

Android代码:

public class MainActivity extends Activity implements GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener, LocationListener {

TextView txtOutputLat, txtOutputLon;
Location mLastLocation;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
String lat, lon;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    txtOutputLat = (TextView) findViewById(R.id.textView);
    txtOutputLon = (TextView) findViewById(R.id.textView2);


    buildGoogleApiClient();




}


@Override
public void onConnected(Bundle bundle) {


    mLocationRequest = LocationRequest.create();
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setInterval(100); // Update location every second

    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);


    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
            mGoogleApiClient);
    if (mLastLocation != null) {
        lat = String.valueOf(mLastLocation.getLatitude());
        lon = String.valueOf(mLastLocation.getLongitude());

    }
    updateUI();
}

@Override
public void onConnectionSuspended(int i) {

}


@Override
public void onLocationChanged(Location location) {
    lat = String.valueOf(location.getLatitude());
    lon = String.valueOf(location.getLongitude());
    updateUI();
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    buildGoogleApiClient();
}

synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();


}

@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    mGoogleApiClient.disconnect();
}

void updateUI() {
    txtOutputLat.setText(lat);
    txtOutputLon.setText(lon);
}
}

0 个答案:

没有答案