Android Question Google Geocoding Problem (Geocoder library)

ciginfo

Well-Known Member
Licensed User
Longtime User
Hello,
With Geocoder library, I can't get "Sub LatLonToPlace (lat As Double, lon As Double)" to run
It seems that the part of the code( between green lines x) is not considered
B4X:
Sub LatLonToPlace(lat As Double, lon As Double) 'As ResumableSub
    
    Dim API_KEY As String = "AIzaSyDoxxxxxxxxxxxxxrvu5oPI"
    Dim res As String
    Dim GetAddressJob As HttpJob
    GetAddressJob.Initialize("GetAddress", Me)
    GetAddressJob.Download2("https://maps.googleapis.com/maps/api/geocode/json", Array As String("latlng", lat & "," & lon,"key",API_KEY))
    Wait For (GetAddressJob) JobDone(GetAddressJob As HttpJob)
    If GetAddressJob.Success Then
        Dim jp As JSONParser
        jp.Initialize(GetAddressJob.GetString)
        Dim m As Map = jp.NextObject
        
        'xxxxxxxxxxxxxx No runs beneath xxxxxxxxxxxxxxxxx
        If m.Get("status") = "OK" Then
            Dim results As List = m.Get("results")
            If results.Size > 0 Then
                Dim first As Map = results.Get(0)
                res = first.Get("formatted_address")
                Log(res)
                'res contain address string
            End If
        End If
        'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx   
        
    End If
    GetAddressJob.Release
End Sub
Which libraries are needed ?
Have I forgotten something?
Thank you
 

drgottjr

Expert
Licensed User
Longtime User
works here. side comment, if you don't mind: you have a habit of only testing for 1 of several outcomes. it's a bad idea.
 
Upvote 0

ciginfo

Well-Known Member
Licensed User
Longtime User
I don't understand you, and you don't answer my question about which libraries should be used
 
Upvote 0

AnandGupta

Expert
Licensed User
Longtime User
I don't understand you, and you don't answer my question about which libraries should be used
IMHO drgottjr means , you are only checking "If m.Get("status") = "OK" Then"

So check "Else"

Regards,

Anand
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
your code works on my device. period. there are no missing libraries.

in a separate matter, it is not enough to check for 1 possibility when there are several possibilities. when you only check for success, you are ignoring why there might be a reason for failure. in other words:
B4X:
if success then
 .....
else
 .....    ' find out why there was a failure
 endif

i am sorry you do not understand.
 
Upvote 0

ciginfo

Well-Known Member
Licensed User
Longtime User
your code works on my device. period. there are no missing libraries.

in a separate matter, it is not enough to check for 1 possibility when there are several possibilities. when you only check for success, you are ignoring why there might be a reason for failure. in other words:
B4X:
if success then
.....
else
.....    ' find out why there was a failure
endif

i am sorry you do not understand.
Désolé, c' est moi qui parle mal et comprends mal l'anglais
Google translate is not 100 % efficient
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
Désolé, c' est moi qui parle mal et comprends mal l'anglais
Google translate is not 100 % efficient
ecoute, entre 2 (ou plus) possibilities, il faut toujours les regarder toutes. c'est a dire:
B4X:
si cette possibilite est vraie alors
   je fais ce-ci
sinon
   je fais ce-la

toi, tu ne regardes la premiere possibilite en laissant je ne sais pas ou la deuxieme. si ce que tu fais n'est pas un success, if faut 1) savoir pourquoi et 2) faire quelque chose dans ce cas. l'echec est aussi important que le success. tu cours le risque que l'app fasse crash en l'ignorant. c'est ca ce que je vouais dire. je te prie de m'excuser la manque d'accents.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
Hi,
In fact I am working on 2 methods to get an address based on GPS coordinates. 2 methods in the same example attached.
The 1st (MEMO1) uses "Geolocation". I have a problem I obtain adresse but the GPS coordinates are not refreshed when I leave or when opening app.
The 2nd (MEMO2) uses "Geocoder". I get the GPS coordinates well at each change of location, but I do not get the address.
Thank you and excuse my bad English.

