Back to project page AndroidPlaces.
The source code is released under:
Apache License
If you think the Android project AndroidPlaces listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* * Copyright (C) 2014 Brian Lee/*from w ww .j a v a 2 s. c o m*/ * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.tigerpenguin.demo.places; import android.app.Activity; import android.app.FragmentManager; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Spinner; import com.tigerpenguin.places.model.PlaceType; import com.tigerpenguin.places.request.InvalidRequestException; import com.tigerpenguin.places.request.NearbySearchRequest; import com.tigerpenguin.places.response.NearbySearchResponse; import com.tigerpenguin.places.responsehandler.NearbySearchHandlerFragment; import java.util.ArrayList; import java.util.List; import static com.tigerpenguin.places.responsehandler.NearbySearchHandlerFragment.NearbySearchResponseHandler; public class PlacesDemo extends Activity implements NearbySearchResponseHandler { private static final String TAG_NEARBY_SEARCH_HANDLER = "TAG_NEARBY_SEARCH_HANDLER"; private Spinner demoLocationSpinner; private Spinner placeTypeSpinner; private View searchButton; private PlaceResultsFragment placeResultsFragment; private NearbySearchHandlerFragment nearbySearchHandler; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.places_demo); demoLocationSpinner = (Spinner) findViewById(R.id.spinnerLocation); placeTypeSpinner = (Spinner) findViewById(R.id.spinnerTypes); searchButton = findViewById(R.id.btnSearch); setupSpinners(); setupFragments(); } public void onSearch(View view) { searchButton.setEnabled(false); int radius = 5000; DemoLocation selectedLocation = (DemoLocation) demoLocationSpinner.getSelectedItem(); placeResultsFragment.setOrigin(selectedLocation.getLatitude(), selectedLocation.getLongitude()); NearbySearchRequest nearbySearchRequest = new NearbySearchRequest(this, nearbySearchHandler, selectedLocation.getLatitude(), selectedLocation.getLongitude(), radius); nearbySearchRequest.setTypes((PlaceType) placeTypeSpinner.getSelectedItem()); try { nearbySearchRequest.execute(); } catch (InvalidRequestException e) { e.printStackTrace(); searchButton.setEnabled(true); } } @Override public void handleResponse(NearbySearchResponse response) { searchButton.setEnabled(true); placeResultsFragment.clear(); placeResultsFragment.handleResponse(response); } private void setupSpinners() { ArrayAdapter<DemoLocation> demoLocationAdapter = new ArrayAdapter<DemoLocation>(this, android.R.layout.simple_spinner_item, getDemoLocations()); demoLocationAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); demoLocationSpinner.setAdapter(demoLocationAdapter); ArrayAdapter<PlaceType> placeTypeAdapter = new ArrayAdapter<PlaceType>(this, android.R.layout.simple_spinner_item, getTypes()); placeTypeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); placeTypeSpinner.setAdapter(placeTypeAdapter); } private void setupFragments() { FragmentManager fm = getFragmentManager(); placeResultsFragment = (PlaceResultsFragment) fm.findFragmentById(R.id.placeResults); nearbySearchHandler = (NearbySearchHandlerFragment) fm.findFragmentByTag(TAG_NEARBY_SEARCH_HANDLER); if (nearbySearchHandler == null) { nearbySearchHandler = new NearbySearchHandlerFragment(); fm.beginTransaction().add(nearbySearchHandler, TAG_NEARBY_SEARCH_HANDLER).commit(); } } private List<PlaceType> getTypes() { ArrayList<PlaceType> types = new ArrayList<PlaceType>(); types.add(PlaceType.AMUSEMENT_PARK); types.add(PlaceType.AQUARIUM); types.add(PlaceType.ART_GALLERY); types.add(PlaceType.MUSEUM); types.add(PlaceType.NIGHT_CLUB); types.add(PlaceType.RESTAURANT); types.add(PlaceType.SHOPPING_MALL); return types; } private List<DemoLocation> getDemoLocations() { ArrayList<DemoLocation> locations = new ArrayList<DemoLocation>(); locations.add(new DemoLocation("Las Vegas", 36.10896, -115.17825)); locations.add(new DemoLocation("Chicago", 41.8783, -87.63)); locations.add(new DemoLocation("New York", 40.7614, -73.9826)); return locations; } private static class DemoLocation { private final String name; private final double latitude; private final double longitude; DemoLocation(String name, double latitude, double longitude) { this.name = name; this.latitude = latitude; this.longitude = longitude; } @Override public String toString() { return name; } public double getLatitude() { return latitude; } public double getLongitude() { return longitude; } } }