Can';无法在Android中获得新的GPS位置

Can';无法在Android中获得新的GPS位置,android,android-maps-v2,Android,Android Maps V2,我正在开发一个应用程序,使用谷歌地图V2和GPS。代码显示了一个部分,从中获取用户位置的纬度和经度 map.setMyLocationEnabled(true); //Locate the user's current location LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE); Criteria crit = new Criteria(); String provider =

我正在开发一个应用程序,使用谷歌地图V2和GPS。代码显示了一个部分,从中获取用户位置的纬度和经度

map.setMyLocationEnabled(true); //Locate the user's current location

LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria crit = new Criteria();
String provider = service.getBestProvider(criteria, false);
Location location = service.getLastKnownLocation(provider);
LatLng locationOfUser = new LatLng(location.getLatitude(),location.getLongitude());
该应用程序基本上使用locationOfUser中的值,并可以为用户选择的标记画一条线。但是,当我从不同的位置尝试应用程序时,这会起作用。蓝色标记表示用户的当前位置发生了更改,但locationOfUser中的值没有更改,并从最后一个位置(而不是当前位置)划出一条线


多谢各位

您需要
requestLocationUpdate

service.requestLocationUpdates(provider, updateInterval, updateDistance, new LocationListener()
    {

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

        }

        @Override
        public void onProviderEnabled(String provider)
        {

        }

        @Override
        public void onProviderDisabled(String provider)
        {

        }

        @Override
        public void onLocationChanged(Location location)
        {
            LatLng locationOfUser = new LatLng(location.getLatitude(),location.getLongitude()); 
        }
    });  

其中,
updateInterval
是期望mim时间间隔,
updateInstance
是期望最小距离间隔。例如,您可以设置updateInterval=1000和UpdateInstance=1。

您需要
requestLocationUpdate

service.requestLocationUpdates(provider, updateInterval, updateDistance, new LocationListener()
    {

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

        }

        @Override
        public void onProviderEnabled(String provider)
        {

        }

        @Override
        public void onProviderDisabled(String provider)
        {

        }

        @Override
        public void onLocationChanged(Location location)
        {
            LatLng locationOfUser = new LatLng(location.getLatitude(),location.getLongitude()); 
        }
    });  

其中,
updateInterval
是期望mim时间间隔,
updateInstance
是期望最小距离间隔。例如,您可以设置updateInterval=1000和UpdateInstance=1。

既然API中添加了
OnMyLocationChangeListener
接口,就不需要从
LocationManager
手动请求位置更新


您只需启用“我的位置”层(
map.setMyLocationEnabled(true)
),设置侦听器(
map.setOnMyLocationChangeListener(this)
),并对在相应回调中传递给您的当前位置执行任何操作(通过实现该接口提供).

既然已将
OnMyLocationChangeListener
接口添加到API中,则无需从
LocationManager
手动请求位置更新


您只需启用“我的位置”层(
map.setMyLocationEnabled(true)
),设置侦听器(
map.setOnMyLocationChangeListener(this)
),并对在相应回调中传递给您的当前位置执行任何操作(通过实现该接口提供).

谢谢你的回答。我稍后会检查这是否有效。谢谢你的回答。我稍后会检查这是否有效