1

私は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

+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

関連する問題