2017-04-26 72 views
1

我試圖創建應用程序以在按下按鈕時存儲Firebase上的位置,並從Firebase檢索位置並顯示地圖中的所有針腳。我已經能夠將位置保存到位置具有經度和緯度的孩子的firebase中,但我不知道如何獲取值和引腳。我試圖按照firebase在其手冊上的方式進行操作,但沒有奏效。任何人都可以幫助解決問題嗎?從Firebase中檢索位置並將標記放在Google Maps API上for Android

這是我到目前爲止的代碼:

public class MapsActivity extends FragmentActivity implements 
OnMapReadyCallback { 
private GoogleMap mMap; 
private final static int MY_PERMISSION_FINE_LOCATION = 101; 
private Button mSaveButton; 
private DatabaseReference mDatabase; 
private DatabaseReference refDatabase; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_maps); 
// Obtain the SupportMapFragment and get notified when the map is ready to 
be used. 
    SupportMapFragment mapFragment = (SupportMapFragment) 
    getSupportFragmentManager() 
      .findFragmentById(R.id.map); 
    mapFragment.getMapAsync(this); 
    mSaveButton = (Button) findViewById(R.id.Savebtn); 
    mDatabase = 
FirebaseDatabase.getInstance().getReference().child("Navigation"); 
    refDatabase = 
FirebaseDatabase.getInstance().getReference().child("Location"); 
} 
@Override 
public void onMapReady(GoogleMap googleMap) { 
    mMap = googleMap; 


    mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() { 

     @Override 
     public void onMapLongClick(LatLng point) { 
      // TODO Auto-generated method stub 

      // added marker saved as marker and coordinates passed to latlng 
      Marker marker = mMap.addMarker(new 
      MarkerOptions().position(point)); 
      final LatLng latlng = marker.getPosition(); 

      mSaveButton.setOnClickListener(new View.OnClickListener(){ 
       @Override 
       public void onClick(View view){ 
        DatabaseReference newPost = mDatabase.push(); 
        newPost.child("Location").setValue(latlng); 

       } 
      }); 

      } 
     }); 

     if (ActivityCompat.checkSelfPermission(this, 
    Manifest.permission.ACCESS_FINE_LOCATION) == 
      PackageManager.PERMISSION_GRANTED) { 
     mMap.setMyLocationEnabled(true); 
    } else { 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
      requestPermissions(new String[] 
{Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSION_FINE_LOCATION); 
     } 

    } 


} 

@Override 
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
    super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
    switch (requestCode) { 
     case MY_PERMISSION_FINE_LOCATION: 
      if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
       if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == 
         PackageManager.PERMISSION_GRANTED) { 
        mMap.setMyLocationEnabled(true); 
       } 

      }else { 
       Toast.makeText(getApplicationContext(), "This requires location permissions to be granted", Toast 
         .LENGTH_LONG).show(); 
       finish(); 
      } 
      break; 
    } 
    } 
} 

這個位置被如何保存在火力

Data 
    -Kidp45TdInOM3Bsyu2b 
     Location 
     latitude:19.772613777905196 
     longitude:-9.92741011083126 
    -KidsmTExZY2KjnS7S-b 
     Location 
     latitude: 18.221073689785065 
     longitude: -6.573890447616577 
    -KidvmAgV0bm2uT_Pcdr 
     Location 
     latitude: 14.44608051870992 
     longitude: -6.510856859385967 

回答

0

第一:你NEDD改變結構是這樣的:

Data 
    Location 
    -Kidp45TdInOM3Bsyu2b 
     latitude:19.772613777905196 
     longitude:-9.92741011083126 
    -KidsmTExZY2KjnS7S-b 
     latitude: 18.221073689785065 
     longitude: -6.573890447616577 
    -KidvmAgV0bm2uT_Pcdr 
     latitude: 14.44608051870992 
     longitude: -6.510856859385967 

要做到這一點的方法是:

 mSaveButton.setOnClickListener(new View.OnClickListener(){ 
      @Override 
      public void onClick(View view){ 
       //change to refDatabase or mDatabase.child("Location") 
       DatabaseReference newPost = refDatabase.push(); 
       //the push() command is already creating unique key 
       newPost.setValue(latlng); 

      } 
     }); 

而對於放標誌物,從您的refDatabase地圖代碼:

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

    ... 

    refDatabase.addChildEventListener(new ChildEventListener() { 
    @Override 
    public void onChildAdded(DataSnapshot dataSnapshot, String prevChildKey) { 
      LatLng newLocation = new LatLng(
         dataSnapshot.child("latitude").getValue(Long.class), 
         dataSnapshot.child("longitude").getValue(Long.class) 
        ); 
      mMap.addMarker(new MarkerOptions() 
       .position(newLocation) 
       .title(dataSnapshot.getKey())); 
    } 

    @Override 
    public void onChildChanged(DataSnapshot dataSnapshot, String prevChildKey) {} 

    @Override 
    public void onChildRemoved(DataSnapshot dataSnapshot) {} 

    @Override 
    public void onChildMoved(DataSnapshot dataSnapshot, String prevChildKey) {} 

    @Override 
    public void onCancelled(DatabaseError databaseError) {} 
    }); 
} 
+0

IT口口聲聲說很遺憾停止工作。 – Nis

+0

錯誤信息是什麼?在哪一行? – faruk

+0

我的代碼失敗getValue(LatLng.class);我已經更新了我的答案 – faruk

相關問題