Connecting to Google Places API from a .Net MVC application - android

I am trying to connect to Google Places API from an .net MVC 4 application that acts as a server for a Andorid based application as Google Places API can’t directly be contacted from the device.
Android device will send request to this .Net App and it will retrieve Places data from Google API and return it to Android device.
I am not very experienced with .Net MVC so I need to ask that is there any particular pattern or architecture I should use for this that could help me performance and productivity wise, or I can simply make web service calls from a controller and return it back?

Here is a sample request made to the places API to search for a library within a specified range of a given latitude and logitude. This uses HttpClient to send the request. Note that the response will be in json and there are a variety of ways to deserialize it and act on the data.
using (var client = new HttpClient())
{
var latitude = -33.8670522;
var longitude = 151.1957362;
var key="your api key here";
var response = await client.GetStringAsync(string.Format("https://maps.googleapis.com/maps/api/place/nearbysearch/json?location={0},{1}&radius=500&type=library&key={2}", latitude, longitude, key));
//here is an example of using Newtonsoft.JSON to deserialize the response
//json string which assumes you have a class called PlacesApiQueryResponse
var result = JsonConvert.DeserializeObject<PlacesApiQueryResponse>(response);
}

Related

Can I use my Android API key when making http request to Google Map API?

I have created an API key for my Android app like this in the Google Cloud Platform console
and then I use that Android API key to make request to Google Map API (reverse geocoding) to this endpoint below using HTTP client like retrofit or Dio (directly from my Android client app)
https://maps.googleapis.com/maps/api/geocode/json?key=AIzaSyCxxxxxxxxxxxddgaQTwwlYDBmP8&latlng=-6.150934,106.8251672
but unfortunately I get error json response from Google:
This IP, site or mobile application is not authorized to use this API
key. Request received from IP address 180.111.11.111, with empty
referer
Can I use my Android API key when making http request from my Android client App to Google Map API? what should I do to make request to Google Map API from my Android client app? should I make my own endpoint as a 'wrapper' instead?
Can I use my Android API key when making http request from my Android client App to Google Map API?
No. Instead of using two keys, you should make a call through the SDK, https://developer.android.com/reference/android/location/Geocoder.
See this answer.
Geocoder geoCoder = new Geocoder(context);
List<Address> matches = geoCoder.getFromLocation(latitude, longitude, 1);
Address bestMatch = (matches.isEmpty() ? null : matches.get(0));

Android Place SDK restricted API key is not working

