나는 MapView
을 포함하는 조각 클래스를 가지고 있습니다. 그것은 잘 작동Google지도 조각이 현재 위치를 표시하지 않습니다.
, 유일한 문제는 내가 참으로 설정, 그것은 현재 위치 버튼이 표시되지 않습니다이다 :
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