【问题标题】:Android Development displaying and send lat and lon to databaseAndroid 开发显示和发送纬度和经度到数据库
【发布时间】:2016-03-15 12:12:40
【问题描述】:

我正在开发一个允许用户从 MySQL 数据库添加/更新/删除记录的项目。该应用程序成功上传并从数据库中检索所有信息。我试图在我从模拟器控件发送值后立即显示经纬度和经度,而不是用户必须通过文本字段手动将值放入自己,这就是我最初编程的方式。显示后,值应与其他文本字段值一起插入到数据库中。我希望这是有道理的。现在当我从模拟器控件发送值时,它们没有显示在 textView 中,我不知道为什么?任何帮助将不胜感激。

更新:这是初始化 lat 和 long 视图的地方,应该从模拟器控件获取值?

//Defining views
//private EditText editTextLatitude;
//private EditText editTextLongitude;
private EditText editTextTimeInserted;
private EditText editTextUserID;
private TextView latituteField;
private TextView longitudeField;
private LocationManager locationManager;
private String provider;

private Button buttonAdd;
private Button buttonView;

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

    //Initializing views
    latituteField = (TextView) findViewById(R.id.TextView02);
    longitudeField = (TextView) findViewById(R.id.TextView04);
    //editTextLatitude = (EditText) findViewById(R.id.editTextLat);
    //editTextLongitude = (EditText) findViewById(R.id.editTextLon);
    editTextTimeInserted = (EditText) findViewById(R.id.editTextTimeInserted);
    editTextUserID = (EditText) findViewById(R.id.editTextUserID);

    buttonAdd = (Button) findViewById(R.id.buttonAdd);
    buttonView = (Button) findViewById(R.id.buttonView);

    //Setting listeners to button
    buttonAdd.setOnClickListener(this);
    buttonView.setOnClickListener(this);

    // Get the location manager
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    // Define the criteria how to select the location provider -> use
    // default
    Criteria criteria = new Criteria();
    provider = locationManager.getBestProvider(criteria, false);
    Location location = locationManager.getLastKnownLocation(provider);

    // Initialize the location fields
    if (location != null) {
        System.out.println("Provider " + provider + " has been selected.");
        onLocationChanged(location);
    } else {
        latituteField.setText("Location not available");
        longitudeField.setText("Location not available");
    }
 }

 public void onLocationChanged(final Location location) {
    int lat = (int) (location.getLatitude());
    int lng = (int) (location.getLongitude());
    latituteField.setText(String.valueOf(lat));
    longitudeField.setText(String.valueOf(lng));

  }

【问题讨论】:

  • 其他任何其他细节?你能缩小范围并告诉你在哪里吗?是来给客户的吗?没有人愿意学习你的完整代码
  • 它在我运行应用程序时显示 textViews,但是当我从模拟器发送 lat 和 long 的值时,它们没有显示它们,请留意上面的更新,我会缩小范围这个@IliiazAkhmedov 的代码,我也很陌生,所以如果我没有马上得到任何东西,请多多包涵..

标签: java android mysql


【解决方案1】:

在 manifest.xml 中添加
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

// Create an instance of GoogleAPIClient.

if (mGoogleApiClient == null) {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
    .addConnectionCallbacks(this)
    .addOnConnectionFailedListener(this)
    .addApi(LocationServices.API)
    .build();
}

@Override
public void onConnected(Bundle connectionHint) {
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
            mGoogleApiClient);
    if (mLastLocation != null) {
        mLatitudeText.setText(String.valueOf(mLastLocation.getLatitude()));
        mLongitudeText.setText(String.valueOf(mLastLocation.getLongitude()));
    }
}

follow the link

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    相关资源
    最近更新 更多