Android Question Elevation API key [Solved]

Roger Daley

Well-Known Member
Licensed User
Longtime User
Hi All

Rephrasing of a previous question.

An API key is required to "enable" the Elevation API in Google maps.
1. The developers guide leads the user through generating the key and gives examples of using it by appending it to the URL.
E.G.
B4X:
https://maps.googleapis.com/maps/api/elevation/json?locations=39.7391536,-104.9847034|36.455556,-116.866667&key=YOUR_API_KEY
This does not work if you try to append to the URL string in B4A code.


This code works, but not if you append the key to the end of the URL string
B4X:
Sub getElevationData  '(URL As String, taskID As Int)
    HttpClient1.Initialize("HttpClient1")
    Private HttpRequest1 As HttpRequest
    URL1= "http://maps.googleapis.com/maps/api/elevation/json?locations="&ALat&","&ALng&"%7C"&BLat&","&BLng
    HttpRequest1.InitializeGet(URL1)
    HttpClient1.Execute(HttpRequest1,1)
End Sub

2. The developers guide also says not to use the key in the source code.



Now that I have generated this API key what do I do with it? Where is it used? How is it used?

Any help much appreciated.

Regards Roger
 

Roger Daley

Well-Known Member
Licensed User
Longtime User
Thanks Erel,

I have read-up and recoded as below, but the question in my first post as to where and how to use the "Elevation API key" still stands.
I have tried severl ways to append the "&key=YOUR_API_KEY" even though Google say not to include this in source code. No success.
I suspect it goes in the manifest like the Google Maps API key but have no clear idea of the details.


Regards Roger



B4X:
Sub getElevationData 
    Private Job1 As HttpJob
    Job1.Initialize("Job1", Me)
    Job1.Download("http://maps.googleapis.com/maps/api/elevation/json?locations="&ALat&","&ALng&"%7C"&BLat&","&BLng)
End Sub

Sub JobDone (Job As HttpJob)
   If Job.Success = True Then
      Select Job.JobName
         Case "Job1"
            Private returnData As String
            returnData = Job.GetString
            ABElevations(returnData)        'Extract elevations from returndata string
         'Case, other jobs
            'Other actions
      End Select
   Else
           'Alternate actions
   End If
   Job.Release
End Sub
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
I am still unable to append/use this elevation API key. All attempts end in errors, crashes or no request sent. Below are a couple of examples I have tried.
I am about ready to abandon the whole idea and take up quantum physics as an easier option.

B4X:
'    Job1.Download2("http://maps.googleapis.com/maps/api/elevation/locations",Array As String("ALat","ALng","AIzaSyB3_UZ-lOii2cDorBMzsJ_hC6FtfJEXXXX"))   
'    Job2.Download2("http://maps.googleapis.com/maps/api/elevation/locations",Array As String("BLat","BLng","AIzaSyB3_UZ-lOii2cDorBMzsJ_hC6FtfJEXXXX"))
   
'    Job1.Download("https://maps.googleapis.com/maps/api/elevation/json?locations="&ALat&","&ALng&"key=AIzaSyB3_UZ-lOii2cDorBMzsJ_hC6FtfJEXXXX")
'    Job2.Download("https://maps.googleapis.com/maps/api/elevation/json?locations="&BLat&","&BLng&"key=AIzaSyB3_UZ-lOii2cDorBMzsJ_hC6FtfJEXXXX")

New question. Has anybody actually used an elevation API key in B4A?

Regards Roger
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
your code does not llook ok for me.


i would try it this way

B4X:
    Private Job1 As HttpJob
  Job1.Initialize("Job1", Me)
  Job1.Download2("http://maps.googleapis.com/maps/api/elevation/json",Array As String("locations",$"${ALat},${ALng}|${BLat},${BLng}"$,"key","yourkeyhere"))
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
Thanks DonManfred,

Nice try but still no go.

First thing, an error message that said https was required, despite what is in the user guide. No problems, I changed that small issue.

Next this error message:
{
"error_message" : "This IP, site or mobile application is not authorized to use this API key. Request received from IP address 1.136.96.139, with empty referer",
"results" : [],
"status" : "REQUEST_DENIED"
}

