2017-12-22 20 views
1

이 위치로 현재 위치에 액세스 할 수 없습니다. 나는 문제가 무엇인지 모르지만. 탐색 바의 맵 조각으로 갈 때마다 현재 위치가 표시되지 않습니다. 여기 내비게이션 상자의지도 조각은 현재 위치에 액세스 할 수 없습니다.

내 코드 ..

LanlordMapFragment.java이

public class LanlordMapFragment extends Fragment implements OnMapReadyCallback { 

private GoogleMap mMap; 


public LanlordMapFragment() { 
    // Required empty public constructor 
} 

@Override 
public void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View v = inflater.inflate(R.layout.fragment_lanlord_map, container, false); 
    return v; 
} 

@Override 
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 
    getActivity().setTitle("MAP"); 

    SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map1); 
    mapFragment.getMapAsync(this); 
} 

@Override 
public void onMapReady(GoogleMap googleMap) { 

    mMap = googleMap; 

    mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("My Location")); 
    if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     mMap.setMyLocationEnabled(true); 
     return; 
    } 

    } 
} 

답변

0

나는 또한 문제의 이러한 유형을 경험했던 것이다 :

에 코드의 조각을 추가하려고하세요 귀하의 일반 뷰 생성 (..) {}

MapView m = (MapView) v.findViewById(R.id.source); 

m.onCreate(savedInstanceState); 
+0

이 코드

mMap.setMyLocationEnabled(true); mMap.getUiSettings().setMyLocationButtonEnabled(true); mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() { @Override public void onMapClick(LatLng latLng) { } }); GetCurrentLocation location = new GetCurrentLocation(getActivity(), new GeocodingNotifier<Location>() { @Override public void GeocodingDetails(Location geocodeLatLng) { if (geocodeLatLng != null) { mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(geocodeLatLng.getLatitude(), geocodeLatLng.getLongitude()), 14f)); } else { // shaow erroe message } } }); 

/// 클래스를 시도하지만 응용 프로그램은 내가이를 넣어 곧 것 –

0

//은 GetCurrentLocation

난 이미 코드를 삽입
import android.Manifest; 
import android.app.Activity; 
import android.content.pm.PackageManager; 
import android.location.Location; 
import android.os.Bundle; 
import android.support.v4.app.ActivityCompat; 
import android.util.Log; 
import android.widget.Toast; 

import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.GooglePlayServicesUtil; 
import com.google.android.gms.common.api.GoogleApiClient; 
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks; 
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener; 
import com.google.android.gms.location.LocationListener; 
import com.google.android.gms.location.LocationRequest; 
import com.google.android.gms.location.LocationServices; 

public class GetCurrentLocation implements ConnectionCallbacks, 
     OnConnectionFailedListener, LocationListener { 

    private static final String TAG = GetCurrentLocation.class.getSimpleName(); 
    private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 1000; 
    public static Location mLastLocation=null; 
    public GoogleApiClient mGoogleApiClient; 
    private boolean mRequestingLocationUpdates = false; 
    private LocationRequest mLocationRequest; 
    private GeocodingNotifier mNotifier; 
    private static int UPDATE_INTERVAL = 10000; // 10 sec 
    private static int FATEST_INTERVAL = 5000; // 5 sec 
    private static int DISPLACEMENT = 10; // 10 meters 


    Activity mContext; 

    public GetCurrentLocation(Activity mContext, GeocodingNotifier mNotifier) { 
     this.mContext = mContext; 
     this.mNotifier = mNotifier; 

     if (checkPlayServices()) { 
      // Building the GoogleApi client 
      buildGoogleApiClient(); 
      createLocationRequest(); 
     } 
    } 

    public void setOnResultsListener(GeocodingNotifier mNotifier) { 
     this.mNotifier = mNotifier; 
    } 

    /** 
    * Method to display the location on UI 
    */ 
    private void displayLocation() { 

     if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      // 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; 
     } 
     mLastLocation = LocationServices.FusedLocationApi 
       .getLastLocation(mGoogleApiClient); 

     if (mLastLocation != null) { 
      double latitude = mLastLocation.getLatitude(); 
      double longitude = mLastLocation.getLongitude(); 


     } 
    } 

    /** 
    * Method to toggle periodic location updates 
    */ 
    private void togglePeriodicLocationUpdates() { 
     if (!mRequestingLocationUpdates) { 

      mRequestingLocationUpdates = true; 

      // Starting the location updates 
      startLocationUpdates(); 

      Log.d(TAG, "Periodic location updates started!"); 

     } else { 

      mRequestingLocationUpdates = false; 

      // Stopping the location updates 
      stopLocationUpdates(); 

      Log.d(TAG, "Periodic location updates stopped!"); 
     } 
    } 

    /** 
    * Creating google api client object 
    */ 
    protected synchronized void buildGoogleApiClient() { 
     mGoogleApiClient = new GoogleApiClient.Builder(mContext) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .addApi(LocationServices.API).build(); 
    } 

    /** 
    * Creating location request object 
    */ 
    protected void createLocationRequest() { 
     mLocationRequest = new LocationRequest(); 
     mLocationRequest.setInterval(UPDATE_INTERVAL); 
     mLocationRequest.setFastestInterval(FATEST_INTERVAL); 
     mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
     mLocationRequest.setSmallestDisplacement(DISPLACEMENT); 

     displayLocation(); 
    } 

    /** 
    * Method to verify google play services on the device 
    */ 
    public boolean checkPlayServices() { 
     int resultCode = GooglePlayServicesUtil 
       .isGooglePlayServicesAvailable(mContext); 
     if (resultCode != ConnectionResult.SUCCESS) { 
      if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) { 
       GooglePlayServicesUtil.getErrorDialog(resultCode, mContext, 
         PLAY_SERVICES_RESOLUTION_REQUEST).show(); 
      } else { 
       Toast.makeText(mContext, 
         "This device is not supported.", Toast.LENGTH_LONG) 
         .show(); 

      } 
      return false; 
     } 
     return true; 
    } 

    /** 
    * Starting the location updates 
    */ 
    public void startLocationUpdates() { 
     if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      // 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(
       mGoogleApiClient, mLocationRequest, this); 
    } 

    /** 
    * Stopping location updates 
    */ 
    public void stopLocationUpdates() { 
     LocationServices.FusedLocationApi.removeLocationUpdates(
       mGoogleApiClient, this); 
    } 

    /** 
    * Google api callback methods 
    */ 
    @Override 
    public void onConnectionFailed(ConnectionResult result) { 
     Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = " 
       + result.getErrorCode()); 
    } 

    @Override 
    public void onConnected(Bundle arg0) { 
     // Once connected with google api, get the location 
     displayLocation(); 
     startLocationUpdates(); 

    } 

    @Override 
    public void onConnectionSuspended(int arg0) { 
     mGoogleApiClient.connect(); 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
     // Assign the new location 
     mLastLocation = location; 
     mNotifier.GeocodingDetails(mLastLocation); 
     displayLocation(); 
     stopLocationUpdates(); 
     mGoogleApiClient.disconnect(); 
    } 

} 
+0

충돌 계속? onMapready 방법하지 말자에서 –

+0

내가 아직 여기 GetCurrentLocation 위치 = 새로운 GetCurrentLocation (getActivity(), 새로운 GeocodingNotifier () 나는 또한 그 클래스를 사용하여 질문에 대해 답 GetCurrentLocation 클래스를 추가 한 –

+0

를 반환해야합니다. –