Использование служб определения местоположения из моего приложения - Android
Я хочу показать местные достопримечательности для пользователей. Поскольку это не главная цель моего приложения, я хочу найти довольно простое решение, например, отправить пользователя в Google Places или в любое другое приложение на основе местоположения.
Есть ли способ сделать это? Если ответ нет, как я могу это сделать? Может быть, используя какой-то API?
Спасибо
2 ответа
Вы можете использовать расположение Android и карты API, описанные здесь. Если нет, вы всегда можете позвонить в Google Maps, но вы должны убедиться, что Google Maps установлен, прежде чем использовать его. Вот пример использования Location
API со страницы разработчика Android, ссылки на которые приведены здесь:
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
makeUseOfNewLocation(location);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
Теперь зарегистрируйте ваше приложение в Google API здесь. Получив ключ API, вы можете использовать http-запрос https://maps.googleapis.com/maps/api/place/search/json?location=&radius=&types=&name=&sensor=&key=YOUR_API_KEY
,
Вы можете найти определение каждого параметра на сайте API Google Maps здесь, но это небольшой список:
location (required) — The latitude/longitude around which to retrieve Place information. This must be provided as a google.maps.LatLng object.
radius (required) — The distance (in meters) within which to return Place results. The recommended best practice is to set radius based on the accuracy of the location signal as given by the location sensor. Note that setting a radius biases results to the indicated area, but may not fully restrict results to the specified area.
types (optional) — Restricts the results to Places matching at least one of the specified types. Types should be separated with a pipe symbol (type1|type2|etc). See the list of supported types.
language (optional) — The language code, indicating in which language the results should be returned, if possible. See the list of supported languages and their codes. Note that we often update supported languages so this list may not be exhaustive.
name (optional) — A term to be matched against the names of Places. Results will be restricted to those containing the passed name value. When a name is included, the area being searched may be broadened, to ensure a suitable number of results.
sensor (required) — Indicates whether or not the Place request came from a device using a location sensor (e.g. a GPS) to determine the location sent in this request. This value must be either true or false.
key (required) — Your application's API key. This key identifies your application for purposes of quota management and so that Places added from your application are made immediately available to your app. Visit the APIs Console to create an API Project and obtain your key.
Вот быстрый пример HTTP-запроса в Android:
HttpGet myGet = new HttpGet("https://maps.googleapis.com/maps/api/place/search/json?location=&radius=&types=&name=&sensor=&key=YOUR_API_KEY");
Получив возвращаемый результат, вы можете проанализировать ответ, используя любую библиотеку json, например, google-gson.
Райан
Ваш вопрос не ясен, но из вашего вопроса я могу узнать, что вы хотите отобразить местоположение, представляющее интерес для пользователя, поправьте меня, если я ошибаюсь.
если хочешь то же самое то
1) вам нужны широта и долгота этих точек, чтобы вы могли использовать любой API, который вы можете получить из своего веб-сервиса
2) после получения списка широты и долготы вы можете отобразить их на карте, используя наложенное наложение