谷歌地图无法获取当前位置:googleMap.getMyLocation();

15

我想制作一个程序来计算一些地方到我的当前位置的距离,但是我的 googleMap.getMyLocation();无法正常工作。

googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
googleMap.setMyLocationEnabled(true);   
googleMap.getUiSettings().setCompassEnabled(false);
mylocation = googleMap.getMyLocation();
direction = new GMapV2Direction();

LatLng from, to;
from = new LatLng(-7.26071409, 112.80674726);
for(int i=0; i<lat.length; i++){    
    to = new LatLng(lat[i], lon[i]);
    doc = direksi.getDocument(from, to, GMapV2Direction.MODE_DRIVING);
    distance[i] = (double)direction.getDistanceValue(doc) / 1000;
}

我将一些地点的纬度和经度保存在lat[]和lon[]中。LatLng 'from' 是我的位置,而'to'则是我目的地的位置。 当我做出改变时,问题出现了。

from = new LatLng(-7.26071409, 112.80674726);
to
from = new LatLng(mylocation.getLatitude(), mylocation.getLongitude());

我希望在不打开Google Maps的情况下进行计算。当我点击地图按钮时,Google Maps将作为弹出窗口出现。因此,计算将在不打开Google Maps的情况下进行。

请帮帮我。


getMyLocation(),此方法已被弃用。请改用LocationClient。 - Alex DG
你正在使用 Google 地图 v2 吗? - Android_coder
@Alex,我不知道如何使用LocationClient。你能详细说明一下吗? - user3506223
@Android 是的,我正在使用 Google Map V2 API。 - user3506223
3个回答

30

试一下这个...希望这能对你有所帮助。

LocationManager locationManager = (LocationManager)
        getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();

Location location = locationManager.getLastKnownLocation(locationManager
        .getBestProvider(criteria, false));
double latitude = location.getLatitude();
double longitude = location.getLongitude();

不是这样的,因为你甚至没有检查getBestProvider和getLastKnownLocation是否为空! - larsaars

12

这是我为你编写的代码,希望它能对你有所帮助...如果你认为它的语法不正确,请编辑它...

这段代码适用于安卓4.x、5.x和6.x,我没有在安卓7上测试过。

//I use this like global var to edit or get informacion about google map
GoogleMap gMap;

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

if (ActivityCompat.checkSelfPermission(Activity_Map.this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(Activity_Map.this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                        ActivityCompat.requestPermissions(Activity_Map.this,new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 1);
                    }else{
                        if(!gMap.isMyLocationEnabled())
                            gMap.setMyLocationEnabled(true);

                        LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
                        Location myLocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

                        if (myLocation == null) {
                            Criteria criteria = new Criteria();
                            criteria.setAccuracy(Criteria.ACCURACY_COARSE);
                            String provider = lm.getBestProvider(criteria, true);
                            myLocation = lm.getLastKnownLocation(provider);
                        }

                        if(myLocation!=null){
                            LatLng userLocation = new LatLng(myLocation.getLatitude(), myLocation.getLongitude());
                            gMap.animateCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 14), 1500, null);
                        }
                    }

}

6

getMyLocation() => 此方法已弃用。您需要使用LocationClient替代。

您可以在此处找到有关LocationClient以及如何获取当前位置的信息。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接