单击位置以自动完成片段

时间:2017-10-06 07:58:12

标签: android google-maps location

当我点击地图时,自动填充片段中是否有可能显示我的位置?

我的意思是,当有人想要通过地图而不是位置地址搜索地点时,即通过在地图上查找地点并按住/触摸/点击街道,那么该地点将被标记我希望将街道名称发送到我的片段。

这是我目前的代码 - 我希望你能理解我的问题:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);



    AutocompleteFilter typeFilter = new AutocompleteFilter.Builder()
            .setTypeFilter(AutocompleteFilter.TYPE_FILTER_ADDRESS)
            .build();
    PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)
            getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);

    SharedPreferences locationsp = PreferenceManager.getDefaultSharedPreferences(this);
    final String sp = locationsp.getString("Location2","No location found");
    //search.setText(sp);
    if(!sp.equals("No location found")){
        autocompleteFragment.setText(sp);
        tmp=sp;
    }


    autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
        @Override
        public void onPlaceSelected(Place place) {
            // TODO: Get info about the selected place.
            onSearch(place);
            Log.i(TAG, "Place: " + place.getName());
        }


        @Override
        public void onError(Status status) {
            // TODO: Handle the error.
            Log.i(TAG, "An error occurred: " + status);
        }
    });

    try {
        Intent intent = new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_OVERLAY)
                        .setFilter(typeFilter)
                        .build(this);
    } catch (GooglePlayServicesRepairableException e) {
        // TODO: Handle the error.
    } catch (GooglePlayServicesNotAvailableException e) {
        // TODO: Handle the error.
    }




    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == PLACE_AUTOCOMPLETE_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            Place place = PlaceAutocomplete.getPlace(this, data);
            Log.i(TAG, "Place: " + place.getName());
        } else if (resultCode == PlaceAutocomplete.RESULT_ERROR) {
            Status status = PlaceAutocomplete.getStatus(this, data);
            // TODO: Handle the error.
            Log.i(TAG, status.getStatusMessage());

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

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

    locationt = findViewById(R.id.mapViewfrag);
    savelocation = findViewById(R.id.savelocation);
    //search = findViewById(R.id.GameLocation);

    map.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
        @Override
        public void onMapClick(LatLng latLng) {
            MarkerOptions markerOptions = new MarkerOptions();
            markerOptions.position(latLng);
            markerOptions.title("You are here");
            markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));

            PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)
            getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);

            map.clear();
            Marker marker = map.addMarker(markerOptions);
            marker.showInfoWindow();
            map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(marker.getPosition().latitude,marker.getPosition().longitude), 15));
            latitude = marker.getPosition().latitude;
            longtitude = marker.getPosition().longitude;
            try {
                Geocoder geo = new Geocoder(MapActivity.this.getApplicationContext(), Locale.getDefault());
                addresses = geo.getFromLocation(latitude, longtitude, 1);
                System.out.println(addresses);


                if (addresses.isEmpty()) {
                    locationt.setText("Waiting for Location");
                } else {
                    if (addresses.size() > 0) {                          locationt.setText(addresses.get(0).getAddressLine(0));
                        //autocompleteFragment.getText(); <<<Thats what i try but it makes me error
                       locationname=locationt.getText().toString();
}

0 个答案:

没有答案