Android Question Elevations from Google Maps [Solved]

Roger Daley

Well-Known Member
Licensed User
Longtime User

Mark Read

Well-Known Member
Licensed User
Longtime User
A quick look at this page seems to show what you need. It states you need an APi key but I tried the example without the key and it works.

https://maps.googleapis.com/maps/ap...39.7391536,-104.9847034|36.455556,-116.866667

returns:

{
"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"
}

Maybe it helps you.
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
Hi All,

Thanks to mark35at I am almost there.
Small problem with the url string, I keep getting the elevation for Lat 0/ Lng 0. My code is below.

Globals

B4X:
Private  ALat, ALng, BLat, BLng   As Double
Private URL1 As String  = "http://maps.googleapis.com/maps/api/elevation/json?locations="&ALat&","&ALng&"%7C"&BLat&","&BLng


Sub getElevationData  '(URL As String, taskID As Int)
    HttpClient1.Initialize("HttpClient1")
    Private HttpRequest1 As HttpRequest
    HttpRequest1.InitializeGet(URL1)
    HttpClient1.Execute(HttpRequest1,1)
End Sub

Sub HttpClient1_ResponseSuccess (ResponseData As HttpResponse, TaskId As Int)
    my_buffer.InitializeToBytesArray(5000)
    'Do Something with the data that is returned.
    ResponseData.GetAsynchronously("ServerResponse",my_buffer, True, 1)
End Sub

Sub HttpClient1_ResponseError (ResponseData As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
    Log("error: " & ResponseData & " " & StatusCode)
    If ResponseData <> Null Then
        Log(ResponseData.GetString("UTF8"))
        ResponseData.Release
    End If
End Sub

Sub ServerResponse_StreamFinish (Success As Boolean, TaskId As Int)
    If Success Then
        Private return_buffer () As Byte
           return_buffer = my_buffer.ToBytesArray
       Private returnData As String = BytesToString(return_buffer, 0, return_buffer.Length, "UTF8")
  
'********************************************
    Log("ALat = "&ALat)
    Log("ALng = "&ALng)
    Log(returnData)
'********************************************
        StopService("")       
   End If
     
End Sub

Below is the result that is logged.
** Activity (main) Resume **
Installing file.
** Activity (main) Pause, UserClosed = false **
PackageAdded: package:horsetrailer.B4A.AntennaBearingTool
** Activity (main) Create, isFirst = true **
~w:1004,main,90
** Activity (main) Resume **
ALat = -37.8122
ALng = 144.95919227600098
{
"results" : [
{
"elevation" : -3492,
"location" : {
"lat" : 0,
"lng" : 0
},
"resolution" : 610.8129272460938
},
{
"elevation" : -3492,
"location" : {
"lat" : 0,
"lng" : 0
},
"resolution" : 610.8129272460938
}
],
"status" : "OK"
}

Obviously I am doing something wrong in the URL1 string but have run out of ideas.
Any help greatly appreciated

Regards Roger
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
New Log below. Somehow the Lat/Long is being lost.

Regards Roger


Installing file.
PackageAdded: package:horsetrailer.B4A.AntennaBearingTool
** Activity (main) Create, isFirst = true **
~w:1004,main,90
** Activity (main) Resume **
ALat = -37.8122
ALng = 144.95919227600098
BLat = -37.81175
BLng = 144.96162
URL1 = http://maps.googleapis.com/maps/api/elevation/json?locations=0,0|0,0
{
"results" : [
{
"elevation" : -3492,
"location" : {
"lat" : 0,
"lng" : 0
},
"resolution" : 610.8129272460938
},
{
"elevation" : -3492,
"location" : {
"lat" : 0,
"lng" : 0
},
"resolution" : 610.8129272460938
}
],
"status" : "OK"
}
** Activity (main) Pause, UserClosed = false **
No wakelock.
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
just a thought, try this:

B4X:
Private URL1 AsString

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

It might be that you are declaring the URL1 when BLat and BLng have no value. Where do you pass the Lat and Lng values to URL1?
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
You are welcome. Dont't forget to mark the thread solved and maybe a like too. :D
 
Upvote 0
Top