【问题标题】:Using location based services from my app - Android从我的应用程序中使用基于位置的服务 - Android
【发布时间】:2011-06-23 14:13:03
【问题描述】:

我想向用户展示当地的兴趣点。 由于这不是我的应用程序的主要目标,我希望找到一个相当简单的解决方案,例如将用户发送到 Google Places 或任何其他基于位置的应用程序。

有没有办法做到这一点? 如果答案是否定的,我该怎么做?也许使用一些 API?

谢谢

【问题讨论】:

    标签: android gps location point-of-interest


    【解决方案1】:

    您可以使用记录在here 的android 位置和地图API。如果没有,您可以随时调用地图意图,但您必须确保在使用之前安装了谷歌地图。这是一个使用链接here的android开发页面中的Location api的示例:

    // 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 here 注册您的应用程序。获得 API 密钥后,您可以使用 http 请求到 https://maps.googleapis.com/maps/api/place/search/json?location=&radius=&types=&name=&sensor=&key=YOUR_API_KEY

    您可以从 google maps api 网站here 找到每个参数的定义,但这是一个小列表:

    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.
    

    这是一个在 android 中的快速 HTTP 请求示例:

    HttpGet myGet = new HttpGet("https://maps.googleapis.com/maps/api/place/search/json?location=&radius=&types=&name=&sensor=&key=YOUR_API_KEY");
    

    获得返回结果后,您可以使用任何 json 库(例如来自 here 的 google-gson)解析响应。

    瑞恩

    【讨论】:

    • 谢谢,但是如果我需要向用户展示附近的酒吧(例如)怎么办?我认为我不能为此使用默认位置服务(如果我错了,请纠正我)。
    • 好吧,找到用户位置后,您可以使用带有 HTTP 请求的 google maps api。有关详细信息,请参阅here。简而言之,您将使用适当的参数(在上面的链接中定义)对 maps.googleapis.com/maps/api/place/search/output?parameters 进行 http 调用,并且该函数将返回附近位置的 json 响应。如果您需要 Android 的示例代码,请告诉我。要使此方法起作用,您需要在 google 上注册一个应用程序,以便在请求中传递密钥。
    • 谢谢,你真的帮了我。如果可能的话,我真的很喜欢一个示例代码,它肯定会帮助我完成这项工作。谢谢
    • @tofira,我编辑了我的答案,向您展示了如何进行此操作的快速示例。
    【解决方案2】:

    您的问题不清楚,但从您的问题中,我可以知道您想显示用户感兴趣的位置,如果我错了,请纠正我。

    如果你想一样,那么

    1) 你需要这些点的纬度和经度 为此,您可以使用任何 API,您可以从您的网络服务中获取这些 API

    2) 获取经纬度列表后,您可以使用 Itemized Overlay 在地图上显示它们

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-09
      • 1970-01-01
      • 1970-01-01
      • 2012-09-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多