2017-06-23 82 views
1

我在按navigationViewR.id.create_marker)中的選項時遇到問題我想在我的MapFragment中創建標記,但我無法做到,有什麼建議嗎?NavigationDrawer + Google地圖事件

MainActivity.java代碼,我按下菜單選項:

navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { 
@Override 
public boolean onNavigationItemSelected(@NonNull MenuItem item) { 
    switch (item.getItemId()) { 
     case R.id.create_marker: 
     Toast.makeText(MainActivity.this, "Crear Marker", Toast.LENGTH_SHORT).show(); 
     // Create marker and display it on the map 
     break; 
    } 
    return false; 
} 
}); 

MapFragment.java代碼:

public class MapFragment extends Fragment 
    implements OnMapReadyCallback { 

private View rootView; 
private GoogleMap gMap; 
private MapView mapView; 

public MapFragment() { 
    // Required empty public constructor 
} 


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

@Override 
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 
    mapView = (MapView) rootView.findViewById(R.id.map); 
    if (mapView != null) { 
     mapView.onCreate(null); 
     mapView.onResume(); 
     mapView.getMapAsync(this); 
    } 
} 

@Override 
public void onResume() { 
    super.onResume(); 
} 

@Override 
public void onMapReady(GoogleMap googleMap) { 
} 
} 

這是我的項目的結構:

enter image description here

回答

0

要創建一個標記,您需要調用addMarker方法在你的地圖,一旦它已經可以使用。

public void onMapReady(GoogleMap googleMap) { 
     // Add a marker in Sydney, Australia, 
     // and move the map's camera to the same location. 
     LatLng sydney = new LatLng(-33.852, 151.211); 
     googleMap.addMarker(new MarkerOptions().position(sydney) 
       .title("Marker in Sydney")); 
     googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); 
    } 

如果你希望你的地圖中打開一個固定的緯度和經度的標記,在座標傳遞,如上面的代碼,否則通過谷歌的位置API請求的位置,並通過獲得的經緯度值addMarker()方法。

相關問題