1

나는 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

+0

변경 사항을 자세히 설명해 주시겠습니까? – Sufian

+0

@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

+0

'OnMapReadyCallback'이 API 24에만있는 경우 API 23에서 호출되지 않습니다. 테스트 한 장치/에뮬레이터에서 Android 버전은 무엇입니까? – Sufian

답변

0

getMap() is deprecated 대신 getMapAsync(this)을 사용하십시오. 또한 onMapReady() 메소드에서 OnMapReadyCallback을 구현하고 거기에 맵 구성을 설정하십시오. 그 외에는 맵을 구성하는 방법에 아무런 문제가 없습니다. 잘하면이 문제를 해결하는 데 도움이됩니다.

해피 코딩!

+0

안녕하세요, 나는 그것을 시도 또한 아무 일이 정말 이상한 다른 데모 애플 리케이션을 시도하고 그것은 효과가 있었지만, 다른 그가 MainActivity에서 모든 일을했다 조각에 그래서 난 내 코드가 잘못 무엇인지 알 수 없다 – Blacksword

관련 문제