Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
geocoder
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
geocoder
Commits
3aa9bdd7
Verified
Commit
3aa9bdd7
authored
1 year ago
by
Sjoerd Wenker
Committed by
Juraj Nemec
1 year ago
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#3018204
by sjerdo, jgullstr: File missing: Reverse-Geocode support
parent
a298fe2c
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
plugins/geocoder_reverse_handler/google.inc
+105
-0
105 additions, 0 deletions
plugins/geocoder_reverse_handler/google.inc
with
105 additions
and
0 deletions
plugins/geocoder_reverse_handler/google.inc
0 → 100644
+
105
−
0
View file @
3aa9bdd7
<?php
// $Id$
/**
* @file
* Plugin to provide a google reverse geocoder.
*/
/**
* Plugins are described by creating a $plugin array which will be used
* by the system that includes this file.
*/
$plugin
=
array
(
'title'
=>
t
(
"Google Reverse Geocoder"
),
'description'
=>
t
(
'Reverse Geocodes via google reverse geocoder'
),
'callback'
=>
'geocoder_reverse_google'
,
'terms_of_service'
=>
'http://code.google.com/apis/maps/documentation/geocoding/#Limits'
,
);
/**
* Process Markup
*/
function
geocoder_reverse_google
(
$lat
,
$long
,
$options
=
array
())
{
try
{
geophp_load
();
$orig_location
=
new
Point
(
$long
,
$lat
);
$query
=
array
(
'latlng'
=>
"
$lat
,
$long
"
,
'sensor'
=>
'false'
,
);
// add language if exist
if
(
isset
(
$options
[
'language'
]))
{
$query
[
'language'
]
=
$options
[
'language'
];
}
$url
=
url
(
"http://maps.googleapis.com/maps/api/geocode/json"
,
array
(
'query'
=>
$query
));
$result
=
drupal_http_request
(
$url
);
if
(
isset
(
$result
->
error
))
{
$args
=
array
(
'@code'
=>
$result
->
code
,
'@error'
=>
$result
->
error
,
);
$msg
=
t
(
'HTTP request to google API failed.\nCode: @code\nError: @error'
,
$args
);
throw
new
Exception
(
$msg
);
}
$data
=
json_decode
(
$result
->
data
);
if
(
$data
->
status
!=
'OK'
)
{
$args
=
array
(
'@status'
=>
$data
->
status
);
$msg
=
t
(
'Google API returned bad status.\nStatus: @status'
,
$args
);
throw
new
Exception
(
$msg
);
}
$addresses
=
array
();
foreach
(
$data
->
results
as
$item
)
{
// Check if we should reject these results
if
(
isset
(
$options
[
'reject_results'
]))
{
if
(
in_array
(
$item
->
geometry
->
location_type
,
$options
[
'reject_results'
],
TRUE
))
{
continue
;
}
}
// Reject this result if it is too far
$address
=
new
Point
(
$item
->
geometry
->
location
->
lng
,
$item
->
geometry
->
location
->
lat
);
$distance
=
$orig_location
->
distance
(
$address
);
if
(
isset
(
$options
[
'reject_distance'
])
&&
$distance
>
$options
[
'reject_distance'
])
{
continue
;
}
$address
->
address
=
$item
->
formatted_address
;
// Add additional metadata to the geometry - it might be useful!
$address
->
data
=
array
();
$address
->
data
[
'geocoder_accuracy'
]
=
$item
->
geometry
->
location_type
;
$address
->
data
[
'geocoder_formatted_address'
]
=
$item
->
formatted_address
;
$address
->
data
[
'geocoder_address_components'
]
=
$item
->
address_components
;
$address
->
data
[
'distance'
]
=
$distance
;
$addresses
[]
=
$address
;
}
if
(
empty
(
$addresses
))
{
return
;
}
// The connonical geometry is the first result (best guesse)
$result
=
array_shift
(
$addresses
);
// If there are any other addresses, these are auxiliary addresses that represent "alternatives"
if
(
count
(
$addresses
))
{
$result
->
data
[
'geocoder_alternatives'
]
=
$addresses
;
}
return
$result
;
}
catch
(
Exception
$e
)
{
watchdog_exception
(
'geocoder'
,
$e
);
return
FALSE
;
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment