1

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

@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

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

4

1 回答 1

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");
于 2013-07-12T16:02:17.090 回答