简体   繁体   中英

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

I am starting to learn places api. I managed to create an activity and when you click the button renders the places api location picker with a map. My problem is I can do this with a hardcoded LatLangBounds, what I want is when I render the map is to start on my current location. I have done this using maps api but for this places api I am confused.

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

First, you can obtain your current position using the Location Services . When you have the position, you can take the VisibleRegion of the camera map, using the method onCameraChange() from the maps API. Now with the visible region object you can construct new LatLngBounds with your current position and call the Places API

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM