Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在firebase实时数据库中存储gps位置_Java_Android_Firebase_Firebase Realtime Database - Fatal编程技术网

Java 在firebase实时数据库中存储gps位置

Java 在firebase实时数据库中存储gps位置,java,android,firebase,firebase-realtime-database,Java,Android,Firebase,Firebase Realtime Database,我需要将我的位置存储到firebase实时数据库中。以下代码正在运行,仅在Android应用程序中显示位置。我如何将此位置发送到firebase public class MainActivity extends AppCompatActivity { Button button; TextView textView; @Override protected void onCreate(Bundle savedInstanceState) {

我需要将我的位置存储到firebase实时数据库中。以下代码正在运行,仅在Android应用程序中显示位置。我如何将此位置发送到firebase

public class MainActivity extends AppCompatActivity {

    Button button;
    TextView textView;

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

        button = (Button) findViewById(R.id.button);
        textView = (TextView) findViewById(R.id.textView);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {              //check for sdk version
           if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ) {
               if (checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION)
                       != PackageManager.PERMISSION_GRANTED && checkSelfPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION)
                       != PackageManager.PERMISSION_GRANTED) {

                   requestPermissions(new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION}, 1000);
               }else{
                   LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                   Location location = locationManager  .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                   try{
                       String city = hereLocation(location.getLatitude(), location.getLongitude());
                       textView.setText(city);

                   }catch (Exception e)
                   { e.printStackTrace();
                       Toast.makeText(MainActivity.this,"Not found!",Toast.LENGTH_LONG).show();
                   }
               }
           }

            }
        });


    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

        switch (requestCode){  //set the text value
            case 1000:{
                if(grantResults[0] == PackageManager.PERMISSION_GRANTED)
                {
                    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                    Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    String city = hereLocation(location.getLatitude(),location.getLongitude());
                    textView.setText(city);
                }else{
                    Toast.makeText(this,"permission is granted!",Toast.LENGTH_LONG).show();
                }
                break;
            }
        }
    }

    private String hereLocation(double lat, double lon){  //access geo location
       String cityName = "";

        Geocoder geocoder= new Geocoder(this ,Locale.getDefault());
        List<Address> addresses;
        try{
            addresses = geocoder.getFromLocation(lat,lon, 10);
            if(addresses.size()> 0){
                for(Address adr:addresses) {

                    if (adr.getLocality() != null && adr.getLocality() .length()> 0) {

                        cityName = adr.getLocality();
                        break;

                    }
                }
            }
        }catch (IOException e){
            e.printStackTrace();
        }

       return cityName;
    }
}
public类MainActivity扩展了AppCompatActivity{
按钮;
文本视图文本视图;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
按钮=(按钮)findViewById(R.id.button);
textView=(textView)findViewById(R.id.textView);
setOnClickListener(新视图.OnClickListener(){
@凌驾
public void onClick(视图v){//检查sdk版本
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.M){
if(checkSelfPermission(android.Manifest.permission.ACCESS\u FINE\u位置)
!=PackageManager.PERMISSION\u已授予和&checkSelfPermission(android.Manifest.PERMISSION.ACCESS\u位置)
!=PackageManager.权限(已授予){
requestPermissions(新字符串[]{android.Manifest.permission.ACCESS\u\u LOCATION},1000);
}否则{
LocationManager LocationManager=(LocationManager)getSystemService(Context.LOCATION\u服务);
Location Location=locationManager.getLastKnownLocation(locationManager.NETWORK\u提供程序);
试一试{
字符串city=hereLocation(location.getLatitude(),location.getLatitude());
textView.setText(城市);
}捕获(例外e)
{e.printStackTrace();
Toast.makeText(MainActivity.this,“未找到!”,Toast.LENGTH_LONG.show();
}
}
}
}
});
}
@凌驾
public void onRequestPermissionsResult(int-requestCode,@NonNull-String[]permissions,@NonNull-int[]grantResults){
开关(requestCode){//设置文本值
案例1000:{
if(grantResults[0]==已授予PackageManager.权限)
{
LocationManager LocationManager=(LocationManager)getSystemService(Context.LOCATION\u服务);
Location Location=locationManager.getLastKnownLocation(locationManager.NETWORK\u提供程序);
字符串city=hereLocation(location.getLatitude(),location.getLatitude());
textView.setText(城市);
}否则{
Toast.makeText(此“已授予权限!”,Toast.LENGTH_LONG.show();
}
打破
}
}
}
专用字符串位置(双lat,双lon){//访问地理位置
字符串cityName=“”;
Geocoder Geocoder=新的Geocoder(这个,Locale.getDefault());
列出地址;
试一试{
地址=地理编码器。getFromLocation(lat,lon,10);
如果(地址.size()>0){
地址(adr:地址){
if(adr.getLocality()!=null&&adr.getLocality().length()>0){
cityName=adr.getLocation();
打破
}
}
}
}捕获(IOE异常){
e、 printStackTrace();
}
返回城市名称;
}
}

城市名称正在使用文本视图存储。它将给出所定位设备的当前城市。我需要将此位置发送到firebase实时数据库。

您可以使用firebase数据库和DatabaseReference类将位置发送到firebase。首先,您必须确保build.gradle中包含以下SDK:

implementation 'com.google.firebase:firebase-storage:16.0.4'
implementation 'com.google.firebase:firebase-database:16.0.4'
此外,请确保已在Firebase控制台中设置实时数据库。最好选择测试选项,这样任何人都可以向数据库写入数据。如果您以前从未使用过Firebase

在依赖项中添加代码后,请执行以下操作:

FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
DatabaseReference databaseReference = firebaseDatabase.getReference("Locations"); 
//Instead of "Locations" you can say something else. Locations will be the name of your path where the location is stored.
//Create a Hashmap to store the information with a key and a value:
Hashmap<String, String> values = new Hashmap<>();
values.put("Location", city);
databaseReference.push().setValue(values);
FirebaseDatabase-FirebaseDatabase=FirebaseDatabase.getInstance();
DatabaseReference DatabaseReference=firebaseDatabase.getReference(“位置”);
//你可以说些别的,而不是“地点”。位置将是存储位置的路径的名称。
//创建Hashmap以使用键和值存储信息:
Hashmap值=新的Hashmap();
价值。put(“位置”,城市);
databaseReference.push().setValue(值);
您可以添加onSuccessListener或onFailureListener以查看其是否有效。
如果不起作用,请检查此项。

您的代码显示尚未与Firebase交互。我建议开始使用,或。如果你被困在那里的一个混凝土台阶上,那就分享一下。