2017-03-25 6 views
0

build.gradeonLocationChanged가 잘못된 위치 (바 렌츠 해)를 가리키고 있습니까?

compile 'com.android.support:appcompat-v7:25.3.0' 
    compile 'com.android.support.constraint:constraint-layout:1.0.1' 
    compile 'com.google.android.gms:play-services:10.2.1' 

의 AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 

<meta-data android:name="com.google.android.geo.API_KEY" 
     android:value="@string/str_map_key"/> 

<meta-data android:name="com.google.android.gms.version" 
     android:value="@integer/google_play_services_version"/> 

MainActivity.java

public class MainActivity extends AppCompatActivity 
    implements OnMapReadyCallback, 
    ActivityCompat.OnRequestPermissionsResultCallback, 
    GoogleApiClient.ConnectionCallbacks, 
    GoogleApiClient.OnConnectionFailedListener, 
    LocationListener { 

private GoogleMap mGoogleMap; 
private static final int LOCATION_PERMISSION_REQUEST_CODE = 1; 
private boolean mPermissionDenied = false; 

private GoogleApiClient mGoogleApiClient; 

private Location mLastLocation; 

//TextView 
TextView longitudeTextView; 
TextView lattitudeTextView; 

private LocationRequest mLocationRequest; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .addApi(LocationServices.API) 
      .build(); 

    MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map_view); 
    mapFragment.getMapAsync(this); 

    longitudeTextView = (TextView) findViewById(R.id.main_longitude_text); 
    lattitudeTextView = (TextView) findViewById(R.id.main_lattitude_text); 
} 

@Override 
protected void onStart() { 
    //when activity is create GoogleMapApiClient is build there under onCreate 
    //than on activity start we gonna connect it to Google 
    mGoogleApiClient.connect(); 
    super.onStart(); 
} 

@Override 
protected void onStop() { 
    //when activity is stopped disconnect GoogleApiClient 
    super.onStop(); 
    mGoogleApiClient.disconnect(); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

@Override 
public void onMapReady(GoogleMap googleMap) { 
    mGoogleMap = googleMap; 

    enableMyLocation(); 
} 

private void enableMyLocation() { 
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) 
      != PackageManager.PERMISSION_GRANTED) { 
     //Permission to access the location is missing 
     ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 
       LOCATION_PERMISSION_REQUEST_CODE); 
    } else { 
     //Access to location has been granted to app 
     mGoogleMap.setMyLocationEnabled(true); 
    } 
} 

@Override 
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, 
             @NonNull int[] grantResults) { 
    if (requestCode != LOCATION_PERMISSION_REQUEST_CODE) { 
     return; 
    } 

    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) 
      != PackageManager.PERMISSION_GRANTED) { 
     // Enable the my location layer if the permission has been granted. 
     enableMyLocation(); 
    } else { 
     // Display the missing permission error dialog when the fragments resume. 
     mPermissionDenied = true; 
    } 

} 

@Override 
public void onConnected(@Nullable Bundle bundle) { 
    mLocationRequest = LocationRequest.create() 
      .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) 
      .setInterval(100); 

    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) { 
     Toast.makeText(this, "Location permission missing !!", Toast.LENGTH_LONG).show(); 
     return; 
    } 
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
} 

@Override 
public void onConnectionSuspended(int i) { 

} 

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

} 

@Override 
public void onLocationChanged(Location location) { 
    //displaying coordinates on screen for test purpose only 
    longitudeTextView.setText(String.valueOf(location.getLongitude())); 
    lattitudeTextView.setText(String.valueOf(location.getLatitude())); 

    LatLng latLng = new LatLng(location.getLongitude(), location.getLatitude()); 
    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 15); 
    mGoogleMap.moveCamera(cameraUpdate); 
} 
} 

아주 기본적인 응용 프로그램은 새로운 좌표로 카메라를 이동합니다.

onLocationChanged은 위치가 변경 될 때마다 호출됩니다. 그것은 새로운 좌표로 카메라를 이동해야하지만, 대신에 Barents 바다의 어딘가를 가리 키기 시작합니다. 나는 5 번 이상 전체 문서를 던지지 만 여전히 그것을 얻지 못했습니다. 응용 프로그램에

내가 실제로 https://s10.postimg.org/d4ol1yr3d/Screenshot_20170325-132620.png

에뮬레이터와 실제 장치 모두에서 테스트입니다 https://s10.postimg.org/xywc61w2x/Screenshot_20170325-123853.png

를 시작합니다.

답변

0

좌표 75.8467264/30.86268675.8467229/30.862678은 바 렌츠 해에 있습니다.

변경

LatLng latLng = new LatLng(location.getLongitude(), location.getLatitude()); 

onLocationChanged()에서

LatLng latLng = new LatLng(location.getLatitude, location.getLongitude()()); 

에지도에 인도 북부를 볼 수 있습니다.

+0

감사합니다. @ Markus Kauppinem. 나는 LatLng에 문제가 있다고 기대하지 않았다. 디버깅에서 인터페이스 구현에 이르기까지 모든 것을 던졌습니다. – Gill