PlaceAutocomplete - 输入坐标

时间:2017-09-26 15:05:37

标签: android google-maps-api-2

在进入坐标后,如何正确地实现这一点,他设置了标记,但没有说"没有找到任何东西"如图所示?

enter image description here

我的猜测是抓住这个错误状态如下:

@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。抱歉我的英文。

2 个答案:

答案 0 :(得分:0)

它只支持名称搜索,换句话说,没有名为55,33.00的地方。

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

Place picker正是您要找的。 祝你好运

埃姆雷

答案 1 :(得分:0)

不幸的是,我没有发现任何正式的内容。

<强> BUT

P.S。忘掉PlaceAutocomplete。

HARD LEVEL

开始设计: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;
    }

}

结果2:

введите сюда описание изображения

结果3:

введите сюда описание изображения

祝你好运:)