谷歌位置服务更新忽略位置监听器(android)

时间:2013-07-12 15:42:12

标签: android google-maps gps listener

我有一个应用程序,它注册一个locationClient进行更新。但是,似乎永远不会访问位置监听器。出现“GPS连接”吐司,所以我知道GPS连接正常,但显示坐标的吐司不会显示,并且不会记录被激活的监听器。但是,会记录“已激活位置更新”。 “connector”是一个启动Asynctask以获取数据库数据的对象。它工作正常。

@Override
public void onConnected(Bundle arg0) 
{
    Toast.makeText(context, "GPS connected", Toast.LENGTH_SHORT).show();
    connector = new Connector();
    connector.execute("clues", null, null);
    listener = new LocationListener()
    {
        @Override
        public void onLocationChanged(Location location) 
        {
            Log.d("location", "listener activated");
            try
            {
            double[] coords = locationCoords(location);
            if(coords[0] != 0)
            {
                map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(coords[0], coords[1]), (float) 14.5));

            }
            Toast.makeText(context, coords[0] + ", " + coords[1], Toast.LENGTH_LONG).show();
            }
            catch(NullPointerException e)
            {
                Log.d("nulls", "null");
            }

        }
    };
    mLocationClient.requestLocationUpdates(mLocationRequest, listener);
    Log.d("location", "location updates initiated");
}

这是相关位的日志。 “map null”正在记录一个不同的问题,即当我尝试实例化时,我的谷歌地图片段尚未初始化。

07-12 15:15:36.608: D/dalvikvm(1116): GC_FOR_ALLOC freed 133K, 9% free 3105K/3392K, paused 46ms, total 49ms
07-12 15:15:36.618: I/dalvikvm-heap(1116): Grow heap (frag case) to 3.757MB for 635812-byte allocation
07-12 15:15:36.688: D/dalvikvm(1116): GC_FOR_ALLOC freed <1K, 8% free 3726K/4016K, paused 65ms, total 65ms
07-12 15:15:36.818: D/dalvikvm(1116): GC_CONCURRENT freed 2K, 7% free 3742K/4016K, paused 8ms+6ms, total 130ms
07-12 15:15:37.208: D/null(1116): map null
07-12 15:15:37.228: D/location(1116): location request constructed
07-12 15:15:37.368: D/null(1116): map null
07-12 15:15:38.198: D/dalvikvm(1116): GC_CONCURRENT freed 141K, 7% free 3994K/4260K, paused 6ms+59ms, total 252ms
07-12 15:15:38.478: D/libEGL(1116): loaded /system/lib/egl/libEGL_emulation.so
07-12 15:15:38.519: D/(1116): HostConnection::get() New Host Connection established 0x2a19b220, tid 1116
07-12 15:15:38.559: D/libEGL(1116): loaded /system/lib/egl/libGLESv1_CM_emulation.so
07-12 15:15:38.608: D/libEGL(1116): loaded /system/lib/egl/libGLESv2_emulation.so
07-12 15:15:39.008: W/EGL_emulation(1116): eglSurfaceAttrib not implemented
07-12 15:15:39.068: D/OpenGLRenderer(1116): Enabling debug mode 0
07-12 15:15:39.258: D/(1116): HostConnection::get() New Host Connection established 0x2a1b0310, tid 1140
07-12 15:15:39.268: I/Choreographer(1116): Skipped 93 frames!  The application may be doing too much work on its main thread.
07-12 15:15:40.028: D/dalvikvm(1116): GC_FOR_ALLOC freed 345K, 12% free 4030K/4564K, paused 95ms, total 106ms
07-12 15:15:40.398: D/dalvikvm(1116): GC_CONCURRENT freed 328K, 11% free 4179K/4696K, paused 10ms+13ms, total 130ms
07-12 15:15:41.248: D/location(1116): location updates initiated
07-12 15:15:41.268: D/dalvikvm(1116): GC_CONCURRENT freed 611K, 17% free 4084K/4864K, paused 7ms+61ms, total 340ms

此日志来自模拟器,但在真实设备上会发生相同的行为。

1 个答案:

答案 0 :(得分:0)

通常在我调用和使用GoogleMap之前,我实例化了supportfragmentmanager,然后使用它来实例化地图。如下所示,我希望这会有所帮助。

    lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    SupportMapFragment mf= (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);


    map = mf.getMap();

    //map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
    ll = new SecondPointLocationListener();

就您打印位置的方法而言,请使用此...

@Override
public void onConnected(Bundle arg0) 
{
    Toast.makeText(context, "GPS connected", Toast.LENGTH_SHORT).show();
    connector = new Connector();
    connector.execute("clues", null, null);
    listener = new LocationListener()
    {
        @Override
        public void onLocationChanged(Location location) 
        {
            Log.d("location", "listener activated");
            try
            {
            double[] coords = locationCoords(location);
            if(coords[0] != 0)
            {
                map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(coords[0], coords[1]), (float) 14.5));

            }

            Double latitude = location.getLatitude();
            Double longitude = location.getLongitude();
            Toast.makeText(context,"Latitude = "+ latitude + "" +"Longitude  "+ longitude,
                    Toast.LENGTH_LONG).show();
            }
            catch(NullPointerException e)
            {
                Log.d("nulls", "null");
            }

        }
    };
    mLocationClient.requestLocationUpdates(mLocationRequest, listener);
    Log.d("location", "location updates initiated");