2011-06-23 44 views
0

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

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

感谢

回答

3

您可以使用android位置和地图API文档here。如果没有,您可以随时致电Google地图的意图,但您必须确保在使用Google地图之前先安装Google地图。下面是使用Location API从Android开发人员网页为例链接here

// 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); 

现在注册与谷歌API here您的应用程序。一旦你得到你的API密钥,你可以使用http请求https://maps.googleapis.com/maps/api/place/search/json?location=&radius=&types=&name=&sensor=&key=YOUR_API_KEY

你可以找到从谷歌地图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)来解析响应。

瑞安

+0

谢谢,但是如果我需要显示用户附近的酒吧(例如)呢?我不认为我可以使用默认的位置服务(纠正我,如果我错了)。 – Tofira

+0

那么,在找到用户位置后,您可以使用带有HTTP请求的Google Maps API。有关更多详情,请参见[此处](http://code.google.com/apis/maps/documentation/places/#PlaceSearches)。简而言之,您将使用适当的参数(在上面的链接中定义)对https://maps.googleapis.com/maps/api/place/search/output?parameters进行http调用,该函数将返回一个json响应与附近的地方。如果您需要Android的示例代码,请告诉我。要使这种方法起作用,您需要使用Google的注册应用程序来传递请求中的密钥。 – Ryan

+0

谢谢,你真的帮了我。如果可能的话,我真的很喜欢示例代码,它肯定会帮助我完成这项工作。感谢 – Tofira

0

你的问题还不清楚,但从你的问题,我能够知道你要显示用户感兴趣的位置,纠正我,如果我错了。

,如果你想同然后

1),你需要纬度和那经度点 的,你可以用你的任何API获得的列表后,可以得到那些从你的web服务

2)纬度和经度,你可以通过使用分项叠加显示在地图上的那些

相关问题