I know that place API requires server key to use but I am using place SDK for android. I am using restricted Android API key (Restricted using package name and SHA1) but it is giving me below error:
This IP, site or mobile application is not authorized to use this API
key. Request received from IP address 203.122.438.42, with empty
referer
For reference, I am pasting my code below:
public PlacesSearchResponse fetch(com.google.maps.model.LatLng location) {
PlacesSearchResponse request = new PlacesSearchResponse();
GeoApiContext context = new GeoApiContext.Builder()
.apiKey(BuildConfig.MAPS_API_KEY)
.build();
try {
request = PlacesApi.nearbySearchQuery(context, location)
.radius(5000)
.keyword("Shivam")
.type(PlaceType.BAKERY)
.await();
} catch (Exception e) {
e.printStackTrace();
} finally {
return request;
//Log.d("==", "=====" + request.results[0].name);
}
}
Dependency I am using:
> //To get near by locations
> implementation 'com.google.maps:google-maps-services:0.18.0'
> implementation 'com.squareup.okhttp3:okhttp:4.9.0'
You are not using Places SDK for Android. As far as I can see from your code you are using a Java wrapper for Google Maps web services. Web services are always intended to be used on server side and the only restriction that they accept is an IP address restriction on API key.
Have a look at this Java library in github (https://github.com/googlemaps/google-maps-services-java) and pay attention to the documentation, especially "Intended usage of this library" section. It states
The Java Client for Google Maps Services is designed for use in server applications. This library is not intended for use inside of an Android app, due to the potential for loss of API keys.
If you are building a mobile application, you will need to introduce a proxy server to act as intermediary between your mobile application and the Google Maps API Web Services. The Java Client for Google Maps Services would make an excellent choice as the basis for such a proxy server.
So, as you can see, the suggested by Google solution is introducing an intermediate proxy server where you can use this library with an API key restricted by IP address of your intermediate proxy server.

How can we get google map search result as json file?

I want to create an app that list items, that get by search in google map.
Its only for display particular details about near hospital. I don't know how to convert map result into json object
I am beginner in android development
The Google Maps API provides these web services as an interface for requesting Maps API data from external services and using them within your Maps applications.
These web services use HTTP requests to specific URLs, passing URL parameters as arguments to the services. Generally, these services return data in the HTTP request as either JSON or XML for parsing and/or processing by your application.
A typical web service request is generally of the following form:
https://maps.googleapis.com/maps/api/service/output?parameters
where service indicates the particular service requested and output indicates the response format (usually json or xml).
JSON (Javascript Object Notation) has an obvious advantage over XML in that the response is lightweight. Parsing such a result is trivial in JavaScript as the format is already a valid Javascript object. For example, to extract the value of the 'formatted_address' keys within a JSON result object, simply access them using the following code:
for (i = 0; i < myJSONResult.results.length; i++) {
myAddress[i] = myJSONResult.results[i].formatted_address;
}
Use this to search places from google maps
String placesSearchStr = "https://maps.googleapis.com/maps/api/place/nearbysearch/" +
"json?location="+lat+","+lng+
"&radius=1000&sensor=true" +
"&types=hospital"+
"&key=your_key_here";

Have Android app users create and change objects on Google Cloud Storage

I am looking for a strategy to have global objects that can be accessed across all users on all devices. My idea is to create an object or file and put it on Google Cloud Server, and using Datastore, Blobstore, or Cloud Storage (or maybe something else?), and have the object/file change as different users interact with it and alter variable values.
Now, how in the world can I do this - I am having a lot of trouble understanding the documentation that Google offers. Are there any convenient APIs for this? If so, how can these API's be accessed? Currently, I have followed the Android Studio "Hello Endpoints" setup, and I have a working backend module running on AppEngine.
So far I have learned how to create an API and API methods:
#Api(
name = "myApi",
version = "v1",
namespace = #ApiNamespace(
ownerDomain = "backend.myapplication.Mike.example.com",
ownerName = "backend.myapplication.Mike.example.com",
packagePath = ""
)
)
public class MyEndpoint {
/**
* A simple endpoint method that takes a name and says Hi back
*/
#ApiMethod(name = "sayHi")
public MyBean sayHi(#Named("name") String name) {
MyBean response = new MyBean();
response.setData("Hi, " + name);
return response;
}
}
So this shows I have successfully created a Cloud Endpoints / backend that goes with an App Engine project.
So, what I was hoping to do with this set up is to create an API method to save data to the cloud server, and then have other users on other devices to be able to retrieve that data.
If you're looking for something like "a big shared object" that lots of people can all change collaboratively, you might want to check out Firebase, which does this and keeps everyone in sync (and integrates nicely with Android and iOS).

How can I interface an Android app with SQL Server?

This is my problem:
I have to make an Android app that recover some data from an existing database situated on a Microsoft SQL Server installation on Windows Server 2003. I don't know so much about server-side programming languages, so I'm searching on the Internet, and I've found that I have to use a Web Service between my app and the ASP Classic page that connect to the database.
I also found that I have to use SOAP to send data to the server with my app, using the ksoap2 library on Android, and I found how to configure it on the client side. But I can't find how to configure the WebService and which format is better for interfacing my app and the ASP Classic page.
It's very easy, actually. All what you need is to create a webservice which will display the results in JSON format. Then you need to run an HTTP request from your Android app in order the get a JSON array containing JSON objects and then you will have your data from your database.
For example: the webservice will have a function written in ASP with a SELECT statement. Before your return the result, you need to encode it in JSON. Check out JSON Encode (MSDN).
The Android app will connect to the web service link and simply retrieve the JSON encoded data. I'll put you in the right way. You just use a snippet like this one to get data from the web service:
AsyncHttpClient client = new AsyncHttpClient();
client.get("http://yourwebservice.aspx", new AsyncHttpResponseHandler() {
#Override
public void onSuccess(String response) {
JSONArray jarr = new JSONArray(response);
for(int i = 0; i < jarr.length(); ++i) {
JSONObject jobj = jarr.getJSONObject(i);
// Do your things...
}
}
});
Create a RESTful web service in Web API or WCF. Web API will communicate with the database and your Android application with communicate with the web service.

Categories

Resources