Android googleMap.getMyLocation(); 无法获取当前位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23104089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:46:29  来源:igfitidea点击:

googleMap.getMyLocation(); cannot get current location

androideclipsegoogle-maps-api-2

提问by user3506223

i want to make a program to calculate the distance between some places to my current location, but my googleMap.getMyLocation(); doesnt work properly.

我想制作一个程序来计算一些地方到我当前位置的距离,但是我的 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;
}

i saved the latitude and longitude of some places in lat[] and lon[]. LatLng 'from' is mylocation and 'to' is my destination places. the problem appear when i change

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

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

i want to do the calculation without opening the googlemaps. the google maps will appear when i touch the map button as pop up window. so the calculation will happen without opening googlemap

我想在不打开谷歌地图的情况下进行计算。当我触摸地图按钮作为弹出窗口时,谷歌地图将出现。所以计算将在不打开 googlemap 的情况下发生

please help me

请帮我

回答by Android_coder

Try this...I hope this will be help to you

试试这个...我希望这对你有帮助

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();

回答by DarckBlezzer

This is my code for you, I hope this help you... edit it if you think, it isn't with correct sintaxis...

这是我给你的代码,我希望这对你有帮助......如果你认为它没有正确的sintaxis,请编辑它......

This work for android 4.x and 5.x and 6.x, I not test this in android 7

这适用于 android 4.x 和 5.x 和 6.x,我没有在 android 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);
                        }
                    }

}

回答by AlexDG

getMyLocation()=> this method is deprecated. You need to use LocationClientinstead.

getMyLocation()=>此方法已弃用。你需要LocationClient改用。

You can find informations about LocationClientand how to get current location here

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