2016-11-07 3 views
-2

현재 응용 프로그램이 Lollipop 버전에서 제대로 작동 할 때까지 현재 위치를 검색 할 수있는 응용 프로그램에서 작업 중이지만 marshmallow에서 테스트 할 때 사용자 위치를 검색 할 수 없습니다.안드로이드 Google Map이 Marshmallow에서 작동하지 않습니다.

MainActivity.class

public class MapsActivity extends FragmentActivity implements 
     OnMapReadyCallback, 
     GoogleApiClient.ConnectionCallbacks, 
     GoogleApiClient.OnConnectionFailedListener, 
     GoogleMap.OnMarkerDragListener, 
     GoogleMap.OnMapLongClickListener, 
     View.OnClickListener{ 

    //Our Map 
    private GoogleMap mMap; 

    //To store longitude and latitude from map 
    private double longitude; 
    private double latitude; 

    //Buttons 
    private ImageButton buttonSave; 
    private ImageButton buttonCurrent; 
    private ImageButton buttonView; 

    //Google ApiClient 
    private GoogleApiClient googleApiClient; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_maps); 
     // Obtain the SupportMapFragment and get notified when the map is ready to be used. 
     SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
       .findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this); 

     //Initializing googleapi client 
     googleApiClient = new GoogleApiClient.Builder(this) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .addApi(LocationServices.API) 
       .build(); 

     //Initializing views and adding onclick listeners 
     buttonSave = (ImageButton) findViewById(R.id.buttonSave); 
     buttonCurrent = (ImageButton) findViewById(R.id.buttonCurrent); 
     buttonView = (ImageButton) findViewById(R.id.buttonView); 
     buttonSave.setOnClickListener(this); 
     buttonCurrent.setOnClickListener(this); 
     buttonView.setOnClickListener(this); 
    } 

    @Override 
    protected void onStart() { 
     googleApiClient.connect(); 
     super.onStart(); 
    } 

    @Override 
    protected void onStop() { 
     googleApiClient.disconnect(); 
     super.onStop(); 
    } 

    //Getting current location 
    private void getCurrentLocation() { 
     mMap.clear(); 
     //Creating a location object 
     Location location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient); 
     if (location != null) { 
      //Getting longitude and latitude 
      longitude = location.getLongitude(); 
      latitude = location.getLatitude(); 

      //moving the map to location 
      moveMap(); 
     } 
    } 

    //Function to move the map 
    private void moveMap() { 
     //String to display current latitude and longitude 
     String msg = latitude + ", "+longitude; 

     //Creating a LatLng Object to store Coordinates 
     LatLng latLng = new LatLng(latitude, longitude); 

     //Adding marker to map 
     mMap.addMarker(new MarkerOptions() 
       .position(latLng) //setting position 
       .draggable(true) //Making the marker draggable 
       .title("Current Location")); //Adding a title 

     //Moving the camera 
     mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 

     //Animating the camera 
     mMap.animateCamera(CameraUpdateFactory.zoomTo(15)); 

     //Displaying current coordinates in toast 
     Toast.makeText(this, msg, Toast.LENGTH_LONG).show(); 
    } 

    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     mMap = googleMap; 
     LatLng latLng = new LatLng(-34, 151); 
     mMap.addMarker(new MarkerOptions().position(latLng).draggable(true)); 
     mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
     mMap.setOnMarkerDragListener(this); 
     mMap.setOnMapLongClickListener(this); 
    } 

    @Override 
    public void onConnected(Bundle bundle) { 
     getCurrentLocation(); 
    } 

    @Override 
    public void onConnectionSuspended(int i) { 

    } 

    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) { 

    } 

    @Override 
    public void onMapLongClick(LatLng latLng) { 
     //Clearing all the markers 
     mMap.clear(); 

     //Adding a new marker to the current pressed position 
     mMap.addMarker(new MarkerOptions() 
       .position(latLng) 
       .draggable(true)); 
    } 

    @Override 
    public void onMarkerDragStart(Marker marker) { 

    } 

    @Override 
    public void onMarkerDrag(Marker marker) { 

    } 

    @Override 
    public void onMarkerDragEnd(Marker marker) { 
     //Getting the coordinates 
     latitude = marker.getPosition().latitude; 
     longitude = marker.getPosition().longitude; 

     //Moving the map 
     moveMap(); 
    } 

    @Override 
    public void onClick(View v) { 
     if(v == buttonCurrent){ 
      getCurrentLocation(); 
      moveMap(); 
     } 
    } 
} 
+0

당신은 어떤 오류가있다? –

+0

아니요. 오류가 발생하지 않습니다. 기본 Google지도가 –

+0

입니다. https://developer.android.com/training/permissions/requesting.html 확인하십시오. 런타임 승인 –

답변

0

는 MainActivity에이 코드를 추가 HomeActivity

private void fn_permission() { 
    if ((ContextCompat.checkSelfPermission(getApplicationContext(), 
      Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)) { 
(ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, 
       Manifest.permission.READ_EXTERNAL_STORAGE)) && (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, 
       Manifest.permission.ACCESS_FINE_LOCATION))) { 

     } else { 
      ActivityCompat.requestPermissions(MainActivity.this, new String[]{ Manifest.permission.ACCESS_FINE_LOCATION 
        }, 
        REQUEST_PERMISSIONS); 

     } 
    }else { 

    } 
} 
+0

어디 허가를 추가해야합니까 –

+0

내 코드를 업데이트했는지 확인하십시오 – Puri