簡體   English   中英

如何在當前位置加載Google的Android Places API

[英]How to load google's android places api in your current location

我開始學習Place api。 我設法創建了一個活動,當您單擊按鈕時,使用地圖呈現了places api位置選擇器。 我的問題是我可以使用硬編碼的LatLangBounds做到這一點,我想要的是渲染地圖時從當前位置開始。 我已經使用maps api做到了,但是對於這個地方api我很困惑。

private static final int PLACE_PICKER_REQUEST = 1;
private TextView mName;
private TextView mAddress;
private TextView mAttributions;
private static final LatLngBounds BOUNDS_HARD_CODED_LOCATION = new LatLngBounds(
        new LatLng(37.398160, -122.180831), new LatLng(37.430610, -121.972090));

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

    /**
     * not part of places api standard code to add back function to action bar
     */
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    mName = (TextView) findViewById(R.id.textView);
    mAddress = (TextView) findViewById(R.id.textView2);
    mAttributions = (TextView) findViewById(R.id.textView3);
    Button pickerButton = (Button) findViewById(R.id.pickerButton);
    pickerButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                PlacePicker.IntentBuilder intentBuilder =
                        new PlacePicker.IntentBuilder();
                intentBuilder.setLatLngBounds(BOUNDS_HARD_CODED_LOCATION );
                Intent intent = intentBuilder.build(PlacePickerActivity.this);
                startActivityForResult(intent, PLACE_PICKER_REQUEST);

            } catch (GooglePlayServicesRepairableException
                    | GooglePlayServicesNotAvailableException e) {
                e.printStackTrace();
            }
        }
    });
}

首先,您可以使用Location Services獲取當前職位。 當你有位置,可以采取VisibleRegion相機地圖的,使用該方法onCameraChange()從地圖API。 現在,使用可見區域對象,您可以使用當前位置構造新的LatLngBounds並調用Places API

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM