2017-06-25 22 views
0

私はこのtutorialを使用してGPS /ユーザーの場所に基づくアプリケーションを開発しています。アイデアは、ユーザーがそれが秒ごとに地図上のデバイスを見つけ、それを更新する必要があります(ボタンをクリックすることによって)は、第2のアクティビティを開いたとき、ここで、あるアクティビティコードです:ユーザーの所在地が地図上に表示されない

public class MapLocation extends AppCompatActivity implements 
    OnMapReadyCallback, 
    GoogleApiClient.ConnectionCallbacks, 
    GoogleApiClient.OnConnectionFailedListener, 
    LocationListener { 

GoogleMap mGoogleMap; 
GoogleApiClient mGoogleApiClient; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.maplocation); 
    initMap(); 
} 

private void initMap() { 
    MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.mapFragment); 
    mapFragment.getMapAsync(this); 
} 

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

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

LocationRequest mLocationRequest; 

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

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
     if (checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      // TODO: Consider calling 
      // public void requestPermissions(@NonNull String[] permissions, int requestCode) 
      // 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 Activity#requestPermissions for more details. 
      return; 
     } 
    } 
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
} 

@Override 
public void onConnectionSuspended(int i) { 

} 

@Override 
public void onConnectionFailed(ConnectionResult connectionResult) { 

} 

@Override 
public void onLocationChanged(Location location) { 
    if(location == null) { 
     Toast.makeText(this, "Can't get current location!", Toast.LENGTH_LONG).show(); 
    } else { 
     LatLng ll = new LatLng(location.getLatitude(), location.getLongitude()); 
     CameraUpdate update = CameraUpdateFactory.newLatLngZoom(ll, 15); 
     mGoogleMap.animateCamera(update); 
    } 
}} 

、ここでXMLコードです:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:background="@color/_fff"> 

    <fragment 
     android:id="@+id/mapFragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:name="com.google.android.gms.maps.MapFragment" /> 
</LinearLayout> 

、ここでは、必要なアクセス許可、次のとおりです。

<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" /> 

マップが正常に動作します、しかし、問題は、それは文句を言わないに、マップ(私の場所)に自分のデバイスを示すことがありますそれは動作します。私はデバイスとエミュレータでアプリケーションを実行していますが、結果は同じですが、地図は正常に動作していますが、私の位置は地図上に表示されません。誰かが私に間違っていることを説明することはできますか?

+0

エミュレータは、例えば上のGPS /場所があることを確認します。https://gyazo.com/577bd3f0c2fb8a9de6b8d1993c7ecc9c –

+0

それは上にある、両方のエミュレータと私のデバイスで –

答えて

0

すぐに見ることができる重大な間違いは、setLocationEnabbled(true)メソッドが欠落していることです。

このコードを試してみてください。

@Override 
public void onMapReady(GoogleMap googleMap) { 
    Toast.makeText(this, "onMapReady", Toast.LENGTH_LONG).show(); 
    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) { 
     return; 
    } else { 
     mMap = googleMap; 
     buildGoogleApiClient(); 
     mMap.setMyLocationEnabled(true); 
     googleMap.getUiSettings().setZoomControlsEnabled(true); 
    } 
} 

@Override 
public void onConnected(@Nullable Bundle bundle) { 
    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 is disabled, turn it on in your settings,", Toast.LENGTH_LONG).show(); 
    } else { 
     Toast.makeText(this, "onConnected", Toast.LENGTH_LONG).show(); 
     mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
       mGoogleApiClient); 
     if (mLastLocation != null) { 
      //place marker at current position 
      mMap.clear(); 
      double latitude = mLastLocation.getLatitude(); 
      double longitude = mLastLocation.getLongitude(); 
      LatLng latLng = new LatLng(latitude, longitude); 
      MarkerOptions markerOptions = new MarkerOptions(); 
      markerOptions.position(latLng); 
      markerOptions.title("Current Position"); 
      markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_VIOLET)); 
      mCurrLocation = mMap.addMarker(markerOptions); 
      CameraPosition cameraPosition = new CameraPosition.Builder() 
        .target(new LatLng(latitude, longitude)) 
        .zoom(11) 
        .build(); 
      mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 
     }else{ 
      Toast.makeText(this, "LastLocation is null", Toast.LENGTH_LONG).show();} 

はそれが役に立てば幸い:)

+0

遅く返事を申し訳ありません...はい、私は、恥ずかしいことを忘れてしまった。ありがとうございました :) –

関連する問題