1

我很抱歉發佈整個代碼以避免其他人的混淆。我在我的應用程序中成功實施了GoogleMap,並且我也獲得了當前位置。我的要求是獲取位置的地址無論標記是放置的。標記是可拖動的,但是當我移動或將標記從一個地方拖到另一個地方時,我沒有得到任何東西,甚至沒有日誌。我搜索了大約2天的解決方案。請儘量幫助我。 這是我的Activity.I提供完整的代碼,包括進口,以便您更好地理解。GoogleMap.OnMarkerDragListener無法正常工作

import android.Manifest; 
import android.content.Intent; 
import android.content.pm.PackageManager; 
import android.location.Address; 
import android.location.Geocoder; 
import android.location.Location; 
import android.os.Build; 
import android.os.Bundle; 
import android.support.design.widget.CoordinatorLayout; 
import android.support.v4.app.ActivityCompat; 
import android.support.v4.content.ContextCompat; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.Toast; 
import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.GooglePlayServicesNotAvailableException; 
import com.google.android.gms.common.GooglePlayServicesRepairableException; 
import com.google.android.gms.common.api.GoogleApiClient; 
import com.google.android.gms.common.api.Status; 
import com.google.android.gms.location.LocationListener; 
import com.google.android.gms.location.LocationRequest; 
import com.google.android.gms.location.LocationServices; 
import com.google.android.gms.location.places.Place; 
import com.google.android.gms.location.places.ui.PlaceAutocomplete; 
import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.OnMapReadyCallback; 
import com.google.android.gms.maps.SupportMapFragment; 
import com.google.android.gms.maps.model.BitmapDescriptorFactory; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.Marker; 
import com.google.android.gms.maps.model.MarkerOptions; 

import java.util.List; 
import java.util.Locale; 

/** 
* Created by noufal on 19/7/16. 
*/ 
public class NewMapsActivity extends AppCompatActivity implements 
     OnMapReadyCallback, 
     GoogleApiClient.ConnectionCallbacks, 
     GoogleApiClient.OnConnectionFailedListener, 
     GoogleMap.OnMarkerClickListener, 
     GoogleMap.OnMarkerDragListener, 
     LocationListener { 

public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99; 
     GoogleApiClient mGoogleApiClient; 
     Location mLastLocation; 
     Marker mCurrLocationMarker; 
     LocationRequest mLocationRequest; 
     Toolbar toolbar; 
private GoogleMap mMap; 
private CoordinatorLayout mCoordinatorLayout; 

     @Override 
protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_maps); 
       toolbar = (Toolbar) findViewById(R.id.toolbar); 
       mCoordinatorLayout = (CoordinatorLayout) findViewById(R.id.signup_coordinator); 
       setSupportActionBar(toolbar); 
       getSupportActionBar().setDisplayShowTitleEnabled(false); 
     if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) 
     { 
     checkLocationPermission(); 
     } 
     // 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.setMapType(GoogleMap.MAP_TYPE_NORMAL); 

     //Initialize Google Play Services 
     if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
     if (ContextCompat.checkSelfPermission(this, 
     Manifest.permission.ACCESS_FINE_LOCATION) 
     == PackageManager.PERMISSION_GRANTED) { 
     buildGoogleApiClient(); 
     mMap.setMyLocationEnabled(true); 
     } 
     } 
     else { 
     buildGoogleApiClient(); 
     mMap.setMyLocationEnabled(true); 
     } 
     } 

protected synchronized void buildGoogleApiClient() { 
     mGoogleApiClient = new GoogleApiClient.Builder(this) 
     .addConnectionCallbacks(this) 
     .addOnConnectionFailedListener(this) 
     .addApi(LocationServices.API) 
     .build(); 
     mGoogleApiClient.connect(); 
     } 

@Override 
public void onConnected(Bundle bundle) { 

     mLocationRequest = new LocationRequest(); 
     mLocationRequest.setInterval(1000); 
     mLocationRequest.setFastestInterval(1000); 
     mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); 
     if (ContextCompat.checkSelfPermission(this, 
     Manifest.permission.ACCESS_FINE_LOCATION) 
     == PackageManager.PERMISSION_GRANTED) { 
     LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
     } 

     } 

