【问题标题】:Google Map fragment not showing current location button谷歌地图片段不显示当前位置按钮
【发布时间】:2016-09-13 12:50:15
【问题描述】:

我有一个包含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()”

【问题讨论】:

  • 您能详细说明您所做的更改吗?
  • @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 = new ClusterManager&lt;&gt;(this.getActivity(), nMap); nMap.setOnCameraChangeListener(mClusterManager); nMap.setOnMarkerClickListener(mClusterManager); }
  • 如果OnMapReadyCallback 只存在于 API 24 上,那么它永远不会在 API 23 上被调用。您测试的设备/模拟器上的 Android 版本是什么?
  • @Sufian 我更新了问题。

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


【解决方案1】:

getMap() is deprecated,请改用getMapAsync(this)。此外,在onMapReady() 方法上实现OnMapReadyCallback,在那里设置您的地图配置。除此之外,我认为您配置地图的方式没有任何问题。希望这将有助于解决问题。

编码愉快!

【讨论】:

  • 嗨,我试过了,也没有任何反应,真的很奇怪知道我的代码有什么问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-14
  • 2020-02-29
  • 1970-01-01
  • 2017-08-29
相关资源
最近更新 更多