Android:将Firebase和Google Maps活动连接在一起时出错

时间:2017-03-15 04:50:25

标签: android google-maps firebase firebase-realtime-database

CREATE FUNCTION `get_fuel_name`(v_id INT) 
RETURNS varchar(100) CHARSET latin1
READS SQL DATA
BEGIN

   RETURN coalesce( (SELECT  `specification_value` 
                      FROM  `vehicle_specifications` 
                      WHERE  `specification_id` =1 
                      AND `vehicle_id` =v_id LIMIT 1
                     ), '');
END

我已经编写了上面的代码来创建一个Google Map Activity来显示带有标记的Map,标记的位置是从firebase数据库获取的,但我有运行时错误,如下所示。

package com.example.dhrumit.google_maps;

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;

public class MapsActivity extends FragmentActivity implements      OnMapReadyCallback {

private GoogleMap mMap;
public FirebaseDatabase database;
public DatabaseReference mRef ;
public Query query;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    database = FirebaseDatabase.getInstance();
    mRef = database.getReference().child("users");
    query= mRef.orderByChild("phone");
    SupportMapFragment mapFragment = (SupportMapFragment)  getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    query.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            LatLngBounds.Builder builder = new LatLngBounds.Builder();
            for (DataSnapshot postDataSnapshot : dataSnapshot.getChildren())
            {
                Double Latitude = (Double)       postDataSnapshot.child("latitude").getValue();
                Double Longitude = (Double) postDataSnapshot.child("longitude").getValue();
                String Name =  (String) postDataSnapshot.child("name").getValue();
                LatLng Location = new LatLng(Latitude, Longitude);
                mMap.addMarker(new MarkerOptions().position(Location).title(Name));
                builder.include(Location);
            }
            LatLngBounds bounds = builder.build();
            CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(bounds,0);
            mMap.animateCamera(cameraUpdate);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

1 个答案:

答案 0 :(得分:0)

这是由于 Firebase数据库&的依赖关系的不同版本所致。 Google Play服务。使它们相同或使用以下依赖项,问题将得到解决。

compile 'com.google.firebase:firebase-database:11.4.2'
compile 'com.google.firebase:firebase-auth:11.4.2'
compile 'com.google.android.gms:play-services-maps:11.4.2'