我在我的Android应用程序上使用LOcation服务,并且我已将两个' location.getLatitude()'和' location.getLongitude()'在适当的位置。运行后,坐标显示在屏幕上。 我担心的是,我需要将坐标发送到我的localhost(即使它们被自动刷新)。 下面是我的picklocation.java样本
package com.example.vic.cdmes_;
import android.Manifest;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Calendar;
public class pickLocation extends AppCompatActivity {
Button buttonLocation;
TextView viewCoordinates;
private LocationListener locationListener;
private LocationManager locationManager;
Button button;
int year_x, month_x, day_x;
static final int DIALOG_ID = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pick_location);
//portion for the date picker
final Calendar cal = Calendar.getInstance();
year_x = cal.get(Calendar.YEAR);
month_x = cal.get(Calendar.MONTH);
day_x = cal.get(Calendar.DAY_OF_MONTH);
ShowDialogForDateOnClick();
//portion for the gps picker
buttonLocation = (Button) findViewById(R.id.locateme);
viewCoordinates = (TextView)findViewById(R.id.viewCoordinates);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {//called whenever the location is updated
viewCoordinates.append("\n "+location.getLatitude()+ " "+location.getLongitude());//displays the value of latitude and longitude
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {//called to enable user put GPS on, will take the user to settings panel, if not.
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
};
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
requestPermissions( new String[]{
Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.INTERNET
},10);
return;
}
}
else {
configureButton();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode){
case 10:
if(grantResults.length>0 && grantResults[0]== PackageManager.PERMISSION_GRANTED)
configureButton();
return;
}
}
private void configureButton() {
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
locationManager.requestLocationUpdates("gps", 5000, 0, locationListener);
}
});
}
public void ShowDialogForDateOnClick()
{
button= (Button)findViewById(R.id.pickdate);
button.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DIALOG_ID);
}
}
);
}
@Override
protected Dialog onCreateDialog(int id)
{
if(id== DIALOG_ID)
return new DatePickerDialog(this, dpickerListener,year_x,month_x,day_x);
return null;
}
private DatePickerDialog.OnDateSetListener dpickerListener = new DatePickerDialog.OnDateSetListener()
{
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth){
year_x=year;
month_x=monthOfYear + 1;
day_x=dayOfMonth;
Toast.makeText(pickLocation.this,day_x +"/"+month_x+"/"+year_x, Toast.LENGTH_LONG).show();
}
};
}
如何将以下内容发送到我的数据库?
public void onLocationChanged(Location location) {//called whenever the location is updated
viewCoordinates.append("\n "+location.getLatitude()+ " "+location.getLongitude());//displays the value of latitude and longitude
}
答案 0 :(得分:0)
在名为webservice或rest api的onLocationChanged方法中,在服务器上发送latlong并存储数据。您正在使用httpclient或volley lib来调用Web服务。