Android Code Snippet Sample using GPS and GeoAddress

This sample its based on the thread https://www.b4x.com/android/forum/threads/background-location-tracking.99873/ of @Erel.

1) In an Activity, I start GPS and get Latitude & Longitude
2) then, pass this values to GeoAddress2,
3) and then obtain, Address.

It runs perfecto on a HAUWEI P10.

I recommend that test it first in your device and look if it can help you what are you intend to develop.

This sample gives you an idea how GPS Library works!.
 

Attachments

  • MyGeoAddress.zip
    10.5 KB · Views: 421

alimanam3386

Active Member
Licensed User
Longtime User
I got error ( Samsung A20 , API 29 )

B4X:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Error occurred on line: 64 (Main)
java.lang.Exception: Sub geolocation_location signature does not match expected signature.
public static anywheresoftware.b4a.pc.RemoteObject b4a.example.geo.main_subs_0._geolocation_location(anywheresoftware.b4a.pc.RemoteObject,anywheresoftware.b4a.pc.RemoteObject,anywheresoftware.b4a.pc.RemoteObject,anywheresoftware.b4a.pc.RemoteObject,anywheresoftware.b4a.pc.RemoteObject) throws java.lang.Exception
class anywheresoftware.b4a.pc.RemoteObject, class anywheresoftware.b4a.pc.RemoteObject, class anywheresoftware.b4a.pc.RemoteObject, class anywheresoftware.b4a.pc.RemoteObject,
 

Attachments

  • 23.jpg
    23.jpg
    17.3 KB · Views: 254

joaquinortiz

Active Member
Licensed User
Longtime User
I got error ( Samsung A20 , API 29 )

B4X:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Error occurred on line: 64 (Main)
java.lang.Exception: Sub geolocation_location signature does not match expected signature.
public static anywheresoftware.b4a.pc.RemoteObject b4a.example.geo.main_subs_0._geolocation_location(anywheresoftware.b4a.pc.RemoteObject,anywheresoftware.b4a.pc.RemoteObject,anywheresoftware.b4a.pc.RemoteObject,anywheresoftware.b4a.pc.RemoteObject,anywheresoftware.b4a.pc.RemoteObject) throws java.lang.Exception
class anywheresoftware.b4a.pc.RemoteObject, class anywheresoftware.b4a.pc.RemoteObject, class anywheresoftware.b4a.pc.RemoteObject, class anywheresoftware.b4a.pc.RemoteObject,

When I posted, I created with this library versions!.

1593392873126.png


1593392957598.png
 

drgottjr

Expert
Licensed User
Longtime User
apologies. i don't use that library, but i was familiar with the issue. the library's author - normally very reliable - said in response to the posts that he tried it, and that it worked. i assumed it was a closed issue, since no one continued the thread. i tried your code and his example; same signature mismatch error. in the library source, i see what i would say was a signature issue, but i've never tried to alter a wrap (even if only to add some log entries) and re-wrap.

a few hours later: i was able to re-wrap the library. i changed the part that i thought caused the crash. it no longer crashes. but there is another issue: the library code ends up in a spot where it's not normally supposed to end up (ending up there is where the signature mismatch is encountered). i can correct the signature mismatch, but ending up in that spot is due to an internal issue. i think google has changed something related to what it calls the location provider. i know my gps works fine as i use it daily. the library depends on what it thinks is the location provider.

so: 1) i don't want to put something out there without the permission of the original author and 2) if he is still interested in maintaining the library and wants me to tell him what i found, i'd be happy to show him.
 
Last edited:

Jorge M A

Well-Known Member
Licensed User
I have version 1.11 of GeoLocation lib

Please double check the version you have.
AS well said by @drgottjr
the library's author - normally very reliable
@DonManfred recognized
i checked the code. The definition of the sub is not correct. V1.11 fixes this issue.
Check the second file in the attachments there:

EDIT: The example is workin fine here for me.
 

drgottjr

Expert
Licensed User
Longtime User
i'm not going to force the issue. the mismatch is in v1.11. and that's not the only issue. look at the code. plus what the auther said was then. we are now now.
 

Jorge M A

Well-Known Member
Licensed User
i'm not going to force the issue.
You're absolutely right and I agree.
However this thread is about a specific example, it is not about the library. With the versions mentioned the example works fine, at least for me. For the rest, I don't use it either.
Greetings.
 

DonManfred

Expert
Licensed User
Longtime User
so: 1) i don't want to put something out there without the permission of the original author and 2) if he is still interested in maintaining the library and wants me to tell him what i found, i'd be happy to show him.
I can provide the java source if you want. You may rigth with the assumption google has changed something.
I just do not know.

Java:
package de.donmanfred;

import java.io.IOException;
import java.util.List;
import java.util.Locale;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.Author;
import anywheresoftware.b4a.BA.Events;
import anywheresoftware.b4a.BA.Permissions;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;

@Version(1.11f)
@ShortName("GeoLocator")
@Author("DonManfred")
//@ActivityObject

