2014-12-14 4 views
1

Я хочу отобразить текущее местоположение. Я мог отображать карту Google в своем приложении, но текущее местоположение не отображается. Я добавил следующие разрешения для Manifest.xml.onLocationChanged() не называется

<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); 
    } 
} 

ответ

0

попробовать это ..

Вы добавили в манифесте?

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

Спасибо! Я добавил разрешение, но текущее местоположение не отображалось. – tarofess

+0

сделал u добавить ? –

+0

Да, я добавил. но, он не работает хорошо ... – tarofess

Смежные вопросы