2017-10-10 5 views
1

내 문제는 안드로이드의 Google지도와 관련이 있습니다. 어느 지점에서든 카메라를 움직이면 즉시 원래 위치로 돌아갑니다. 어떻게 멈출 수 있습니까? , 당신은 내 코드를 찾을 수 있습니다 지도가 정상적으로 시작한 다음지도를 탐색하려고하면 내 원래 위치로 자기 자신을 재배치합니다.내 위치에서 Google지도를 계속 다시 센터링하는 것을 중지하는 방법

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleMap.OnMapLongClickListener { 

    private GoogleMap mMap; 
    LocationManager locationmanager ; 
    LocationListener locationlistener; 


    @Override 
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
     super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
     if (grantResults.length> 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){ 
      if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){ 

       locationmanager.requestLocationUpdates(locationmanager.GPS_PROVIDER, 0, 0, locationlistener); 

       Location lastKnownLocation = locationmanager.getLastKnownLocation(locationmanager.GPS_PROVIDER); 

       centerMpOnLocation(lastKnownLocation, "your Location "); 


      } 
     } 
    } 

    public void centerMpOnLocation (Location location, String title){ 
      if (location != null){ 
      LatLng userLocation = new LatLng(location.getLatitude(), location.getLongitude()); 

      mMap.clear(); 
      if (title != "your Location") { 
       mMap.addMarker(new MarkerOptions().position(userLocation).title(title)); 
      } 
       mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 15)); 

      }else { 
       Toast.makeText(this, "Wait till we get your Location", Toast.LENGTH_SHORT).show(); 
      } 
    } 
    @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); 
    } 


    /** 
    * Manipulates the map once available. 
    * This callback is triggered when the map is ready to be used. 
    * This is where we can add markers or lines, add listeners or move the camera. In this case, 
    * we just add a marker near Sydney, Australia. 
    * If Google Play services is not installed on the device, the user will be prompted to install 
    * it inside the SupportMapFragment. This method will only be triggered once the user has 
    * installed Google Play services and returned to the app. 
    */ 
    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     mMap = googleMap; 

     mMap.setOnMapLongClickListener(this); 

     Intent intent= getIntent(); 

     if (intent.getIntExtra("placeNumber", 0) == 0){ 

      locationmanager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 

      locationlistener = new LocationListener() { 
       @Override 
       public void onLocationChanged(Location location) { 

        centerMpOnLocation(location, "your Location"); 



       } 

       @Override 
       public void onStatusChanged(String s, int i, Bundle bundle) { 

       } 

       @Override 
       public void onProviderEnabled(String s) { 

       } 

       @Override 
       public void onProviderDisabled(String s) { 

       } 
      }; 

      if (Build.VERSION.SDK_INT < 23){ 
       locationmanager.requestLocationUpdates(locationmanager.GPS_PROVIDER, 0, 0, locationlistener); 
      }else { 

       if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){ 

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

       }else{ 
        locationmanager.requestLocationUpdates(locationmanager.GPS_PROVIDER, 0, 0, locationlistener); 

        Location lastKnownLocation = locationmanager.getLastKnownLocation(locationmanager.GPS_PROVIDER); 

        centerMpOnLocation(lastKnownLocation, "your Location"); 
       } 

      } 
     }else { 

      Location placeLocation = new Location(locationmanager.GPS_PROVIDER); 

      placeLocation.setLatitude(MainActivity.locations.get(intent.getIntExtra("placeNumber", 0)).latitude); 
      placeLocation.setLongitude(MainActivity.locations.get(intent.getIntExtra("placeNumber", 0)).longitude); 

      centerMpOnLocation(placeLocation, MainActivity.memorablePlaces.get(intent.getIntExtra("placeNumber", 0))); 

     } 


    } 

    @Override 
    public void onMapLongClick(LatLng latLng) { 
     Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault()); 
     String address = "" ; 
     try { 
      List <Address> addresses = geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1); 

      if (addresses != null && addresses.size() > 0){ 
       if (addresses.get(0).getThoroughfare() != null){ 
        if (addresses.get(0).getSubThoroughfare() != null){ 
         address += addresses.get(0).getSubThoroughfare() + " "; 
        } 
        address += addresses.get(0).getThoroughfare(); 
       } 
      }else if(address == "") { // عشان لو ماعرفتش اطلع عنوان هاحتاج اطلع تاريخ ووقت حاجه بديله يعنى 

       SimpleDateFormat sdf = new SimpleDateFormat("HH:mm yyyy-MM-dd"); 

       address = sdf.format(new Date()); 

      } 
     } catch (IOException e) { 

      e.printStackTrace(); 
     } 

     mMap.addMarker(new MarkerOptions().position(latLng).title(address)); 

     MainActivity.memorablePlaces.add(address); 
     MainActivity.locations.add(latLng); 
     MainActivity.arrayAdapter.notifyDataSetChanged(); 

     Toast.makeText(this, "Location Saved", Toast.LENGTH_SHORT).show(); 
    } 
}` 

누구나 수정할 수 있습니다. 감사합니다.

답변

0

위치 업데이트가있을 때마다 중심에 있습니다.

locationlistener = new LocationListener() { 
       @Override 
       public void onLocationChanged(Location location) { 

        centerMpOnLocation(location, "your Location"); 



       } 
    ... 
    } 

("당신의 위치를"위치) centerMpOnLocation를 제거;을 입력하거나 로직을 변경하십시오.