如何从我们当前位置加载地图

时间:2017-02-02 07:03:54

标签: android google-maps

我想从当前位置加载地图。现在我使用以下代码。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);


    }

加载地图后

@Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        mMap.setOnMarkerClickListener(MapsActivity.this);


        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        mMap.setMyLocationEnabled(true);      
        if(checkLocationService()){
            startLocationUpdates();
        }

此处地图首先显示相机位置移动到当前位置后的默认位置。但是我想从当前位置加载地图。像UBER,OLA ..请让我有任何想法来实现我的要求。

2 个答案:

答案 0 :(得分:0)

首先等待位置更改,然后调用Google地图活动/加载地图。

private void requestLocation(){
    locationManager.requestLocationUpdates(
        LocationManager.GPS_PROVIDER, 0, 0, mListener
    );
}

private LocationListener mListener = new LocationListener() {
    @Override
    public void onLocationChanged(Location location) {
        // Call maps activity here / load map
    }
};

你也可以导航到海洋中部。显示waiting for location消息,等待位置更新,然后导航回当前位置。这通常用于使用地图的应用程序。

答案 1 :(得分:0)

您可以找到从基本位置到获取的当前位置的距离。如果它位于某些位置下,您可以填充地图。

private void setUpMapIfNeeded(){

    if (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();
        mMap.setMyLocationEnabled(true);
        // Check if we were successful in obtaining the map.
        if (mMap != null) {


         mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {

       @Override
       public void onMyLocationChange(Location arg0) {
        // TODO Auto-generated method stub



        }
    }
}