Spanish b4j geocoder.

mvera

Active Member
Licensed User
Longtime User
hola necesito al introducir una calle y numero , sacar la latitud y longitud y llegue a esto pero no comprendo el error del log.

B4X:
Sub Button1_MouseClicked (EventData As MouseEvent)
   
    direccion("jose bordes","1000")
   
End Sub


Sub direccion(calle As String, numero As String)
    Dim Key As String = "1238"
    Dim postString As String
 
       
       
    postString="address="&numero&"+"&calle&"&Key="&Key
   

    Log("https://maps.googleapis.com/maps/api/geocode/json?"&postString)
   
       
    dir.PostString("https://maps.googleapis.com/maps/api/geocode/json?",postString)
    'puntos.PostString(miniruta,"lat="&latitud1&"&lo="&longitud1&"")
    'https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&Key=YOUR_API_KEY
End Sub


Sub JobDone(Job As HttpJob)
Try



    Dim m As Map
    Dim respuesta,id,query,comentario,fecha,hora,iden_reporte,iden_reporte1,query1 As String
   

   
   

   
    m.Initialize()
   
    Log("entramos....."&Job.Success)
    Log("mesaje "&Job.ErrorMessage)
   
   
    If Job.Success Then


...........................
..........................


1.hago click y le mando la direccion
2.entra al Sub JobDone(Job As HttpJob)
3. devuelve este error.

https://maps.googleapis.com/maps/api/geocode/json?address=1000+jose bordes&Key=123
{
"error_message" : "Invalid request. Missing the 'address', 'bounds', 'components', 'latlng' or 'place_id' parameter.",
"results" : [],
"status" : "INVALID_REQUEST"
}
entramos.....false
mesaje Bad Request

4.Pero si copio y pego la url directo en el navegador , me devuelve los valores correctamente.


Espero me puedan orientar.
gracias.
 

Matias Ferrer

Member
Licensed User
Longtime User
Estimado,
Creo que tiene un problema de codificación. Deberías enviar los datos de la calle, y demás utilizando la función "EncodeUrl" de la librería StringUtils

Sería algo así: address=" & su.EncodeUrl(calle,"UTF8")


Todo lo que envíes por URL, POST o GET debes codificarlo.

Saludos,
 

mvera

Active Member
Licensed User
Longtime User
lo intente de estos dos formas.

Dim su As StringUtils

1.
postString="address="&su.EncodeUrl(numero,"UTF8")&"+"&su.EncodeUrl(calle,"UTF8")&"&Key="&Key

2.
postString="address="&su.EncodeUrl($"${numero}"$,"UTF8")&"+"&su.EncodeUrl($"${calle}"$,"UTF8")&"&Key="&Key



pero el resultado siempre fue el mismo.

https://maps.googleapis.com/maps/api/geocode/json?address=100+jose+bordes&Key=123
{
"error_message" : "Invalid request. Missing the 'address', 'bounds', 'components', 'latlng' or 'place_id' parameter.",
"results" : [],
"status" : "INVALID_REQUEST"
}
entramos.....false
mesaje Bad Request


gracias por la orientacion.
 

cuellar

Member
Licensed User
Longtime User
Prueba con:

B4X:
job.Download("https://maps.googleapis.com/maps/api/geocode/json?address=Carrer d'Àlaba, 12, 08005 Barcelona, España")
 

mvera

Active Member
Licensed User
Longtime User
hola lo intente asi:

B4X:
https://maps.googleapis.com/maps/api/geocode/json?address=Carrer+d%27%C3%80laba%2C+12%2C+08005+Barcelona%2C+Espa%C3%B1a+jose+bordes&Key=123
{
   "error_message" : "Invalid request. Missing the 'address', 'bounds', 'components', 'latlng' or 'place_id' parameter.",
   "results" : [],
   "status" : "INVALID_REQUEST"
}
entramos.....false
mesaje Bad Request

otra opcion


B4X:
   postString="&Key="&Key
   dir.PostString("https://maps.googleapis.com/maps/api/geocode/json?address=Carrer d'Àlaba, 12, 08005 Barcelona, España",postString)