tu as revele ta cle d'access dans ton fichier. pas bon. remplace le fichier apres avoir efface ta cle
 
Upvote 0

AnandGupta

Expert
Licensed User
Longtime User
Thank you and excuse my bad English.
ciginfo, I suggest you to send message in French Forum. This way you will not be required to translate.
ciginfo, je vous suggère d'envoyer un message sur le forum français. De cette façon, vous ne serez pas obligé de traduire.

Regards,

Anand
 
Upvote 0

ciginfo

Well-Known Member
Licensed User
Longtime User
ciginfo, I suggest you to send message in French Forum. This way you will not be required to translate.
ciginfo, je vous suggère d'envoyer un message sur le forum français. De cette façon, vous ne serez pas obligé de traduire.

Regards,

Anand
Unfortunately too few members consult the French forum, it is ineffective.
 
Upvote 0

ciginfo

Well-Known Member
Licensed User
Longtime User
tu as revele ta cle d'access dans ton fichier. pas bon. remplace le fichier apres avoir efface ta cle
OK, thank you,, voilà qui est fait.
Please look at the example
 

Attachments

  • Localiser1.zip
    16.2 KB · Views: 278
Upvote 0

AnandGupta

Expert
Licensed User
Longtime User
Unfortunately too few members consult the French forum, it is ineffective.
Yes, non English Forum are less crowded, but still I will recommend,
  1. Post in French Forum first
  2. Wait for reply
  3. If not satisfied, post in English Forum and mention the link of French Forum with message that you are French speaking.
  4. Members will be more co-operative and help you with translated message itself.
  5. Many French speaking members who know English well are here and will gladly show you the solution.
  6. This will save you from translation and help other members understanding your problem.
Regards,

Anand
 
Upvote 0

yfleury

Active Member
Licensed User
Longtime User
Salut @ciginfo J'ai regarder ton projet
Dans le module Starter il n'y a aucune référence au GPS.
Commence par ce tutoriel
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
il ne faut pas activer le gps! seulement donner le permis d'acces. la librairie s'occupe du gps. il y a beaucoup d'erreurs dan le projet. d'ailleurs - et surtout - on ne sait pas toujours si @ciginfo a resolu le 1er probleme souleve dans ce fils-ci (tout en haut). il a tout a coup sauter a un autre theme. la librairie geolocation, elle, dirige le gps. c'est ca - entre autres - qu'il faut comprendre.
 
Last edited:
Upvote 0

ciginfo

Well-Known Member
Licensed User
Longtime User
OK. but to keep it simple, let's just consider my first problem. I can easily get the latitude and longitude but I can't get the address based on the GPS coordinates.
If m.Get("status") = "OK" This line poses a problem for me, it seems to be missing an element in this sub recovered in a tutorial.
B4X:
Dim API_KEY As String = "AIzaSxxxxxxxxxxxxxxxxxxxxxxxvu5oPI"
    Dim res As String
    Dim GetAddressJob As HttpJob
    GetAddressJob.Initialize("GetAddress", Me)
    GetAddressJob.Download2("https://maps.googleapis.com/maps/api/geocode/json", Array As String("latlng", lat & "," & lon,"key",API_KEY))
    Wait For (GetAddressJob) JobDone(GetAddressJob As HttpJob)
    If GetAddressJob.Success Then
        Dim jp As JSONParser
        jp.Initialize(GetAddressJob.GetString)
        Dim m As Map = jp.NextObject
        If m.Get("status") = "OK" Then 'Here the problem
            Dim results As List = m.Get("results")
                If results.Size > 0 Then
                 Dim first As Map = results.Get(0)
                 res = first.Get("formatted_address")
                 Log(res)
                'res contain address string
                 LblAdresse2.Text = res
                Else
                 Log("Result out")
                End If
        End If
    End If
    
    GetAddressJob.Release
End Sub
 

Attachments

  • LocaliseAdresse.zip
    10.1 KB · Views: 275
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
B4X:
Dim API_KEY As String = "AIzaSxxxxxxxxxxxxxxxxxxxxxxxvu5oPI"
    Dim res As String
    Dim GetAddressJob As HttpJob
    GetAddressJob.Initialize("GetAddress", Me)
    GetAddressJob.Download2("https://maps.googleapis.com/maps/api/geocode/json", Array As String("latlng", lat & "," & lon,"key",API_KEY))
    Wait For (GetAddressJob) JobDone(GetAddressJob As HttpJob)
    If GetAddressJob.Success Then
        Dim jp As JSONParser
        jp.Initialize(GetAddressJob.GetString)
        Dim m As Map = jp.NextObject
        If m.Get("status") = "OK" Then 'Here the problem
            Dim results As List = m.Get("results")
                If results.Size > 0 Then
                 Dim first As Map = results.Get(0)
                 res = first.Get("formatted_address")
                 Log(res)
                'res contain address string
                 LblAdresse2.Text = res
                Else
                 Log("Result out")
                End If
        End If
    End If
    
    GetAddressJob.Release
End Sub

this is the same code from post #1. i have already told you this code is ok. it works on my device. there are 2 small problems, but the code is ok.
je t'ai deja dit que le code marche sur mon mobile. c'est le meme code que dans ton premier post (auquel j'ai repondu en disant que ca marchait). il y a 2 petits problemes, mais en principe (tant qu'en practique) ca marche. je vois bien mon adresse avec ton code et en fournissant les coordonnees a google geocoder (avec ma cle, evidemment). puisque tu sautes d'une chose a autre, je ne sais plus si on parle toujours du meme probleme.
if there is an error, you need to check for it, as i explained before: if success ... else ...
s'il y a un erreur, il faut faire comme j'ai deja explique: si success ... sinon ...
 
Upvote 0

ciginfo

Well-Known Member
Licensed User
Longtime User
Thank you very much for wasting so much time with me. Glad to know that my code works fine on your device but still not at home. The GPS coordinates are well collected but still not the address. If I test with If Succes else at 3 levels as in the code below I get "Error2", hence the error in If m.Get (" status ") =" OK "Then
Note that my API_KEY works fine in others applications
Really sorry for the inconvenience
B4X:
If GetAddressJob.Success Then
        Dim jp As JSONParser
        jp.Initialize(GetAddressJob.GetString)
        Dim m As Map = jp.NextObject
       [B] [/B]If m.Get("status") = "OK" Then 
            Dim results As List = m.Get("results")
                If results.Size > 0 Then
                 Dim first As Map = results.Get(0)
                 res = first.Get("formatted_address")
                 Log(res)
                'res contain address string
                 LblAdresse2.Text = res
                 Else
                     Log("Error3")
                End If
        Else
          [B]  [/B]Log("Error2")
        End If
    Else
        Log("Error1")
    End If
 

Attachments

  • error.png
    error.png
    4.1 KB · Views: 245
Upvote 0

AnandGupta

Expert
Licensed User
Longtime User
Please log( m.Get("status") ) and check its value.
Veuillez vous connecter (m.Get ("status")) et vérifier sa valeur.

(my translation looks like not correct to me, so read it correctly from English)
(ma traduction ne me semble pas correcte, alors lisez-la correctement de l'anglais)

Regards,

Anand
 
Upvote 0

ciginfo

Well-Known Member
Licensed User
Longtime User
Please log( m.Get("status") ) and check its value.
Veuillez vous connecter (m.Get ("status")) et vérifier sa valeur.

(my translation looks like not correct to me, so read it correctly from English)
(ma traduction ne me semble pas correcte, alors lisez-la correctement de l'anglais)

Regards,

Anand
When I Check log( m.Get("status") it indicates REQUEST_DENIED
 
Upvote 0
Top