Google地图片段未显示当前位置按钮

时间:2016-09-13 12:50:15

标签: android google-maps android-fragments google-maps-android-api-2

我有一个Fragment类,其中包含MapView

它运行正常,唯一的问题是它没有显示当前位置按钮,我将其设置为true:

 nMap = mapView.getMap();
nMap.setMyLocationEnabled(true);
nMap.getUiSettings().setZoomControlsEnabled(true);

我在我的Manifest xml文件中添加权限:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>

但它没有解决问题。

mapView = (MapView) rootView.findViewById(R.id.map);
mapView.onCreate(savedInstanceState); 
if (mapView != null) { 
    nMap = mapView.getMap(); 
    nMap.setMyLocationEnabled(true); 
    nMap.getUiSettings().setZoomControlsEnabled(true); 
    mClusterManager = new ClusterManager<>(this.getActivity(), nMap); 
    nMap.setOnCameraChangeListener(mClusterManager); 
    nMap.setOnMarkerClickListener(mClusterManager); 
}

更新1:

这很奇怪我实现了这段代码:

public class MainFragment extends Fragment implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener {

//Define Variables that necessary for app
CameraUpdate update;
JSONArray array;
ClusterManager<ItemCluster> mClusterManager;
View rootView;
ItemCluster offsetItem;
MapView mapView;
GoogleMap nMap;
GoogleApiClient mLocationClient;

/*
the Constructor of Main Fragment that get the JSON array as input from AsyncTask
which download the JSON file from Meteoapps website
 */
@SuppressLint("ValidFragment")
public MainFragment(JSONArray input) {
    array = input;
}

//----Default constructor
public MainFragment() {
}


// Create the view when the fragment initialized
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container, false);
    mapView = (MapView) rootView.findViewById(R.id.map);
    mapView.onCreate(savedInstanceState);
    mapView.getMapAsync(this);
    return rootView;
}

@Override
public void onMapReady(GoogleMap googleMap) {

    if (mapView != null) {
        nMap = googleMap;
        nMap.setMyLocationEnabled(true);
        nMap.getUiSettings().setZoomControlsEnabled(true);
        mClusterManager = new ClusterManager<>(this.getActivity(), nMap);
        mClusterManager.setRenderer(new OwnIconRendered(getActivity().getApplicationContext(), nMap, mClusterManager));
        nMap.setOnCameraChangeListener(mClusterManager);
        nMap.setInfoWindowAdapter(mClusterManager.getMarkerManager());
        nMap.setOnMarkerClickListener(mClusterManager);
        mLocationClient = new GoogleApiClient.Builder(getActivity())
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
        mLocationClient.connect();
        Location currentlocation = LocationServices.FusedLocationApi
                    .getLastLocation(mLocationClient);

    }

我知道getLastLocation可能会导致问题,但在这种情况下不是因为我尝试了另一个代码上的代码而且它有效,然后当我尝试注销currentlocation.getLatitude()时,我遇到了这个错误:

  

System.err:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'double android.location.Location.getLatitude()'

1 个答案:

答案 0 :(得分:0)

docs,请改用getMapAsync(this)。另外,在OnMapReadyCallback方法上实施onMapReady(),在那里设置地图配置。除此之外,我发现您没有配置地图的问题。希望这有助于解决问题。

快乐的编码!

相关问题