NavigationDrawer + Google地图事件

时间:2017-06-23 06:00:48

标签: android google-maps

我在R.id.create_markerMapFragment)中按下某个选项时遇到问题我想在MainActivity.java中创建一个标记,但我不能这样做,有什么建议吗?< / p>

代码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) { } }

{{1}}

这是我项目的结构:

enter image description here

1 个答案:

答案 0 :(得分: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));
    }

如果您希望地图打开固定Lat-Lng的标记,请传递上面代码中显示的坐标,否则通过Google Location Api请求位置并将获得的Lat-Lng值传递给addMarker()方法