PlaceAutocomplete - 输入坐标

PlaceAutocomplete - enter coordinates

如何正确实现输入坐标后,他设置了marker,却没有说“没有找到”如图所示?

我的猜测是捕捉这个错误状态如下:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    try {
        switch(requestCode){
            case PLACE_AUTOCOMPLETE_REQUEST_CODE:
                switch(resultCode){
                    case RESULT_OK:
                         ...
                         break;
                    case RESULT_ERROR:
                         if (!PlaceAutocomplete.getStatus(this, data).isSuccess()) {
                              markerPlace = map.addMarker(new MarkerOptions().position(PlaceAutocomplete.getPlace(this, data).getLatLng()).icon(vectorToBitmap(R.drawable.ic_place_black_24dp, ContextCompat.getColor(getApplicationContext(), R.color.colorPrimaryDark))));
                              autocompleteFragment.setText(PlaceAutocomplete.getPlace(this, data).getLatLng() + "");
                         } 
                         break;
                    }
                    break;
            }
        } catch (NullPointerException ex) {
            ex.printStackTrace();
        }

但这行不通...

P.S。对不起我的英语。

只支持名称搜索,也就是说,没有名为55,33.00的地方。

您可以检查Place Autocomplete Documantation 编程搜索的参考文献,没有看到任何让用户使用 latlng 进行搜索的方法。

Place picker 就是您要找的。

祝你好运

埃姆雷

很遗憾,我没有找到任何官方信息。

但是

P.S。忘记 PlaceAutocomplete。


困难级别


开始设计:CustomSearchBox

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardCornerRadius="3dp"
    card_view:cardElevation="3dp"
    card_view:cardPreventCornerOverlap="false"
    card_view:cardUseCompatPadding="true">

    <android.support.v7.widget.Toolbar
        android:id="@+id/tool_bar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:theme="@style/ToolbarColoredBackArrowBlack">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">

            <CustomEditText
                android:id="@+id/searchPlace"
                style="@style/CustomEditViaGoogleMaps"
                android:layout_height="wrap_content"
                android:layout_width="0dp"
                android:layout_weight="1.8"
                android:cursorVisible="false"
                android:ellipsize="end"
                android:hint="@string/place_search_hint"
                android:imeOptions="actionSearch|flagNoFullscreen"
                android:inputType="numberDecimal"
                android:digits="0123456789.,"
                android:maxLines="1"
                android:scrollHorizontally="true"
                android:textColor="@android:color/black"
                android:textSize="20sp" />

            <ImageView
                android:id="@+id/clearSearch"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.2"
                android:layout_gravity="center"
                android:layout_marginStart="9dp"
                android:layout_marginEnd="14dp"
                android:visibility="gone"
                android:contentDescription="@string/app_name"
                app:srcCompat="@drawable/ic_clear_black_24dp" />

        </LinearLayout>

    </android.support.v7.widget.Toolbar>

</android.support.v7.widget.CardView>

风格:

<style name="ToolbarColoredBackArrowBlack" parent="AppTheme">
        <item name="android:textColorSecondary">@color/color_toolbar_via_google_maps</item>
        <item name="android:textColorPrimary">@color/color_toolbar_via_google_maps</item>
    </style>

<style name="CustomEditViaGoogleMaps">
        <item name="android:theme">@style/EditAppThemeViaGoogleMaps</item>
    </style>
    <style name="EditAppThemeViaGoogleMaps">
        <item name="colorControlActivated">@color/color_toolbar_via_google_maps</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:textColorHint">@color/color_text_toolbar_via_google_maps</item>
        <item name="colorControlNormal">@android:color/white</item>
        <item name="colorControlHighlight">@android:color/white</item>
        <item name="android:colorButtonNormal">@android:color/white</item>
    </style>

颜色:

<color name="color_toolbar_via_google_maps">#616161</color>
<color name="color_text_toolbar_via_google_maps">#BDBDBD</color>

结果:

Java

Marker markerPlace;
CustomEditText placeSearch;
ImageView clearSearch;
...

@Override
public void onCreate(Bundle savedInstanceState) {
    ....
    placeSearch = findViewById(R.id.searchPlace);
    clearSearch = findViewById(R.id.clearSearch);
    placeSearch();
}

public void placeSearch() {
    placeSearch.getBackground().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);//line edit to white
    placeSearch.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            placeSearch.setCursorVisible(true);
        }
    });
    placeSearch.setOnEditorActionListener(new EditText.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                ((InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(placeSearch.getWindowToken(), 0);
                placeSearch.setCursorVisible(false);
                String[] latLng = placeSearch.getText().toString().split(",");
                if (markerPlace != null) map.clear();
                markerPlace = map.addMarker(new MarkerOptions().position(new LatLng(Double.parseDouble(latLng[0]), Double.parseDouble(latLng[1]))).icon(vectorToBitmap(R.drawable.ic_place_black_24dp, ContextCompat.getColor(getApplicationContext(), R.color.colorPrimaryDark))));
                map.animateCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition.Builder().target(new LatLng(Double.parseDouble(latLng[0]), Double.parseDouble(latLng[1]))).zoom((float) 15.5).bearing(360).tilt(0).build()));
                markerPlace.setTitle(placeSearch.getText() + "");
                markerPlace.showInfoWindow();
                return true;
            }
            return false;
        }
    });
    placeSearch.addTextChangedListener(new TextWatcher() {
        @Override
        public void afterTextChanged(Editable s) {}
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (s.length() > 0) clearSearch.setVisibility(View.VISIBLE);
            else clearSearch.setVisibility(View.GONE);
        }
    });
    clearSearch.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            markerPlace = null;
            map.clear();
            placeSearch.setText("");
        }
    });
}

public class CustomEditText extends AppCompatEditText {

    Context context;
    CustomEditText placeSearch;

    public CustomEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    @Override
    public boolean onKeyPreIme(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
            ((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(this.getWindowToken(), 0);
            placeSearch = findViewById(R.id.searchPlace);
            placeSearch.setCursorVisible(false);
        }
        return false;
    }

}

结果二:

结果 3:

祝你好运:)