onLocationChanged()没有被调用

时间:2014-12-14 06:54:52

标签: android google-maps

我想显示当前位置。 我可以在我的应用上显示Google地图,但当前位置不会显示。 我向Manifest.xml添加了以下权限。

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>

实际上当我确认logcat时,onLocationChanged()没有被调用。 我不知道原因。 我应该在哪里修理?请帮帮我。

~MyActivity~

public class MyActivity extends Activity implements OnClickListener, GooglePlayServicesClient.OnConnectionFailedListener,
        com.google.android.gms.location.LocationListener, GooglePlayServicesClient.ConnectionCallbacks{
    private TextView mSensorTextView;
    private Button mStartButton;
    private Button mStopButton;
    private MyService mService;
    private MyReceiver mMyReceiver = new MyReceiver();
    private LocationClient mLocationClient = null;
    private GoogleMap mMap = null;
    private static final LocationRequest REQUEST = LocationRequest.create().setInterval(5000)
            .setFastestInterval(16).setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        mSensorTextView = (TextView)findViewById(R.id.count_num);
        mStartButton = (Button)findViewById(R.id.start_button);
        mStopButton = (Button)findViewById(R.id.stop_button);
        mStartButton.setOnClickListener(this);
        mStopButton.setOnClickListener(this);

        mMap = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap(); {
            if(mMap != null) {
                mMap.setMyLocationEnabled(true);
            }
        }

        mLocationClient = new LocationClient(getApplicationContext(), this, this); {
            if (mLocationClient != null) {
                mLocationClient.connect();
            }
        }
    }

    @Override
    protected void onResume(){
        super.onResume();
        Intent intent = new Intent(MyActivity.this, MyService.class);
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
        registerReceiver(mMyReceiver, new IntentFilter(MyService.ACTION));
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        unbindService(mConnection);
    }

    private class MyReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction() == MyService.ACTION);
            mSensorTextView.setText("歩数: " + Integer.toString(mService.getmDispCount()));
        }
    }

    private ServiceConnection mConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            Log.v("called","");
            mService = ((MyService.MyBinder)iBinder).getService();
            mSensorTextView.setText("歩数: " + Integer.toString(mService.getmDispCount()));
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            mService = null;
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.start_button:
                mService.startCount();
                Date startDate = new Date();
                SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日kk時mm分ss秒");
                Log.v("date","" + sdf1.format(startDate));
                break;
            case R.id.stop_button:
                mService.stopCount();
                mSensorTextView.setText("歩数: 0");
                Date endDate = new Date();
                SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日kk時mm分ss秒");
                Log.v("date","" + sdf2.format(endDate));
                break;
        }
    }

    @Override
    public void onLocationChanged(Location location) {
        CameraPosition cameraPos = new CameraPosition.Builder()
        .target(new LatLng(location.getLatitude(), location.getLongitude())).zoom(7.0f)
        .bearing(0).build();
        mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPos));
    }

    @Override
    public void onDisconnected() {
    }

    @Override
    public void onConnectionFailed(ConnectionResult result) {
    }

    @Override
    public void onConnected(Bundle connectionHint) {
        mLocationClient.requestLocationUpdates(REQUEST, this);
    }
}

1 个答案:

答案 0 :(得分:0)

试试这个..

您是否在清单上添加了

<permission
    android:name="pakagename.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />