Hi

First, thx for this module...

Second, i new to drupal and this might be a noob question, but here it comes:

Is it possible to use data from Smart Ip inside views? - i'm trying use the zip and country in the gmaps tool, so the map is opened in the area where the user is located - like on ex. gpsies.com.

thx
//Brian

Comments

arpeggio’s picture

Status: Active » Closed (works as designed)

Hi,

Yes, you can use data from Smart IP in your views, blocks, custom module, etc. via PHP code. If you wish to access geolocation of the authenticated user, you may use Drupal $user object:

global $user;
$user->data['geoip_location']['country']
$user->data['geoip_location']['country_code']
$user->data['geoip_location']['zip']
$user->data['geoip_location']['city']
$user->data['geoip_location']['latitude']
$user->data['geoip_location']['longitude']
$user->data['geoip_location']['region']
$user->data['geoip_location']['region_code']

And if you wish to track including your anonymous users, enable the Device Geolocation sub module and you may access a more detailed geolocation of your visitors:

$_SESSION['smart_ip']['country']
$_SESSION['smart_ip']['country_code']
$_SESSION['smart_ip']['zip']
$_SESSION['smart_ip']['latitude']
$_SESSION['smart_ip']['longitude']
$_SESSION['smart_ip']['region']
$_SESSION['smart_ip']['region_code']
$_SESSION['smart_ip']['street_number']
$_SESSION['smart_ip']['postal_code']
$_SESSION['smart_ip']['route']
$_SESSION['smart_ip']['neighborhood']
$_SESSION['smart_ip']['locality']
$_SESSION['smart_ip']['sublocality']
$_SESSION['smart_ip']['establishment']
$_SESSION['smart_ip']['administrative_area_level_1']
$_SESSION['smart_ip']['administrative_area_level_2']
etc...
uolevi’s picture

I can access smartip from own pages with PHP and draw a separate Gmap with Smartip info:

$ip3=$_SERVER['REMOTE_ADDR']; 
$test3 = smart_ip_get_location($ip3);
if ($test3["latitude"] && $test3["longitude"]) {
  $lat = $test3["latitude"];
  $lon = $test3["longitude"];
}
else {
 $lat = "65.05632748892958";
 $lon = "25.456974506378174";
}

$mymap=array('id' => 'nodemap',
             'latitude' => $lat,
             'longitude'=> $lon,
             'zoom' => 11,
             'width' => '95%',
             'height' => '500px',
);

$outfittermap = theme('gmap', array('#settings' => $mymap));
print $outfittermap;

But I don't understand if I should put the location info to Gmap's macro or where in Views.
Is there any example how to access Smartip location information from Views, because I can not find any info about Device Geolocation or Smartip information in Views, even if they are enabled.

arpeggio’s picture

Status: Closed (works as designed) » Active

I'm sorry, thought that the "Global: Custom text" have PHP input filter and can insert PHP snippet inside the Views fields but unfortunately it is not supported. I'll mark this issue as active and in the next couple of weeks I will try to start support Views.

socialnicheguru’s picture

views customfield might work :)

imDhaval’s picture

love to sub

arpeggio’s picture

@SocialNicheGuru thank you for sharing us about views customfield. I'll try this module if find a time.

TNmoxa’s picture

Version: 6.x-1.0-alpha1 » 7.x-1.4-alpha1

I have a problem calling smart_ip_get_location() from my custom module

Fatal error: Call to undefined function smart_ip_get_location()

I'm using drupal 7.0 and Smart IP version 7.x-1.4-alpha1
Checkbox "Anonymous user" in "Roles To Geolocate" is selected.
I tried to use $_SESSION['smart_ip']['location'], but for anonymous users who come to my site first time drupal troughs a notice

Notice: Undefined variable: _SESSION in bg_picture_get_date() (line 23 of /home/content/06/7198206/html/sites/all/modules/bg_picture/bg_picture.module).

I'm new to drupal, so please help me with any solution.
Thank you!

arpeggio’s picture

aworters’s picture

Version: 7.x-1.4-alpha1 » 6.x-1.x-dev

Hi Arpeggio,
I am also looking to incorporate the Smart IP data into Views... I have installed the Views Custom Field module to help me but still no idea on how to use the correct PHP syntax to make the data appear in Views... Please can you lay out the correct codie for a total programming newbie?
Many thanks in advance!

arpeggio’s picture

Hi aworters,

If you want to show just the visitor's coordinates:

<p>Latitude: <?php print $_SESSION['smart_ip']['location']['latitude']; ?></p>
<p>Longitude: <?php print $_SESSION['smart_ip']['location']['longitude']; ?></p>

Or if you want to display entire location data:

<dl>
  <?php foreach ($_SESSION['smart_ip']['location'] as $item => $value): ?>
    <?php if (!empty($value) && $item != 'region_code' && $item != 'timestamp'): ?>
      <?php
        $item = str_replace('_', ' ', $item);
        $item[0] = strtoupper($item[0]);
      ?>
      <dt><?php print $item; ?></dt>
      <dd><?php print $value; ?></dd>
    <?php endif; ?>
  <?php endforeach; ?>
</dl>

Or simply (if Device Geolocation module is enabled):

<?php print theme('device_geolocation_visitor_info', array('location' => $_SESSION['smart_ip']['location'])); ?>
aworters’s picture

Hi Arpeggio,
Thanks for the prompt reply with lots to work with... I am looking to output the user's County Code, City, Postal Code and the Route. The final plan is that the site would identify the User's Country and City and then display content related to their location. The initial code I have used is:

Country Code: print $_SESSION['smart_ip']['location']['country_code'];

City: print $_SESSION['smart_ip']['location']['city'];

Postal Code: print $_SESSION['smart_ip']['location']['postal_code'];

Route: print $_SESSION['smart_ip']['location']['route'];

I see the output in the preview but when I look at the Views created page it doesn't show anything.
Please excuse my total stupidity but any help making this work would be very much appreciated.

Thanks.

arpeggio’s picture

Hi Aworters,

Please try to add any field (Eg. Node: Title) together with your customfield PHP code. I think Views must query something, it will not display anything if its query is empty, even if your customfield do have values.

aworters’s picture

Hello again Arpeggio.. that worked well, thank you for the help. Would you be interested in doing a bit of development work to help me integrate the user's location into the content? If so, please get in touch and I will forward the project spec.

arpeggio’s picture

Hi Aworters,

You're welcome! I have sent you message in your contact form.

socialnicheguru’s picture

is there a way for me to print the longitude and latitude as a decimal value?

arpeggio’s picture

@SocialNicheGuru, the coordinates is already printing in decimal degrees format. Or do you mean you need to display it in Degrees, Minutes, Seconds Format? I suggest to use this code:

echo latitude_dms($latitude);
echo longitude_dms($longitude);

function dd_to_dms($coord) {
  $negative = ($coord < 0) ? TRUE : FALSE;
  $coord    = abs($coord);
  $degrees  = floor($coord);
  $coord   -= $degrees;
  $coord   *= 60;
  $minutes  = floor($coord);
  $coord   -= $minutes;
  $coord   *= 60;
  $seconds  = round($coord, 6);
  return array($degrees, $minutes, $seconds, $negative);
}
function longitude_dms($coordinate) {
  module_load_include('inc', 'field_coordinates', 'includes/field_coordinates');
  $output = '';
  list($degrees, $minutes, $seconds, $negative) = dd_to_dms($coordinate['longitude']);
  $output .= "${degrees}&deg; ${minutes}' ${seconds}\" ";
  if (!$negative) {
    $output .= 'E';
  }
  else {
    $output .= 'W';
  }
  return $output;
}
function latitude_dms($coordinate) {
  module_load_include('inc', 'field_coordinates', 'includes/field_coordinates');
  $output = '';
  list($degrees, $minutes, $seconds, $negative) = dd_to_dms($coordinate['latitude']);
  $output .= "${degrees}&deg; ${minutes}' ${seconds}\" ";
  if (!$negative) {
    $output .= 'N';
  }
  else {
    $output .= 'S';
  }
  return $output;
}
favosys’s picture

Hello

Does anybody know how I can center the map on the current user?

I have restaurant locations and I want my users to see nearby restaurants. I have my view with all my nodes and they appear on the map all good. I know I can access the current user long a lat with PHP but I can't put PHP on the Gmap macro in the center option not in views or the Gmap configuration.

HELP !!

favosys’s picture

I also found this code to make the map from scratch which works great:

<?php
$ip3=$_SERVER['REMOTE_ADDR']; 
$test3 = smart_ip_get_location($ip3);
if ($test3["latitude"] && $test3["longitude"]) {
  $lat = $test3["latitude"];
  $lon = $test3["longitude"];
}
else {
 $lat = "65.05632748892958";
 $lon = "25.456974506378174";
}

$mymap=array('id' => 'nodemap',
             'latitude' => $lat,
             'longitude'=> $lon,
             'zoom' => 11,
             'width' => '95%',
             'height' => '500px',
);

$outfittermap = theme('gmap', array('#settings' => $mymap));
print $outfittermap;
?>

But I don't know how to add my node locations to it

So now I have a map with all my nodes but not centered on the user and a map centered on the user with no nodes.

socialnicheguru’s picture

add location proximity to your argument for your node view.
under default value select php
add your code

if ($test3["latitude"] && $test3["longitude"]) {
  $lat = $test3["latitude"];
  $lon = $test3["longitude"];
}
else {
 $lat = "65.05632748892958";
 $lon = "25.456974506378174";
}

I haven't tried it. You would have to check what the return values are for the location module argument but in theory it should work

favosys’s picture

Thank you for a quick answer.

I tried this but I couldn't get it to work.

Coordinate type says this:

Coordinate Type:

Type of center point.
Postal code argument format: country_postcode_distance or postcode_distance
Lat/Lon argument format: lat,lon_distance

So for the PHP code I tried:

$ip3=$_SERVER['REMOTE_ADDR']; 
$test3 = smart_ip_get_location($ip3);
if ($test3["latitude"] && $test3["longitude"]) {
  $lat = $test3["latitude"];
  $lon = $test3["longitude"];
}
else {
 $lat = "40";
 $lon = "0";
}

$value = $lat . "," . $lon . "_20";

return value;

But now the map just doesn't appear

favosys’s picture

Nevermind, I checked the GMAP Advanced help and I got it to work with just PHP code :)

socialnicheguru’s picture

where there any modifications to the above to get it to work? If so, can you share your steps?

kenwen’s picture

Hi,

I'm trying to pass the current user long / lat through to a view where I sort data by distance

I've got smart ip and device geolocation modules active and from my limited understanding I should be able to pull the data from device geolocation and pass through to the views sorting criteria?

would appreciate any help!

I think this is the bit I need to do :)
Enter PHP code that returns a latitude/longitude. Do not use . You must return only an array with float values set for the 'latitude' and 'longitude' keys.

Thanks,
Ken

arpeggio’s picture

I think this is the bit I need to do :)
Enter PHP code that returns a latitude/longitude. Do not use <?php ?>. You must return only an array with float values set for the 'latitude' and 'longitude' keys.

I suggest to use this:
array('latitude' => $_SESSION['smart_ip']['latitude'], 'longitude' => $_SESSION['smart_ip']['longitude'])

Note: The $_SESSION['smart_ip']['latitude'], $_SESSION['smart_ip']['longitude'] are the estimate location of your current visitor and it is already in decimal degrees.

birdsnbees’s picture

Subscribing.

kenwen’s picture

Hi Arpeggio,

thanks for that!

however its not quite working - you can see it here, http://www.fitness.io/fitness-map

I'm going to try creating a location view, the one above uses nodes

thx,
Ken

apmsooner’s picture

@arpeggio, made slight modifications to what you suggested to get this to work. I wanted to filter nodes by proximity to the user based on their ip address and this is what i did to achieve that because i think a few people are trying to achieve this...

add this filter- Location: Distance / Proximity
with this setting - use php code to determine latitude/longitude

the code:

$prox = array('latitude' => $_SESSION['smart_ip']['location']['latitude'], 'longitude' => $_SESSION['smart_ip']['location']['longitude']);
return $prox;

The type operator produced the same result for me so didn't seem to matter. Anyhow, after much trial and error... it works for me! I assume this way is in fact correct way or anything you would revise?

arpeggio’s picture

I haven't tried this yet but thank you for sharing apmsooner. =)

apmsooner’s picture

you bet! and thanks for the great module. It was quite easy to setup and utilize for localized ad banners which is what i've been looking to for a while now.

kenwen’s picture

woo hoo - works for me!

exactly what i've been after, thanks

grasmash’s picture

has this been committed?

apmsooner’s picture

As far as what i submitted in post #27, theres no patch necessary. Its just a matter of setting up the view correctly. It works perfectly for me. I'm sure something similar can be done to display on GMAP or whatever so not sure there is anything else to do on this.

arpeggio’s picture

Yes there's no need to commit/push. Thanks to apmsooner.

Sinan Erdem’s picture

Hi, I think I have a similar issue:

I have nodes with some postcodes. For example a node has postcodes through 1000 - 2000. I need to show the related node to the user in a block. I can already do a search for zipcodes with views exposed filters. But what I need is to show the related node to the user from his/her IP.

Is it possible with some kind of argument or something?

Thank you very much...

apmsooner’s picture

Read #27

Sinan Erdem’s picture

apmsooner, thank you for the answer. But I really dont have the coordinate info in my nodes and I cannot geocode it, because my nodes contain an interval of zipcodes.

For example node "A" has start postcode:1000, end postcode: 1200. So if the user resides in zipcode 1111, i need to show the node A to this user.

Maybe I can write an argument php code similar to #27. I would check if the obtained postcode from smart_ip module resides in the interval. If it does, I could send a positive return. I have to try and see..

Again thanks...

apmsooner’s picture

Ahh, yeah i'd say maybe look at #1 and combine what you need from there with the sample i posted in #27 and that would probably get you what you need.

radoya’s picture

Everything sounds perfect but I am not sure where is settings where I can set "use php code to determine latitude/longitude" as mentioned in post #27. When I set filter "Location: Distance / Proximity" there is no option to use any php. I tried also to set argument but under location there are only 3 option: country, region and city: no long/lat available.
I'll be very appreciate for any advice :D.

radoya’s picture

I didn't upgrade to the latest version of location module. Now all settings show up.
Thanks, anyway.

andrea.cavattoni’s picture

Here is not working:-(
the map disappear, anybody had the same problem?

arpeggio’s picture

Status: Active » Fixed

I have implemented this feature. Please use version 6.x-1.3. Thanks to @nyleve101 for sponsoring this feature.

@cavax please create separate issue for the problem you have encountered. Thanks.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.