GoogleMap.OnMarkerDragListener无法正常工作

时间:2016-07-21 05:11:22

标签: android google-maps google-maps-markers google-maps-android-api-2

我很抱歉发布整个代码以避免其他人的混淆。我在我的应用程序中成功地使用了GoogleMap,我也获得了我当前的位置。我的要求是获取标记所在位置的地址。标记是可拖动的,但当我移动或拖动标记从一个地方到另一个地方我没有得到任何东西,甚至没有日志。我搜索了大约2天的解决方案。请尝试帮助我。 这是我的Activity.I我提供的整个代码包括你更好理解的导入。

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.
                        }
                }
        }
}

2 个答案:

答案 0 :(得分:6)

您需要在onMapReady方法中添加此内容:

mMap.setOnMarkerDragListener(this);

答案 1 :(得分:2)

onMapReady()

中添加以下行
mMap.setOnMarkerDragListener(this);

onMarkerDragEnd()中获取您的新标记位置并从新LatLng获取地址:

// 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();