@Permissions(values={"android.permission.INTERNET", "android.permission.ACCESS_NETWORK_STATE","android.permission.ACCESS_FINE_LOCATION"})
@Events(values={"Location(success As Boolean, Lattitude As Double, Longitude As Double, fromMockProvider As Boolean, ismocklocation As Boolean)"})
//@DependsOn(values={"android-viewbadger"})
/**
 * com.android.support:support-v4
 * com.android.support:appcompat-v7
 * com.android.support:cardview-v7
 * com.android.support:gridlayout-v7
 * com.android.support:mediarouter-v7
 * com.android.support:palette-v7
 * com.android.support:recyclerview-v
 * com.android.support:preference-v7
 * com.android.support:support-v13
 */

public class GeoLocatorwrapper {
    private BA ba;
    private String eventName;
    private static double lattitude, longitude;
    private static boolean fromMockProvider;
    private String address, city, state, country, postalCode, knownName;

    /**
     *  <link>...|https://www.b4x.com</link>
     */
    public static void LIBRARY_DOC() {
    }

    public void Initialize(BA ba, String EventName) {
        this.eventName = EventName.toLowerCase(BA.cul);
        this.ba = ba;
        fromMockProvider = false;
        GetLocation(ba);
    }
    public void GetLocation(BA ba) {
        LocationManager locationManager = (LocationManager) ba.context.getSystemService(ba.context.LOCATION_SERVICE);

        if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {

            // showSettingsAlert();
            //Toast.makeText(context, "Permission Denied", Toast.LENGTH_SHORT).show();
        String info = "Permission Denied";
            ba.raiseEventFromDifferentThread(this, null, 0, eventName + "_location", true, new Object[] {Boolean.valueOf(false),null,null,info});

        } else if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            

            Location location1 = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            Location location2 = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
            if (location != null) {
                double latti = location.getLatitude();
                double longi = location.getLongitude();
                lattitude = latti;
                longitude = longi;
                fromMockProvider = location.isFromMockProvider();
            String info = "NETWORK_PROVIDER";
          ba.raiseEventFromDifferentThread(this, null, 0, eventName + "_location", true, new Object[] {Boolean.valueOf(true),lattitude,longitude, info, fromMockProvider});
            } else if (location1 != null) {
                double latti = location1.getLatitude();
                double longi = location1.getLongitude();
                lattitude = latti;
                longitude = longi;
            String info = "GPS_PROVIDER";
                fromMockProvider = location1.isFromMockProvider();
          ba.raiseEventFromDifferentThread(this, null, 0, eventName + "_location", true, new Object[] {Boolean.valueOf(true),lattitude,longitude, info, fromMockProvider});
            } else if (location2 != null) {
                double latti = location2.getLatitude();
                double longi = location2.getLongitude();
                lattitude = latti;
                longitude = longi;
                fromMockProvider = location2.isFromMockProvider();
            String info = "PASSIVE_PROVIDER";
          ba.raiseEventFromDifferentThread(this, null, 0, eventName + "_location", true, new Object[] {Boolean.valueOf(true),lattitude,longitude, info, fromMockProvider});
            } else {
                //Toast.makeText(context, "Unble to Trace your location", Toast.LENGTH_SHORT).show();
            double value = 0;
                fromMockProvider = false;
                String info = "Unble to Trace your location";
          ba.raiseEventFromDifferentThread(this, null, 0, eventName + "_location", true, new Object[] {Boolean.valueOf(false),value,value, info, fromMockProvider});
            }
        }
    }
    public double getLattitude() {
        return lattitude;
    }
    public double getLongitude() {
        return longitude;
    }
    /*
     * Make sure to call this method only
     * after you got a valid Location.
     */
    public void geoAddress(BA ba) throws IOException {
        Geocoder geocoder;
        List<Address> addresses;
        geocoder = new Geocoder(ba.context, Locale.getDefault());
        addresses = geocoder.getFromLocation(lattitude, longitude, 1);
        BA.Log("addresses:"+addresses.size());
        if(addresses.size() > -1){
            address = addresses.get(0).getAddressLine(0);
            city = addresses.get(0).getLocality();
            state = addresses.get(0).getAdminArea();
            country = addresses.get(0).getCountryName();
            postalCode = addresses.get(0).getPostalCode();
            knownName = addresses.get(0).getFeatureName();
        } else {
            city = "";
            state = "";
            country = "";
            postalCode = "";
            knownName = "";
        }
    }
    public void geoAddress2(BA ba, double lattitude, double longitude) throws IOException {
        Geocoder geocoder;
        List<Address> addresses;
        geocoder = new Geocoder(ba.context, Locale.getDefault());
        addresses = geocoder.getFromLocation(lattitude, longitude, 1);
        if(addresses.size() > -1){
            address = addresses.get(0).getAddressLine(0);
            city = addresses.get(0).getLocality();
            state = addresses.get(0).getAdminArea();
            country = addresses.get(0).getCountryName();
            postalCode = addresses.get(0).getPostalCode();
            knownName = addresses.get(0).getFeatureName();
        } else {
            city = "";
            state = "";
            country = "";
            postalCode = "";
            knownName = "";
        }
    }
    public String getAddress() {
        return address;
    }
    public String getCity() {
        return city;
    }
    public String getState() {
        return state;
    }
    public String getCountry() {
        return country;
    }
    public String getPostalCode() {
        return postalCode;
    }
    public String getKnownName() {
        return knownName;
    }   
}
 

Star-Dust

Expert
Licensed User
Longtime User
forgive me maybe I did not understand the topic well. But can't you get the same with this function without needing the library?

 
Top