我需要找到标记位置,但显示的位置是当前位置

时间:2018-06-01 12:12:15

标签: android google-maps

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    private Button next;
    private Marker marker;

    LocationManager locationManager;

    int PLACE_PICKER_REQUEST = 1;
    PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();

    static final int REQUEST_LOCATION = 1;
    double lat, lon;

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

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        next = findViewById(R.id.mapbutton);



        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                LatLng position = marker.getPosition();
                String latitude = Double.toString(position.latitude);
                String longitude = Double.toString(position.longitude);

                RetrieveData.latitudeRet = latitude;
                RetrieveData.longitudeRet = longitude;

                startActivity(new Intent(MapsActivity.this, ComplaintPicture.class));
            }
        });

    }

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

        if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {

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

        }else{
            mMap.setMyLocationEnabled(true);
            Location currentLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

            if(currentLocation != null){
                lat = currentLocation.getLatitude();
                lon = currentLocation.getLongitude();
            }
        }

        // Add a marker

        LatLng location = new LatLng(lat , lon);
        marker = mMap.addMarker(new MarkerOptions().position(location).draggable(true));
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(location, 20));
    }
}

可以看到当前位置并且还添加了标记。拖动标记并放置在任何位置后,从标记位置获取的位置与当前位置相同。我用marker.getPosition()来获取纬度和经度。我想所示的latlng是应用程序启动时的初始标记位置。移动后标记位置没有刷新。

0 个答案:

没有答案