main._direccion (java line: 241)
java.lang.IllegalArgumentException: Illegal character in query at index 64: https://maps.googleapis.com/maps/api/geocode/json?address=Carrer d'Àlaba, 12, 08005 Barcelona, España
    at java.net.URI.create(URI.java:852)
    at org.apache.http.client.methods.HttpPost.<init>(HttpPost.java:76)
    at anywheresoftware.b4a.http.HttpClientWrapper$HttpUriRequestWrapper.InitializePost2(HttpClientWrapper.java:395)
    at b4j.example.httpjob._postbytes(httpjob.java:213)
    at b4j.example.httpjob._poststring(httpjob.java:261)
    at b4j.google.map.main._direccion(main.java:241)
    at b4j.google.map.main._button1_mouseclicked(main.java:219)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:84)
    at anywheresoftware.b4j.objects.NodeWrapper$1.handle(NodeWrapper.java:89)
    at anywheresoftware.b4j.objects.NodeWrapper$1.handle(NodeWrapper.java:1)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3470)
    at javafx.scene.Scene$ClickGenerator.access$8100(Scene.java:3398)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3766)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:380)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:416)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.URISyntaxException: Illegal character in query at index 64: https://maps.googleapis.com/maps/api/geocode/json?address=Carrer d'Àlaba, 12, 08005 Barcelona, España
    at java.net.URI$Parser.fail(URI.java:2848)
    at java.net.URI$Parser.checkChars(URI.java:3021)
    at java.net.URI$Parser.parseHierarchical(URI.java:3111)
    at java.net.URI$Parser.parse(URI.java:3053)
    at java.net.URI.<init>(URI.java:588)
    at java.net.URI.create(URI.java:850)
    ... 44 more

tambien lo probe directo por url y funciona correcto.
 

Matias Ferrer

Member
Licensed User
Longtime User
Estimado,
He probado tus datos con esta rutina y funciona en mi B4J sin problemas.

B4X:
Dim calle As String = "josé bordes"
    Dim numero As String = "1000"
   
    Dim postString As String
    postString = "address=" & su.EncodeUrl($"${calle} ${numero}, CHILE"$, "UTF8") & "&sensor=false"
    Log (postString)
   
    FileHttp.PostString("https://maps.googleapis.com/maps/api/geocode/json?" & postString, "")


Como resultado me da esta info. Sin errores

B4X:
Resultado: {
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "José Bordes",
               "short_name" : "José Bordes",
               "types" : [ "route" ]
            },
            {
               "long_name" : "La Reina",
               "short_name" : "La Reina",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "La Reina",
               "short_name" : "La Reina",
               "types" : [ "administrative_area_level_3", "political" ]
            },
            {
               "long_name" : "Santiago",
               "short_name" : "Santiago",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Región Metropolitana",
               "short_name" : "Región Metropolitana",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Chile",
               "short_name" : "CL",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "José Bordes, La Reina, Región Metropolitana, Chile",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : -33.4533972,
                  "lng" : -70.5621652
               },
               "southwest" : {
                  "lat" : -33.4539482,
                  "lng" : -70.5659095
               }
            },
            "location" : {
               "lat" : -33.453549,
               "lng" : -70.5634142
            },
            "location_type" : "GEOMETRIC_CENTER",
            "viewport" : {
               "northeast" : {
                  "lat" : -33.4523237197085,
                  "lng" : -70.5621652
               },
               "southwest" : {
                  "lat" : -33.4550216802915,
                  "lng" : -70.5659095
               }
            }
         },
         "partial_match" : true,
         "place_id" : "ChIJ_7Iz2TfOYpYROW2pothpPC0",
         "types" : [ "route" ]
      }
   ],
   "status" : "OK"
}


Pruebalo y me cuentas.!

Saludos desde Argentina.
 

mvera

Active Member
Licensed User
Longtime User
mmmm ok voy a verificar en que me equivoco , muchas gracias por tu tiempo.
 

mvera

Active Member
Licensed User
Longtime User
era el key que no funcionaba correctamente al sacarlo me funciono perfecto.
muchas gracias a todos.
 
Top