Google Geolocation API

Hello Everyone!! I’m new in blynk. Could you please tell me how to store latitude & longitude values on the variables from this code? I’ve used a valid API, ssid & pass for WiFi, but it won’t show the lat & long value in the serial monitor. I,ve been waited for a long. But nothing is displayed on the serial monitor after connected with the network. If I able to get the values then I’ll modify the code for blynk map by my won. For now, please tell me where is the problem in this code? Why the values are not shown on the serial monitor??

#ifdef ARDUINO_ARCH_SAMD
#include <WiFi101.h>
#elif defined ARDUINO_ARCH_ESP8266
#include <ESP8266WiFi.h>
#elif defined ARDUINO_ARCH_ESP32
#include <WiFi.h>
#include <ESP8266HTTPClient.h>
#else
#error Wrong platform
#endif 

#include <WifiLocation.h>

const char* googleApiKey = "#########################";
const char ssid[] = "#######";
const char pass[] = "######";

WifiLocation location(googleApiKey);

void setup() {
    Serial.begin(115200);
    // Connect to WPA/WPA2 network
#ifdef ARDUINO_ARCH_ESP32
    WiFi.mode(WIFI_MODE_STA);
#endif
#ifdef ARDUINO_ARCH_ESP8266
    WiFi.mode(WIFI_STA);
#endif
    WiFi.begin(ssid, pass);
    while (WiFi.status() != WL_CONNECTED) {
        Serial.print("Attempting to connect to WPA SSID: ");
        Serial.println(ssid);
        // wait 5 seconds for connection:
        Serial.print("Status = ");
        Serial.println(WiFi.status());
        delay(500);
    }
    location_t loc = location.getGeoFromWiFi();

    Serial.println("Location request data");
    Serial.println(location.getSurroundingWiFiJson());
    Serial.println("Latitude: " + String(loc.lat, 7));
    Serial.println("Longitude: " + String(loc.lon, 7));
    Serial.println("Accuracy: " + String(loc.accuracy));


}

void loop() {

}

Please edit your post to add triple backticks at the beginning and end of your code, so that it displays correctly.
Triple backticks look like this:
```

Pete.

1 Like

hello dear, I’ve edited the post. Please help. This is the code from github: https://github.com/gmag11/WifiLocation

How is this related to Blynk?
What is it that you’re trying to achieve?

Pete.

I just want to make this work for blynk map. That is, I want to give the lat & lon value of the blynk map via geolocation api, not manually stated on the code.

If I can display it, then I’ll use the values here.

WidgetMap myMap(V1);
...
int index = 1;
float lat = 51.5074;
float lon = 0.1278;
myMap.location(index, lat, lon, "value");

Waiting won’t help. The code to get the location is in void setup, so runs once on boot, 500ms after the Wi-Fi Status is printed to the serial monitor.

You should probably raise an issue on the github page for this library and let the author sort-out the issue.

Pete.

1 Like

Also, I assume that you’ve done all the things that it says on the github page - assigning a billing method, then logging in to the Developer Console and enabling the geolocation API?

Pete.

is that okay? I have flashed the board 3times & it shows that I’ve used the key 3times… Is that mean my API that I’ve been created on the google is okay?

I have no idea, I’ve never used this service.

Pete.

1 Like

okay, thanks

Have your tried to put this in your void loop? - @PeteKnight has already explained that with this code in your void setup() that it will only run once and won’t update constantly.

    location_t loc = location.getGeoFromWiFi();

    Serial.println("Location request data");
    Serial.println(location.getSurroundingWiFiJson());
    Serial.println("Latitude: " + String(loc.lat, 7));
    Serial.println("Longitude: " + String(loc.lon, 7));
    Serial.println("Accuracy: " + String(loc.accuracy));
1 Like

A timer controlled function would be better… no need to update the geolocation data thousands of times a second :stuck_out_tongue:

2 Likes

I agree, just getting them started first :wink: there’s nothing related to Blynk in there at the moment so the beautifully crafted BlynkTimer isn’t at their disposal :laughing:

Even for a snail ?

:snail: :snail: :snail:

1 Like

True :stuck_out_tongue: Looks like another example of someone joining the forum without doing any reading and asking for us to fix things… hopefully not, but we will see.

However, (for the OP’s info) Blynktimer is based on Simpletimer which will work the same, and allow a more reasonable reading rate with “normal” Arduino code.

2 Likes