2017-07-12 33 views
1

我需要從MainActivity添加標記到Android谷歌地圖(片段)android - 添加標記到片段地圖從活動

這是我的代碼;

class Map extends android.app.Fragment implements OnMapReadyCallback 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.activity_mapa, container,false); 
    } 

    @Override 
    public void onViewCreated(View view, Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 

     MapFragment fragment = (MapFragment)getChildFragmentManager().findFragmentById(R.id.map); 
     fragment.getMapAsync(this); 
    } 

    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     LatLng marker = new LatLng(19.33978502, -99.19086277); 
     googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(marker, 15)); 
     googleMap.addMarker(new MarkerOptions().title("Marca de Prueba 1").position(marker)); 
    } 
} 

,我想從這裏添加標記:

所有這一切,因爲我想從我的主要活動在這裏我有一些按鈕和EditText上

MainActivity

我的地圖互動
public class MainActivityextends AppCompatActivity 
    implements NavigationView.OnNavigationItemSelectedListener 
{ 

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

    FragmentManager fm = getFragmentManager(); 
    fm.beginTransaction().replace(R.id.content_frame, new Mapa()).commit(); 
} 

Here is how it looks, already has a mark, but insted of calling it from the map activity i need to set it from the MainActivity

回答

0

你只需要能夠從活動被稱爲片段來定義一個公共方法:

class Mapa extends android.app.Fragment implements OnMapReadyCallback 

    GoogleMap mMap; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.activity_mapa, container,false); 
    } 

    @Override 
    public void onViewCreated(View view, Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 

     MapFragment fragment = (MapFragment)getChildFragmentManager().findFragmentById(R.id.map); 
     fragment.getMapAsync(this); 
    } 

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

    //Added public method to be called from the Activity 
    public void placeMarker(String title, double lat, double lon) { 
     if (mMap != null) { 
     LatLng marker = new LatLng(lat, lon); 
     mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(marker, 15)); 
     mMap.addMarker(new MarkerOptions().title(title).position(marker)); 
     } 
    } 
} 

然後,在活動中,保持一個參考片段,這樣就可以調用placeMarker()方法:

public class MainActivity extends AppCompatActivity 
    implements NavigationView.OnNavigationItemSelectedListener { 

    private Mapa mMapFragment; 

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

     mMapFragment = new Mapa(); 
     FragmentManager fm = getFragmentManager(); 
     fm.beginTransaction().replace(R.id.content_frame, mMapFragment).commit(); 
    } 

    private void placeMarkerInMap(String title, double lat, double lon) { 
    if (mMapFragment != null) { 
     mMapFragment.placeMarker(title, lat, lon); 
    } 
    } 
} 

然後,當用戶填寫EditText並單擊按鈕時,您可以在Activity中調用placeMarkerInMap()方法。

請注意,如果您在執行onMapReady()回調之前嘗試調用此方法,則不會有有效的GoogleMap引用用於放置標記。 如果您需要在活動初始​​啓動時放置標記,則需要在FragmentTransaction中使用參數。 See here for more details

+0

謝謝! 我做了你說的,但是當我打電話給placeMarker方法的主要acticity它說,無法解決。 :有什麼想法? – JosCarrillo

+0

我的片段被稱爲Mapa * – JosCarrillo

+0

@JosCarrillo我看到了什麼問題。我剛剛更新了答案。聲明實際上應該是'private Mapa mMapFragment;',改變它並且它將起作用。 –