私はMapView
を含むFragmentクラスを持っています。それが正常に動作しているGoogle Mapのフラグメントが現在の場所のボタンを表示していません
、唯一の問題は、私は本当のことを設定し、それが現在の場所ボタンが表示されないです。
nMap = mapView.getMap();
nMap.setMyLocationEnabled(true);
nMap.getUiSettings().setZoomControlsEnabled(true);
と私は私のマニフェスト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:私は上のコードを試してみましたので、getLastLocationはない問題が発生するが、この場合にも知って
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);
}
:
これは私がこのコードを実装する非常に奇妙です別のコードとそれが動作し、ログアウトするときcurrentlocation.getLatitude()
私はこのエラーに直面する:
System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference
あなたが行った変更を詳しく説明できますか? – Sufian
@Sufian私はこのように使っていました: 'mapView =(MapView)rootView.findViewById(R.id.map); mapView.onCreate(savedInstanceState); if(mapView!= null){ nMap = mapView.getMap(); nMap.setMyLocationEnabled(true); nMap.getUiSettings()。setZoomControlsEnabled(true); mClusterManager =新しいClusterManager <>(this.getActivity()、nMap); nMap.setOnCameraChangeListener(mClusterManager); nMap.setOnMarkerClickListener(mClusterManager); } ' – Blacksword
' OnMapReadyCallback'がAPI 24にしかない場合、API 23では決して呼び出されません。テストしたデバイス/エミュレータのAndroidのバージョンは何ですか? – Sufian