通过这个主题的所有帖子阅读并尝试了所有组合: 1.在进行google places API调用时使用了android键( 2.切换到浏览器键 3.后来尝试使用服务器密钥。 4.重新生成密钥并尝试组合1-3。
什么都没有用!!清单文件中Mapv2 API的密钥是android密钥。这个应用程序工作正常,直到我创建了一个新项目并使用新项目列出了我的包。我为这个包重新创建了新的android密钥。旧密钥仍在那里,但有一个不同的项目。我没有删除旧项目但删除了它下面的所有密钥。 所以现在我在新项目下有了新的android键,浏览器。 我做错了吗?
我收到错误消息为"提供的API密钥无效" ... 当我使用浏览器密钥从浏览器进行此调用时,它正在工作。不是来自我的Android应用程序。 有小费吗 ?。
请参阅以下代码: -
final String PLACES_API_BASE = "https://maps.googleapis.com/maps/api/place/nearbysearch";
final String OUT_JSON = "/json";
final String KEY=<browser-key>
final String SENSOR="false";
StringBuilder querystring = new StringBuilder(PLACES_API_BASE+OUT_JSON);
try{
querystring.append("?sensor="+SENSOR+"&key="+KEY);
String localquery="&location=37.316318,-122.005916&radius=500&name=traderjoe";
querystring.append(URLEncoder.encode(localquery, "UTF-8"));
URL url = new URL(querystring.toString());
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
String line;
StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
while((line = reader.readLine()) != null) {
builder.append(line);
}
System.out.println("JSON BUILDER INPUT FROM GOOGLE PLACES QUERY="+builder.toString());
这是我收到错误消息的地方: - 提供的APi密钥是invaild
答案 0 :(得分:1)
以下是我在CSharp中使用googlePlaceApi的代码
public List<GooglePlace> GoogleApiPlace(Coordinate startCoordinate)
{
List<GooglePlace> ListOfPlace = new List<GooglePlace> ();
string apiURL = "https://maps.googleapis.com/maps/api/place/nearbysearch/xml?location=";
string apiKey = "myKey";
string linkApi = apiURL + startCoordinate.Latitude.ToString ().Replace (',', '.') + "," + startCoordinate.Longitude.ToString ().Replace (',', '.') + "&radius=1000&sensor=true&types=establishment&key=" + apiKey;
var request = HttpWebRequest.Create(linkApi);
request.ContentType = "application/xml";
request.Method = "GET";
try
{
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
if (response.StatusCode != HttpStatusCode.OK)
{
Console.Out.WriteLine("Error fetching data, Server returned status code {0}", response.StatusCode);
}
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
var content = reader.ReadToEnd();
if (string.IsNullOrWhiteSpace(content))
{
Console.Out.WriteLine("Responsed contained empty body...");
}
else
{
XmlDocument document = new XmlDocument();
document.LoadXml(content);
Console.Out.WriteLine(content);
XmlNodeList nodeList = document.GetElementsByTagName("result");
foreach (XmlNode docNode in nodeList)
{
string tot = ((XmlElement)docNode).GetElementsByTagName("lat")[0].InnerText;
GooglePlace place = new GooglePlace()
{
Location = new Location()
{
Coordinate = new Coordinate()
{
Latitude = Double.Parse(((XmlElement)docNode).GetElementsByTagName("lat")[0].InnerText, CultureInfo.InvariantCulture),
Longitude = Double.Parse(((XmlElement)docNode).GetElementsByTagName("lng")[0].InnerText, CultureInfo.InvariantCulture)
}
},
Name = docNode.ChildNodes.Item(0).InnerText.TrimEnd().TrimStart(),
Vicinity = docNode.ChildNodes.Item(1).InnerText.TrimEnd().TrimStart()
};
ListOfPlace.Add(place);
}
}
}
}
}
catch
{
//TODO : Exception
}
return ListOfPlace;
}
Api Key就在这里
希望这可以帮到你;)
答案 1 :(得分:0)
答案 2 :(得分:0)
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
string jsonString = string.Empty;
string url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=500&types=restaurant&key=[Your_APIKey]";
using (System.Net.WebClient client = new WebClient())
{
jsonString = client.DownloadString(url);
}
var valueSet = JsonConvert.DeserializeObject<RootObject>(jsonString);
}
}
public class Location
{
public double lat { get; set; }
public double lng { get; set; }
}
public class Geometry
{
public Location location { get; set; }
}
public class OpeningHours
{
public bool open_now { get; set; }
public List<object> weekday_text { get; set; }
}
public class Photo
{
public int height { get; set; }
public List<object> html_attributions { get; set; }
public string photo_reference { get; set;}
public int width { get; set; }
}
public class Result
{
public Geometry geometry { get; set; }
public string icon { get; set; }
public string id { get; set; }
public string name { get; set; }
public OpeningHours opening_hours { get; set; }
public List<Photo> photos { get; set; }
public string place_id { get; set; }
public double rating { get; set; }
public string reference { get; set; }
public string scope { get; set; }
public List<string> types { get; set; }
public string vicinity { get; set; }
public int? price_level { get; set; }
}
public class RootObject
{
public List<object> html_attributions { get; set; }
public string next_page_token { get; set; }
public List<Result> results { get; set; }
public string status { get; set; }
}
答案 3 :(得分:0)
您必须在Google控制台中启用“Google Places API for Android”而非“Google Places API for web services”。如果您使用Android中的地方网络服务,则必须启用“Google商家信息系统API for Web Services”。密钥将是一个浏览器密钥。