【问题标题】:How to add the current location to my map activity?如何将当前位置添加到我的地图活动中?
【发布时间】:2019-07-24 08:40:49
【问题描述】:

我有一个带有 mapView 的活动。目前我可以通过在我的代码中添加相应的 long 和 lat 来添加修复位置。

我很想用我当前的位置让应用程序替换那些经纬度。

我一直在尝试许多不同的方法,但我无法弄清楚如何做到这一点。

我的代码如下:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

GoogleMap mMap

@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;

    // Add a marker in Copenhagen and move the camera
    LatLng Copenhagen = new LatLng(55.67594 , 12.56553);
    mMap.addMarker(new MarkerOptions().position(Copenhagen).title("Marker in CBS"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(Copenhagen));

    //zoom to position with level 15
    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(Copenhagen, 15);
    googleMap.animateCamera(cameraUpdate);
  }
}

【问题讨论】:

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


    【解决方案1】:

    首先使用此代码获取您的当前位置:

    public void getCurrentLocation() {
            if (isPermisionAccess()) {
                locationManager = (LocationManager) mActivity.getSystemService(LOCATION_SERVICE);
                if (locationManager != null) {
                    Location gpsLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                    Location netLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (gpsLocation != null) {
                        currentlatitude = gpsLocation.getLatitude();
                        currentlongitude = gpsLocation.getLongitude();
                    } else if (netLocation != null) {
                        currentlatitude = netLocation.getLatitude();
                        currentlongitude = netLocation.getLongitude();
    
                    }
                }
            }
    
        }
    
    private boolean isPermisionAccess() {
            return (ContextCompat.checkSelfPermission(mActivity, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED);
        }
    

    然后:

    // Add a marker in myLocation and move the camera
    LatLng myLocation = new LatLng(currentlatitude  , currentlongitude );
    mMap.addMarker(myLocation).title("Marker in CBS"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(myLocation ));
    
    //zoom to position with level 15
    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(myLocation , 15);
    googleMap.animateCamera(cameraUpdate);
    

    别忘了:

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-21
      • 2023-03-05
      • 1970-01-01
      • 2021-11-19
      • 2021-10-30
      • 2019-06-24
      • 1970-01-01
      相关资源
      最近更新 更多