@Override 
public void onConnectionSuspended(int i) { 

     } 

@Override 
public void onLocationChanged(Location location) { 

     mLastLocation = location; 
     if (mCurrLocationMarker != null) { 
     mCurrLocationMarker.remove(); 
     } 

     //Place current location marker 
     LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude()); 
     System.out.println("Current Latitude:"+location.getLatitude()); 
     System.out.println("Current Longitude:"+location.getLongitude()); 

     String mAddress=getCompleteAddressString(location.getLatitude(), location.getLongitude()); 
     System.out.println("mAddress:"+mAddress); 
     MarkerOptions markerOptions = new MarkerOptions(); 
     markerOptions.position(latLng); 
     markerOptions.title(mAddress); 

     markerOptions.draggable(true); 
     markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)); 
     mCurrLocationMarker = mMap.addMarker(markerOptions); 
     mCurrLocationMarker.showInfoWindow(); 
     //move map camera 
     mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
     mMap.animateCamera(CameraUpdateFactory.zoomTo(16)); 

     //stop location updates 
     if (mGoogleApiClient != null) { 
     LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); 
     } 

     } 
     @Override 
     public boolean onMarkerClick(Marker marker) { 
       return true; 
     } 

     @Override 
     public void onMarkerDragStart(Marker marker) { 
       LatLng position=marker.getPosition(); 

       Log.d(getClass().getSimpleName(), String.format("Drag from %f:%f", 
         position.latitude, 
         position.longitude)); 
     } 

     @Override 
     public void onMarkerDrag(Marker marker) { 
       LatLng position=marker.getPosition(); 

       Log.d(getClass().getSimpleName(), 
         String.format("Dragging to %f:%f", position.latitude, 
           position.longitude)); 
     } 

     @Override 
     public void onMarkerDragEnd(Marker marker) { 
       LatLng position=marker.getPosition(); 

       Log.d(getClass().getSimpleName(), String.format("Dragged to %f:%f", 
         position.latitude, 
         position.longitude)); 
     } 
@Override 
public void onConnectionFailed(ConnectionResult connectionResult) { 

     } 

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

     // Asking user if explanation is needed 
     if (ActivityCompat.shouldShowRequestPermissionRationale(this, 
     Manifest.permission.ACCESS_FINE_LOCATION)) { 

     // Show an explanation to the user *asynchronously* -- don't block 
     // this thread waiting for the user's response! After the user 
     // sees the explanation, try again to request the permission. 

     //Prompt the user once explanation has been shown 
     ActivityCompat.requestPermissions(this, 
     new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 
     MY_PERMISSIONS_REQUEST_LOCATION); 


     } else { 
     // No explanation needed, we can request the permission. 
     ActivityCompat.requestPermissions(this, 
     new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 
     MY_PERMISSIONS_REQUEST_LOCATION); 
     } 
     return false; 
     } else { 
     return true; 
     } 
     } 

