我在我的项目中使用谷歌地图。它工作但我做了一些事情(不知道是什么)现在getMap()给出了错误。我想我更新了一些东西。我该如何解决?
我的代码就是这样;
GoogleMap googleMap;
..
if (locationManagerCheck.isLocationServiceAvailable()) {
if (googleMap == null) {
try {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();//ERROR
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
e.getMessage(), Toast.LENGTH_SHORT)
.show();
}...
我试过去:
googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMapAsync(this);
但是'this'给出了另一个错误 你能救我吗?
编辑:
public class MainActivity extends FragmentActivity implements OnMapReadyCallback {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocationManager_check locationManagerCheck = new LocationManager_check(
this);
if (locationManagerCheck.isLocationServiceAvailable()) {
if (googleMap == null) {
try {
MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
e.getMessage(), Toast.LENGTH_SHORT)
.show();
}
{
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
googleMap.setMyLocationEnabled(true);
googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(final Marker marker) {....}
}
@Override
public void onMapReady(GoogleMap Map) {
googleMap=Map;
}
}
答案 0 :(得分:2)
您应该查看documentation
要将地图添加到活动,请将地图片段添加到布局:
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:name="com.google.android.gms.maps.MapFragment"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
然后在您的活动中使用它:
public class MainActivity extends FragmentActivity implements OnMapReadyCallback {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap map) {
map.setMyLocationEnabled(true);
map.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(final Marker marker) {....}
}
地图将以异步方式创建,地图准备好后将调用onMapReady函数,在此功能中,您可以添加标记,更改位置,标题,缩放等。
答案 1 :(得分:0)
在您检索地图时,它是null
。现在已经初始化了视图,你需要这样做。
例如,在Fragment
中,您可以在调用onViewCreated
Activity
或onCreate
中的setContentView(R.layout.your_layout);
中执行此操作
例如:
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
MapView mapView = ((MapFragment) getFragmentManager().findFragmentById(R.id.map));
mapView.getMapAsync(this);
}