2017-08-28 40 views
-1

我正在開發包含Google地圖活動的應用程序。無法在Google地圖活動中移動。它始終返回到我的當前位置

我的問題是: 當我試圖在移動地圖或&縮小其立即返回到我的當前位置。此外,當我搜索位置時,在搜索到的位置添加了新標記,但立即返回到當前位置。

我搜索的解決方案,但沒有發現任何.. 我希望有人能弄明白

這是我的谷歌地圖的活動:

public class MapActivity extends FragmentActivity implements OnMapReadyCallback { 

    private GoogleMap mMap; 
    public LocationManager locationManager; 
    public LocationListener locationListener; 
    public DatabaseReference mDatabase; 
    private FirebaseAuth mAuth; 
    private ArrayList<User> userArrayList = new ArrayList<>(); 
    User useri; 

    @Override 
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
     super.onRequestPermissionsResult(requestCode, permissions, grantResults); 

     if (requestCode == 1) { 

      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); 
        } 
       } 
      } 
     } 
    } 

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

    } 

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

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

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

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

       mMap.addMarker(new MarkerOptions().position(userLocation).title("המיקום שלי") 
         .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_action_marker1))); 
       mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 11)); 

      } 

      @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); 
      mMap.setMyLocationEnabled(true); 

     } 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); 
       //mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
       //MapStyleOptions style = MapStyleOptions.loadRawResourceStyle(this, R.raw.style); 
       //mMap.setMapStyle(style); 
       mMap.setMyLocationEnabled(true); 
       final Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER); 
       final LatLng userLocation = new LatLng(lastKnownLocation.getLatitude(), lastKnownLocation.getLongitude()); 
       mDatabase.child("Users").child(mAuth.getCurrentUser().getUid()).child("lat").setValue(lastKnownLocation.getLatitude()); 
       mDatabase.child("Users").child(mAuth.getCurrentUser().getUid()).child("lng").setValue(lastKnownLocation.getLongitude()); 
       mMap.clear(); 
       mMap.addMarker(new MarkerOptions().position(userLocation).title("המיקום שלי") 
         .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_action_marker1))); 
       mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 11)); 

       showUsersOnMap(); 

      } 
     } 
    } 


    // Search for location and show it on the map 
    public void onClick(View view) { 

     if(view.getId() == R.id.searchLocationBtn){ 
      EditText searchBoxLocation = (EditText) findViewById(R.id.searchBoxLocation); 
      String location = searchBoxLocation.getText().toString(); 
      List<Address> addressList = null; 
      MarkerOptions markerOptions = new MarkerOptions(); 
      if(! location.equals("")){ 
       Geocoder geocoder = new Geocoder(this); 
       try { 
        addressList = geocoder.getFromLocationName(location, 1); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 

       for (int i = 0 ; i < addressList.size(); i++){ 
        Address myAddress = addressList.get(i); 
        LatLng latLng = new LatLng(myAddress.getLatitude(), myAddress.getLongitude()); 
        markerOptions.position(latLng); 
        mMap.clear(); 
        mMap.addMarker(markerOptions); 
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,11)); 
       } 
      } 
     } 
    } 

    // Function to show all the users on the map 
    public void showUsersOnMap(){ 
     mDatabase.child("Users").addValueEventListener(new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 
       for (DataSnapshot ds: dataSnapshot.getChildren()){ 
        User user = ds.getValue(User.class); 
        userArrayList.add(user); 
       } 

       for (int i = 0; i < userArrayList.size(); i++) { 
        useri = userArrayList.get(i); 
        if (useri.getLat() != 0 && useri.getLng() != 0) { 
         MarkerOptions markerOptions = new MarkerOptions(); 
         LatLng userlatLng = new LatLng(useri.getLat(), useri.getLng()); 
         markerOptions.position(userlatLng); 
         mMap.addMarker(new MarkerOptions().position(userlatLng).title(useri.getName()).snippet(useri.getPhone()) 
           .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_action_marker2))); 
         //mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng2,10)); 

        } 
        else Toast.makeText(getApplicationContext(),"ישנה בעיה. אנא נסה להתחבר למפה שוב",Toast.LENGTH_LONG).show(); 
       } 

      } 

      @Override 
      public void onCancelled(DatabaseError databaseError) { 

      } 
     }); 
    } 
} 

UPDATE:

我想我發現了什麼問題。 我只是禁用onLocationChanged()內的mMap.moveCamera(),它工作得很好。

現在我想知道它是否可以這樣做?當我開車時,它仍然會將相機保留在當前位置?

回答

0

在搜索代碼你每次添加標記在循環

for (int i = 0 ; i < addressList.size(); i++){ 
        Address myAddress = addressList.get(i); 
        LatLng latLng = new LatLng(myAddress.getLatitude(), myAddress.getLongitude()); 
        markerOptions.position(latLng); 
        mMap.clear(); 
        mMap.addMarker(markerOptions); 
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,11)); 
       } 

在相同的代碼,你的相機移動到特定的位置mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,11))

mMap.addMarker()將增加一個新標記地圖。

mMap.moveCamera()將焦點移到給定位置。

檢查您的實施。

希望它有幫助:)

+0

我想我發現了這個問題。請參閱最新的問題 – Nauruto

相關問題