I have been back to the credentials in the API console and the key is correct, the application is correct. :(


Thanks again for your help.
It is time to move away from the keyboard.

Regards Roger
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Nice try but still no go.
Based on the results you get (i´ll note that it was no crash)
This IP, site or mobile application is not authorized to use this API key. Request received from IP address 1.136.96.139, with empty referer
I guess we need to find out more on this... Here seems to be the problem. It must be a setting in the key-definition...

I have not used this api so far; just wanted to help to setup the right httpjob-parameters. And i guess it does helped based on the result you got.

I´ll try to setup a key for me and give it a try

It is time to move away from the keyboard.
Don´t give up..
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Based on Elevation API Setup you need to setup a SERVERKEY.

To use a serverkey the api must be called from a server (your website for example) and the ip of the defined website must be static.

In my understanding you could try the following:
- Setup a serverkey and define the ip of your website which should be able to run php.
- Setup a small phpscript which does call the google-api.
The php script will be called from your app with okhttputils. The php does the request at google and returns the result then to your app.

My findings:

1. The only way i got it running using a php-script in the middle was this.
I defined a new key in the console and give the key unlimited restrictions.
gelevation0074.png


gelevation0075.png




gelevation0076.png


CLOSE the dialog without setting any restrictions...


Basically i used this php-script on my server

PHP:
<?php
if (isset($_REQUEST['alat'])){$alat=$_REQUEST['alat'];} else {$alat=0;}
if (isset($_REQUEST['alng'])){$alng=$_REQUEST['alng'];} else {$alng=0;}
if (isset($_REQUEST['blat'])){$blat=$_REQUEST['blat'];} else {$blat=0;}
if (isset($_REQUEST['blng'])){$blng=$_REQUEST['blng'];} else {$blng=0;}

echo file_get_contents("https://maps.googleapis.com/maps/api/elevation/json?locations=".$alat.",".$alng."|".$blat.",".$blng."&key=your key here");
?>

B4X:
Sub getElevationData
  Dim ALat, ALng, BLat, BLng As Double
  
    ALat = 39.7391536
    ALng = -104.9847034
    BLat = 36.455556
    BLng = -116.866667
  
    Private Job1 As HttpJob
  Job1.Initialize("Job1", Me)
  Job1.Download2("http://basic4android.de/elevation.php",Array As String("alat",ALat,"alng",ALng,"blat",BLat,"blng",BLng))
End Sub

Sub JobDone(Job As HttpJob)
  If Job.Success = True Then
        Log($"JobDone(${Job.success} -> ${Job.GetString})"$)          
        If Job.JobName = "Job1" Then
            Dim s As String = Job.GetString
        End If
    Else
        Log($"JobDone(${Job.success} -> ${Job.ErrorMessage})"$)          
    End If
    Job.Release
End Sub

** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Service (httputils2service) Create **
** Service (httputils2service) Start **
JobDone(true -> {
"results" : [
{
"elevation" : 1608.637939453125,
"location" : {
"lat" : 39.7391536,
"lng" : -104.9847034
},
"resolution" : 4.771975994110107
},
{
"elevation" : -50.78903961181641,
"location" : {
"lat" : 36.455556,
"lng" : -116.866667
},
"resolution" : 19.08790397644043
}
],
"status" : "OK"
}
)
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **

NOTE that you should not use such a key in your android app due to security-risks! Use a php script in the middle where the key is save.
Note to secure your php. Means; you shiuld insert restrictions in your php to ensure no one is using your php without xour acknowledge
 
Last edited:
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
Thanks DonManfred,

You have really gone the extra mile to give this answer. Unfortunately my web site a free hosted web site [Simplesite] which I don't think allows php scripts. Additionally my knowledge of php is limited to it's spelling.

Your post is going to be helpful to others who want to get elevation data, to me the short answer is that it is not worth this much effort to add a minor feature to an existing App.

Once again many thanks for such an effort to provide the answer.

Regards Roger
 
Upvote 0
Top