2017-09-26 66 views
0

在输入坐标后如何正确实现这一点,他设置了标记,但没有说出“没有找到任何东西”,如图所示?PlaceAutocomplete - 输入坐标

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

但是这不工作...

附:对不起我的英语不好。

+0

不PlaceAutoComplete坐标工作?它不应该是地名吗? –

+0

@EmreAktürk地名工作完美,但坐标不工作:( – iFr0z

+0

也许它不支持坐标? –

回答

0

不幸的是,我没有发现任何官员。

附:忘记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=".," 
       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:

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

祝你好运:)

0

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

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

Place picker是你在找什么。

好运

埃姆雷

+0

在文档中,这不是写...现在得出这样的结论为时尚早 – iFr0z