当前位置显示在模拟器上但不在手机上显示

时间:2015-12-31 19:08:55

标签: java android google-maps

我正在设置地图以跟踪我的位置。一开始我想让它检测我的Lat和Long并显示我的位置,并在我移动时更新标记位置。

当我在GenyMotion模拟器上测试并输入我自己的位置时,它会工作并相应地显示更新的位置。但是当我在移动设备上测试它时,我没有得到任何标记并获得“位置无效”Toast消息。

由于这是第一次在设备上,可能没有最后的已知位置。因此,为了确保,我拿走了设备并移动了一段距离(大约50米左右,然后向后移动),并且设备上仍然没有更新。

public class TrackMapsActivity extends FragmentActivity implements OnMapReadyCallback, LocationListener {

    private GoogleMap mMap;
    LocationManager locationManager;
    Location location;
    String provider;
    Double lat, lng;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_track_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);

        locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        provider = locationManager.getBestProvider(new Criteria(), false);

        try {
            //Get last known user location
            location = locationManager.getLastKnownLocation(provider);

            if(location != null){
                Toast.makeText(TrackMapsActivity.this, "Location NOT null", Toast.LENGTH_SHORT).show();
                onLocationChanged(location);
            }
            else {
                Toast.makeText(TrackMapsActivity.this, "Location null", Toast.LENGTH_SHORT).show();
            }
        }
        catch (SecurityException e){
            e.printStackTrace();
        }
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
    }
    @Override
    protected void onResume() {
        super.onResume();
        try {
            locationManager.requestLocationUpdates(provider, 400, 1, this);
        }
        catch (SecurityException e){
            e.printStackTrace();
        }
    }
    @Override
    protected void onPause() {
        super.onPause();
        try {
            locationManager.removeUpdates(this);
        }
        catch (SecurityException e){
            e.printStackTrace();
        }
    }
    @Override
    public void onProviderEnabled(String provider) {

    }
    @Override
    public void onLocationChanged(Location location) {
        Toast.makeText(TrackMapsActivity.this, "You Moved", Toast.LENGTH_SHORT).show();
        lat = location.getLatitude();
        lng  = location.getLongitude();
        LatLng yourLocation = new LatLng(lat, lng);

        if(mMap != null) {
            mMap.clear();
            mMap.addMarker(new MarkerOptions().position(yourLocation).title("Your Location"));
            mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(yourLocation, 10));
        }

    }
    @Override
    public void onProviderDisabled(String provider) {

    }
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }
}

1 个答案:

答案 0 :(得分:0)

你是否允许使用gps :) 您需要的主要权限是

  

android.permission.ACCESS_COARSE_LOCATION或
  android.permission.ACCESS_FINE_LOCATION。