当前位置不会自动显示到文本视图 [英] Current Location not auto display to the textview

查看:86
本文介绍了当前位置不会自动显示到文本视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我想获取用户当前的经度和纬度位置,但是我的代码可以获取,直到我在模拟器设置中按发送"按钮为止,但我想要的是自动获取位置并将其设置为textview,所以应该我愿意?任何人都请建议我解决此问题,因为我真的不熟悉位置代码.

Currently I want to get the user current location of latitude and longitude but my code can get until I pressed the send button in the emulator setting but what I want is to auto get the location and set it to the textview so what should I do? Anyone please advice me to solve this problem please because I really not familiar with the location code.

这是我的代码

package com.example.rex.ota30;

import android.*;
import android.Manifest;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import static com.example.rex.ota30.R.id.textView;

public class Search extends AppCompatActivity implements LocationListener {

private FirebaseDatabase db;
private DatabaseReference sref, lref;
private TextView searchun, searchlo, searchla;
private String un, la, lo;
private LocationManager lm;
private LocationListener ll;

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

    db = FirebaseDatabase.getInstance();

    sref = db.getReference("users");
    lref = db.getReference("userslocation");

    searchun = (TextView) findViewById(R.id.sun);
    Intent i = this.getIntent();
    un = i.getStringExtra("username");
    searchun.setText(un);

    searchlo = (TextView) findViewById(R.id.slo);
    searchla = (TextView) findViewById(R.id.sla);

    lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

        return;
    }
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, (LocationListener) this);
}

@Override
public void onLocationChanged(Location location) {

    la = String.valueOf(location.getLatitude());
    searchla.setText(la);

    lo = String.valueOf(location.getLongitude());
    searchlo.setText(lo);

    lref.child(un).child("latitude").setValue(la);
    lref.child(un).child("longitude").setValue(lo);
}

@Override
public void onProviderDisabled(String provider) {
    Log.d("Latitude","disable");
}

@Override
public void onProviderEnabled(String provider) {
    Log.d("Latitude","enable");
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    Log.d("Latitude","status");
}
}

这是到达地点的示例.

Here's the sample of the getting location.

推荐答案

仅在位置更改时调用OnLocationChanged.
因此,仅当按下发送按钮时,该位置才会显示在Textview中.
如果要从头开始显示位置,请尝试

OnLocationChanged is only called when the location has changed.
So the location is displayed in the Textview only when the send button is pressed.
If you want to display the location from the beginning, try this

protected void onCreate(Bundle savedInstanceState) {
  ...
  lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, (LocationListener) this);
  ...
  // add code
  Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
  la = String.valueOf(location.getLatitude());
  searchla.setText(la);

  lo = String.valueOf(location.getLongitude());
  searchlo.setText(lo);
}

这篇关于当前位置不会自动显示到文本视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