2016-10-21 25 views
0

我有此EventActivity其中我有地圖與標誌物:Android - 如何從外部OnMapReady函數獲取經度和緯度值?

public class EventActivity extends AppCompatActivity implements OnMapReadyCallback { 

private double latitude; 
private double longitude; 
private GoogleMap eventMap; 

    @Override 
    public void onCreate(Bundle savedInstanceState){ 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_event); 

     // Declares map fragment. 
     SupportMapFragment eventMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.activity_event_map); 
     eventMapFragment.getMapAsync(this); 

     // Gets latitude and longitude values from database. 

      // (things happen here and I get the values correctly) 

      latitude = 12.4567785 
      longitude = 25.7773665 
    } 

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

     // Sets map position. 
     LatLng position = new LatLng(latitude, longitude); 
     eventMap.addMarker(new MarkerOptions().position(position)); 
     eventMap.moveCamera(CameraUpdateFactory.newLatLng(position)); 
    } 

} 
  • OnCreate中()我得到的對雙值正確的。我甚至可以舉杯祝酒,並向他們展示。

  • 的問題是,當我想將它們設置爲我的地圖上OnMapReady()標記它得到什麼或位置。

如何正確地將值從OnCreate()傳遞給OnMapReady()?

編輯:我使用Firebase作爲我的數據庫。

回答

3

這樣分配經/緯度值後也許初始化地圖片段:

public class EventActivity extends AppCompatActivity implements OnMapReadyCallback { 

private double latitude; 
private double longitude; 
private GoogleMap eventMap; 

@Override 
public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_event); 

    // Gets latitude and longitude values from database. 

     // (things happen here and I get the values correctly) 

     latitude = 12.4567785 
     longitude = 25.7773665 

    // Declares map fragment. 
    SupportMapFragment eventMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.activity_event_map); 
    eventMapFragment.getMapAsync(this); 

} 

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

    // Sets map position. 
    LatLng position = new LatLng(latitude, longitude); 
    eventMap.addMarker(new MarkerOptions().position(position)); 
    eventMap.moveCamera(CameraUpdateFactory.newLatLng(position)); 
} 

}

+0

感謝但仍然不工作先生:(我想知道這是否是一種正常的行爲......我認爲某種解決方案可能是編輯OnMapReady超級方法,但我不能這樣做......所以在這裏我被困住了 –

+1

如果你在onCreate中使用整個活動範圍的變量,它們應該已經可以在onMapReady中訪問了,也許還可以嘗試移除eventMap變量並直接訪問googleMap參數。 –

0

我需要解決類似的問題,目前有這樣的解決方案在我的應用程序工作。我想從我的位置添加一條新的多段線到有問題的標記,因此想讓setOnMarkerListener有權訪問我的位置。這是我如何解決它:

步驟1. 我創建了兩個全球雙打如下:

private Double myLatitude = -33.865143; 
private Double myLongitude = 151.209900; 

我給他們悉尼的默認值,以避免爲我添加折線到任何錯誤的NullPointerException折線陣列。 步驟2. 以我onLocationChanged方法我更新的值:

@Override 
public void onLocationChanged(Location location) { 
    myLatitude = location.getLatitude(); 
    myLongitude = location.getLongitude(); 
.... 

現在,在步驟3我想創造從mylocation一個新行到所選擇的標記,以便用下面的代碼:

mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() { 
      @Override 
      public boolean onMarkerClick(Marker marker) { 
       marker.showInfoWindow(); 
       areaText.setText(marker.getTitle()); 
       distanceText.setText("0 Meters"); 
       markerLatitude = marker.getPosition().latitude; 
       markerLongitude = marker.getPosition().longitude; 

       lines.add(mMap.addPolyline(new PolylineOptions() 
         .add(new LatLng(myLatitude, myLongitude), 
           new LatLng(marker.getPosition().latitude, marker.getPosition().longitude)) 
         .width(10) 
         .color(Color.RED))); 
       if (lines.size() > 1) 
        lines.remove((lines.size()-1)); 
       return true; 
      } 


     }); 

希望這可以幫助有同樣問題的人。