2017-06-05 5 views
0

ここでは、ユーザーの現在の位置マーカーの周りのマップ回転のタスクを実行中にいくつかの奇妙なことがありました。だから私がやろうとしているのは、2番目の "現在地"タップのGoogleマップアプリケーションのようなものです。マーカーは、地図が移動している間は地図中心にその位置を保持する必要があります。ベアリングと位置

私はGoogleMap v2のカメラ位置オブジェクトを更新するためにベアリング値を使用する必要があると理解していました。

CameraUpdate cameraUpdatePos; 
      CameraPosition.Builder currentPlaceBuilder = new CameraPosition.Builder().target(loc); 
      if (location.hasBearing()) 
       currentPlaceBuilder.bearing(location.getBearing()); 
      cameraUpdatePos = CameraUpdateFactory.newCameraPosition(currentPlaceBuilder.build()); 
      map.animateCamera(cameraUpdatePos); 

私のアプリでfalse hasBearing()呼び出し結果と0.0を受け取るたびにバグが返されます。しかし、Googleマップのアプリは、この時点で私の方向性を正しく示しています。私はサービス

LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); 
     googleApiClient = new GoogleApiClient.Builder(context) 
       .addConnectionCallbacks(gmsCallbacks) 
       .addOnConnectionFailedListener(gmsCallbacks) 
       .addApi(LocationServices.API) 
       .build(); 

     locationRequest = LocationRequest.create(); 
     locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) 
       .setInterval(25 * 1000) 
       .setFastestInterval(5 * 1000) 
       .setSmallestDisplacement(1); 
     LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder() 
       .addLocationRequest(locationRequest) 
       .setAlwaysShow(true);` 

および方法を使用しています

`@Override 
     public void onConnected(@Nullable Bundle bundle) { 
      if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED 
        && ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) 
       return; 
      Log.i(TAG, "Connected"); 
      location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient); 
      LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, gmsCallbacks); 
      if (locationChangeCallback != null) 
       locationChangeCallback.onLocationChanged(location); 
     }` 

をonconnected誰もがグーグルマップが自動方位マップモードのために彼らのベアリングの計算をした方法を知っていますか? 誰かがこのような状況を抱えており、助言を受けて助けてくれるかもしれません。ありがとう。

答えて

1

これはあなたの答えのためのあなた

public class GoogleApiActivity extends AppCompatActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener , { 
SupportMapFragment fragment; 
GoogleMap googleMap; 
GoogleApiClient googleApiClient; 
Marker marker; 


@Override 
protected void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_google_api); 
    mapLoading(); 
    googleClientApi(); 


} 

private void mapLoading(){ 
    FragmentManager manager = getSupportFragmentManager(); 
    fragment = (SupportMapFragment) manager.findFragmentById(R.id.map_space); 
    if (fragment == null) 
    { 
     fragment = SupportMapFragment.newInstance(); 
     manager.beginTransaction().replace(R.id.map_space, fragment).commit(); 
    } 
} 

@Override 
protected void onResume(){ 
    super.onResume(); 
    if (googleMap == null){ 
     fragment.getMapAsync(this); 
    } 
} 

@Override 
public void onMapReady(GoogleMap googleMap){ 
    this.googleMap = googleMap; 
    Toast.makeText(this, "Map Ready CallBack", Toast.LENGTH_SHORT).show(); 

    this.googleMap.setTrafficEnabled(true); 
    this.googleMap.setIndoorEnabled(true); 
    this.googleMap.setBuildingsEnabled(true); 
    this.googleMap.getUiSettings().setZoomControlsEnabled(true); 
    this.googleMap.setOnCameraChangeListener(this); 
} 


private void googleClientApi(){ 
    googleApiClient = new GoogleApiClient.Builder(this) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .addApi(LocationServices.API) 
      .build(); 
} 

@Override 
public void onConnected(@Nullable Bundle bundle){ 
    LocationRequest locationRequest = createLocationRequest(); 
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) 
    { 
     // TODO: Consider calling 
     // ActivityCompat#requestPermissions 
     // here to request the missing permissions, and then overriding 
     // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //           int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for ActivityCompat#requestPermissions for more details. 
     return; 
    } 
    LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, createLocationRequest(),GoogleApiActivity.this); 
} 

@Override 
public void onConnectionSuspended(int i){ 

} 

@Override 
public void onConnectionFailed(@NonNull ConnectionResult connectionResult){ 

} 


public LocationRequest createLocationRequest() 
{ 
    LocationRequest locationRequest = new LocationRequest(); 
    locationRequest.setInterval(0); 
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
    locationRequest.setSmallestDisplacement(0); 
    return locationRequest; 
} 

@Override 
protected void onStart() 
{ 
    super.onStart(); 
    if (googleApiClient != null) 
    { 
     googleApiClient.connect(); 
    } 
} 

@Override 
protected void onStop() 
{ 
    super.onStop(); 
    if (googleApiClient != null) 
    { 
     googleApiClient.disconnect(); 
    } 
} 

@Override 
public void onLocationChanged(Location location){ 
    Toast.makeText(this, "Inside onLocationChanged "+location.getAccuracy(), Toast.LENGTH_SHORT).show(); 
    if (location != null) 
    { 
     if (marker == null){ 
      marker = googleMap.addMarker(new MarkerOptions() 
        .position(new LatLng(location.getLatitude(), location.getLongitude())) 
        .title("My Location")); 
      googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 18)); 
     }else{ 
      marker.setPosition(new LatLng(location.getLatitude(),location.getLongitude())); 
      updateCameraBearing(googleMap, location.getBearing(),location); 
     } 
    } } 

private void updateCameraBearing(GoogleMap googleMap, float bearing, Location location) { 
    if (googleMap == null) return; 
    CameraPosition camPos = CameraPosition 
      .builder(
        googleMap.getCameraPosition() // current Camera 
      ).target(new LatLng(location.getLatitude(),location.getLongitude())) 
      .bearing(bearing) 
      .build(); 
    googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(camPos)); 
} 

}

+0

本当にありがとうございましたを助けかもしれないが、私はすでにこれを試してみました。問題は、ベアリングの価値がかなりゼロであることです。そして私は何らかの理由でコンパスセンサーを使ってGoogleマップを実現しました。私は、Googleのアプリケーションは加速度センサーやmegneticのようなこれらのセンサーを使用していると思います。しかし、私がそのようなセンサーを使用しているとき、私は同じ行動を取らない。私はすべてantsyマップと高負荷のCPUです。 – Magirus

関連する問題