@Override 
public void onRequestPermissionsResult(int requestCode, 
     String permissions[], int[] grantResults) { 
     switch (requestCode) { 
     case MY_PERMISSIONS_REQUEST_LOCATION: { 
     // If request is cancelled, the result arrays are empty. 
     if (grantResults.length > 0 
     && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

     // permission was granted. Do the 
     // contacts-related task you need to do. 
     if (ContextCompat.checkSelfPermission(this, 
     Manifest.permission.ACCESS_FINE_LOCATION) 
     == PackageManager.PERMISSION_GRANTED) { 

     if (mGoogleApiClient == null) { 
     buildGoogleApiClient(); 
     } 
     mMap.setMyLocationEnabled(true); 
     } 

     } else { 

     // Permission denied, Disable the functionality that depends on this permission. 
     Toast.makeText(this, "permission denied", Toast.LENGTH_LONG).show(); 
     } 
     return; 
     } 

     // other 'case' lines to check for other permissions this app might request. 
     // You can add here other case statements according to your requirement. 
     } 
     } 



     private String getCompleteAddressString(double LATITUDE, double LONGITUDE) { 
       String strAdd = ""; 
       Geocoder geocoder = new Geocoder(this, Locale.getDefault()); 
       try { 
         List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1); 
         if (addresses != null) { 
           Address returnedAddress = addresses.get(0); 
           String address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex() 
           String city = addresses.get(0).getLocality(); 
           String state = addresses.get(0).getAdminArea(); 
           String country = addresses.get(0).getCountryName(); 
           String postalCode = addresses.get(0).getPostalCode(); 
           String knownName = addresses.get(0).getFeatureName(); 
           StringBuilder strReturnedAddress = new StringBuilder(""); 

           for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) { 
             strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n"); 
           } 
           strAdd = strReturnedAddress.toString(); 
           Log.w("MyCurrentctionaddress", "" + strReturnedAddress.toString()); 
         } else { 
           Log.w("MyCurrentloctionaddress", "NoAddress returned!"); 
         } 
       } catch (Exception e) { 
         e.printStackTrace(); 
         Log.w("MyCurrentloctionaddress", "Canont get Address!"); 
       } 
       return strAdd; 
     } 
     // Menu icons are inflated just as they were with actionbar 
     @Override 
     public boolean onCreateOptionsMenu(Menu menu) { 
       // Inflate the menu; this adds items to the action bar if it is present. 
       getMenuInflater().inflate(R.menu.map_menu, menu); 

       return true; 
     } 
     @Override 
     public boolean onOptionsItemSelected(MenuItem item) { 

       switch (item.getItemId()) { 
         case R.id.action_map_search: 
           findPlace(); 

       } 
       return super.onOptionsItemSelected(item); 
     } 
     public void findPlace() { 
       try { 
         Intent intent = 
           new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_FULLSCREEN) 
             .build(this); 
         startActivityForResult(intent, 1); 
       } catch (GooglePlayServicesRepairableException e) { 
         // TODO: Handle the error. 
       } catch (GooglePlayServicesNotAvailableException e) { 
         // TODO: Handle the error. 
       } 
     } 
     // A place has been received; use requestCode to track the request. 
     @Override 
     protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
       if (requestCode == 1) { 
         if (resultCode == RESULT_OK) { 
           Place place = PlaceAutocomplete.getPlace(this, data); 
           Log.e("Tag", "Place: " + place.getAddress() + place.getPhoneNumber() + place.getLatLng().latitude); 
           mMap.clear(); 
           LatLng latLng = new LatLng(place.getLatLng().latitude,place.getLatLng().longitude); 

           MarkerOptions markerOptions = new MarkerOptions(); 
           markerOptions.position(latLng); 
           markerOptions.title(place.getName().toString()); 

           markerOptions.draggable(true); 
           markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)); 
           mCurrLocationMarker = mMap.addMarker(markerOptions); 
           mCurrLocationMarker.showInfoWindow(); 
           //move map camera 
           mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
           mMap.animateCamera(CameraUpdateFactory.zoomTo(16)); 


         } else if (resultCode == PlaceAutocomplete.RESULT_ERROR) { 
           Status status = PlaceAutocomplete.getStatus(this, data); 
           // TODO: Handle the error. 
           Log.e("Tag", status.getStatusMessage()); 

         } else if (resultCode == RESULT_CANCELED) { 
           // The user canceled the operation. 
         } 
       } 
     } 
} 
+0

你可以請張貼你的xml文件嗎? – Lawrance

回答

4

您需要添加這對您的onMapReady方法:從新LatLngonMapReady()

mMap.setOnMarkerDragListener(this); 

mMap.setOnMarkerDragListener(this); 
+0

謝謝..它的作品。 – Noufal

2

添加以下行讓您的標記的新位置和onMarkerDragEnd()和取地址:

// New Marker Position 
LatLng position = marker.getPosition(); 
// Remove Old Marker 
mCurrLocationMarker.remove(); 
// Add New marker position 
markerOptions.position(position); 
// Add Title on Marker 
markerOptions.title(getCompleteAddressString(position.latitude, position.longitude)); 
// Set Draggable 
markerOptions.draggable(true); 
// Set Icon of Marker 
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)); 
// Add marker on map 
mCurrLocationMarker = mMap.addMarker(markerOptions); 
// Show Info window on marker 
mCurrLocationMarker.showInfoWindow(); 
+0

謝謝你的回答。我提高了你的答案。 – Noufal