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

use*_*223 14 eclipse android google-maps-api-2

我想制作一个程序来计算一些地方到我当前位置之间的距离,但是我的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;
}
Run Code Online (Sandbox Code Playgroud)

我保存了lat []和lon []中某些地方的纬度和经度.LatLng'from'是mylocation,'to'是我的目的地.我改变时出现问题

from = new LatLng(-7.26071409, 112.80674726);
to
from = new LatLng(mylocation.getLatitude(), mylocation.getLongitude());
Run Code Online (Sandbox Code Playgroud)

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

请帮我

And*_*der 29

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

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();
Run Code Online (Sandbox Code Playgroud)


Dar*_*zer 10

这是我的代码,我希望这可以帮助你...如果你认为编辑它,它不是正确的sintaxis ...

这项工作适用于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);
                        }
                    }

}
Run Code Online (Sandbox Code Playgroud)


Ale*_*xDG 5

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

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