如何在Android中使用谷歌地图API获取当前位置?

时间:2016-06-07 14:41:33

标签: android google-maps

我是谷歌地图API的新手,所以我想知道如何让谷歌地图在首次加载时检测我当前的位置。目前它只能显示硬编码位置的标记,但我希望它是我当前的位置。

这是我当前的硬编码位置代码:

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap
    LatLng test = new LatLng(33.8675,151.2070);
    mMap.addMarker(new MarkerOptions().position(test).title("test"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(test));
}

3 个答案:

答案 0 :(得分:2)

您可以按照tutorial进行操作。您需要使用LocationManager来获取最佳提供商并使用它来获取位置。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //show error dialog if GoolglePlayServices not available
        if (!isGooglePlayServicesAvailable()) {
            finish();
        }
        setContentView(R.layout.activity_my);
        SupportMapFragment supportMapFragment =
                (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.googleMap);
        googleMap = supportMapFragment.getMap();
        googleMap.setMyLocationEnabled(true);
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        String bestProvider = locationManager.getBestProvider(criteria, true);
        Location location = locationManager.getLastKnownLocation(bestProvider);
        if (location != null) {
            onLocationChanged(location);
        }
        locationManager.requestLocationUpdates(bestProvider, 20000, 0, this);
    }

这些服务允许应用程序获取设备地理位置的定期更新,或者在设备进入特定地理位置附近时触发应用程序指定的Intent

检查以下相关链接:

答案 1 :(得分:0)

在onActivityResult上添加此内容

LatLng latLng = new LatLng(place.getLatLng().latitude,place.getLatLng().longitude);
googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));

并将其添加到onMapReady

googleMap.setOnMyLocationChangeListener(myLocationChangeListener);

并添加1个方法

private OnMyLocationChangeListener myLocationChangeListener = new OnMyLocationChangeListener() {
        @Override
        public void onMyLocationChange(Location location) {
            if(awal) {
                awal=false;
                googleMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(location.getLatitude(), location.getLongitude())));
                googleMap.animateCamera(CameraUpdateFactory.zoomTo(13));
            }
        }
    };

答案 2 :(得分:-1)

public void onMapReady(GoogleMap googleMap) {
    gMap = googleMap;


    if (!gMap.isMyLocationEnabled())
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, 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.
            ActivityCompat.requestPermissions(this,new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 1);
            ActivityCompat.requestPermissions(this,new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
            ActivityCompat.requestPermissions(this,new String[]{android.Manifest.permission.INTERNET}, 1);


            Toast.makeText(this, "Grant", Toast.LENGTH_LONG).show();

            return;


    }else {
            Criteria criteria = new Criteria();
            LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            String provider = locationManager.getBestProvider(criteria, false);
            Location location = locationManager.getLastKnownLocation(provider);
            float la = (float) location.getLatitude() ;
            float lo = (float) location.getLongitude();
            Toast.makeText(this, la+"______________"+lo, Toast.LENGTH_LONG).show();


            LatLng TutorialsPoint = new LatLng(la, lo);
            gMap.addMarker(new
                    MarkerOptions().position(TutorialsPoint).title("Tutorialspoint.com"));
            gMap.moveCamera(CameraUpdateFactory.newLatLng(TutorialsPoint));
        }

}