【问题标题】:google map not showing current location in andorid?谷歌地图没有在android中显示当前位置?
【发布时间】:2016-04-24 16:34:55
【问题描述】:

我想在谷歌地图上显示用户的当前位置,但目前我无法显示该位置。我有一个片段,我正在其中编写代码。

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_home, container, false);

        initGamp(rootView);



        // Inflate the layout for this fragment
        return rootView;
    }

initgmap 函数

  public void initGamp(View root) {

        SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        // check if map is created successfully or not


    }


 @Override
    public void onMapReady(GoogleMap googleMap) {

        mMap = googleMap;

        // Add a marker in Sydney, Australia, and move the camera.
//        LatLng sydney = new LatLng(-34, 151);
//        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
//        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
        if (ActivityCompat.checkSelfPermission(getActivity().getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity().getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        Log.i("MAP","BEFORE LOCATION");
        mMap.setMyLocationEnabled(true);
        mMap.getUiSettings().setMyLocationButtonEnabled(true);
        Log.i("MAP", "AFTER LOCATION");
        LocationManager locationManager = (LocationManager) getActivity().getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        String bestProvider = locationManager.getBestProvider(criteria, true);
        Location location = locationManager.getLastKnownLocation(bestProvider);
        if (location != null) {
            Toast.makeText(getActivity().getApplicationContext(),"not null",Toast.LENGTH_SHORT).show();
            onLocationChanged(location);
        }
        locationManager.requestLocationUpdates(bestProvider, 20, 0, this);

        Log.i("MAP", "END LOCATION");



    }

    @Override
    public void onLocationChanged(Location location) {

        Log.i("MAP","CHANGE LOCATION");
        Toast.makeText(getActivity().getApplicationContext(),"chnage",Toast.LENGTH_SHORT).show();
        mMap.clear();
        double latitude = location.getLatitude();
        double longitude = location.getLongitude();
        LatLng latLng = new LatLng(latitude, longitude);
        mMap.addMarker(new MarkerOptions().position(latLng));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
        mMap.animateCamera(CameraUpdateFactory.zoomTo(15));


    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {


    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }

所有权限:

<!-- Required to show current location -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.CALL_PHONE" />

上面的代码我用于显示位置,但我没有工作。我还有当前位置按钮,点击时我仍然没有得到当前位置。

【问题讨论】:

标签: java android google-maps gps


【解决方案1】:

这里的关键是等到onMapReady() 回调发生后再请求位置更新。一旦在MapFragmentMapView 对象上设置了此接口的实例,当映射准备好使用并提供GoogleMap 的非空实例时,就会触发onMapReady(GoogleMap) 方法。

这是满足您需要的完整活动代码。在活动加载时注册一个位置监听器,然后在每个位置更改事件发生时。

这里是示例活动代码:

public class MainActivity extends AppCompatActivity implements
OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks,     GoogleApiClient.OnConnectionFailedListener, LocationListener {

LocationRequest mLocationRequest;
GoogleApiClient mGoogleApiClient;

LatLng latLng;
GoogleMap mGoogleMap;
SupportMapFragment mFragment;
Marker currLocationMarker;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mFragment = (SupportMapFragment)       getSupportFragmentManager().findFragmentById(R.id.map);
mFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap gMap) {
mGoogleMap = gMap;
mGoogleMap.setMyLocationEnabled(true);

buildGoogleApiClient();

mGoogleApiClient.connect();

}

protected synchronized void buildGoogleApiClient() {
Toast.makeText(this,"buildGoogleApiClient",Toast.LENGTH_SHORT).show();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}

@Override
public void onConnect

【讨论】:

    猜你喜欢
    • 2018-02-11
    • 1970-01-01
    • 1970-01-01
    • 2017-08-29
    • 2014-08-21
    • 2015-08-14
    • 1970-01-01
    • 1970-01-01
    • 2013-03-27
    相关资源
    最近更新 更多