Android 导航抽屉&x2B;谷歌地图事件
在我的Android 导航抽屉&x2B;谷歌地图事件,android,google-maps,Android,Google Maps,在我的navigationView(R.id.create\u marker)中按一个选项时出现问题我想在我的MapFragment中创建一个标记,但我不能,有什么建议吗 MainActivity.java的代码,其中我按下菜单选项: navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean o
navigationView
(R.id.create\u marker
)中按一个选项时出现问题我想在我的MapFragment
中创建一个标记,但我不能,有什么建议吗
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;
}
});
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) {
}
}
MapFragment.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;
}
});
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) {
}
}
这是我的项目结构:
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;
}
});
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) {
}
}
要创建标记,您需要在地图上调用
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()方法。要创建标记,需要在地图上调用
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()方法