N
N
Nubbb2021-02-12 14:32:33
Google Maps
Nubbb, 2021-02-12 14:32:33

How to determine city by user ip via google in Laravel + Nuxt?

tell me how to organize the determination of the user's location on the site, that is, before loading the site,

I thought to determine the location through the plugin, but the coordinates are displayed after the site is loaded, but it is necessary before the page is displayed

export default function (context) {
  navigator.geolocation.getCurrentPosition(
    (position) => {
      console.log(position.coords.latitude)
      console.log(position.coords.longitude)
    },
    (error) => {
      console.log(error.message)
    }
  )
}


maybe there is a package for Laravel that determines the user's city by the user's ip? if, for example, send a request to Laravel via axios

so that the user's coordinates come from the back: and the city, coordinates, and I wrote them down in the side

export default async function (context) {
  await context.$axios
    .get('api/v1/geo')
    .then((response) =>
      context.store.commit('user/SET_GEO', response.geo)
    )
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Konstantin B., 2021-02-12
@Kostik_1993

navigator is a client theme. You need to get the user's IP from the headers and knock on your API with it.
For example, like this

V
Victor Smolsky, 2021-09-06
@simplysiteru

In my projects, I solve this problem with the help of such a snippet.
Place a static method in any class, pass the user's IP to it.

public static function ip_info($ip = NULL, $purpose = "location", $deep_detect = TRUE) {
    $output = NULL;
    if (filter_var($ip, FILTER_VALIDATE_IP) === FALSE) {
      $ip = $_SERVER["REMOTE_ADDR"];
      if ($deep_detect) {
        if (filter_var(@$_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP))
          $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        if (filter_var(@$_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP))
          $ip = $_SERVER['HTTP_CLIENT_IP'];
      }
    }
    $purpose    = str_replace(array("name", "\n", "\t", " ", "-", "_"), NULL, strtolower(trim($purpose)));
    $support    = array("country", "countrycode", "state", "region", "city", "location", "address");
    $continents = array(
      "AF" => "Africa",
      "AN" => "Antarctica",
      "AS" => "Asia",
      "EU" => "Europe",
      "OC" => "Australia (Oceania)",
      "NA" => "North America",
      "SA" => "South America"
    );
    if (filter_var($ip, FILTER_VALIDATE_IP) && in_array($purpose, $support)) {
      $ipdat = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=" . $ip));
      if (@strlen(trim($ipdat->geoplugin_countryCode)) == 2) {
        switch ($purpose) {
          case "location":
            $output = array(
              "city"           => @$ipdat->geoplugin_city,
              "state"          => @$ipdat->geoplugin_regionName,
              "country"        => @$ipdat->geoplugin_countryName,
              "country_code"   => @$ipdat->geoplugin_countryCode,
              "continent"      => @$continents[strtoupper($ipdat->geoplugin_continentCode)],
              "continent_code" => @$ipdat->geoplugin_continentCode
            );
            break;
          case "address":
            $address = array($ipdat->geoplugin_countryName);
            if (@strlen($ipdat->geoplugin_regionName) >= 1)
              $address[] = $ipdat->geoplugin_regionName;
            if (@strlen($ipdat->geoplugin_city) >= 1)
              $address[] = $ipdat->geoplugin_city;
            $output = implode(", ", array_reverse($address));
            break;
          case "city":
            $output = @$ipdat->geoplugin_city;
            break;
          case "state":
            $output = @$ipdat->geoplugin_regionName;
            break;
          case "region":
            $output = @$ipdat->geoplugin_regionName;
            break;
          case "country":
            $output = @$ipdat->geoplugin_countryName;
            break;
          case "countrycode":
            $output = @$ipdat->geoplugin_countryCode;
            break;
        }
      }
    }
    return $output;
  }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question