Skip to content

Commit b78e413

Browse files
committed
Geo location
1 parent 8bc873e commit b78e413

File tree

9 files changed

+329
-0
lines changed

9 files changed

+329
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/*
3+
@Author Amit Bera
4+
5+
@ Website: www.amitbera.com
6+
*/
7+
class Devamitbera_Searchbylocation_Helper_Data extends Mage_Core_Helper_Abstract{
8+
9+
public function getVendorProductByLatLong(){
10+
11+
$resource = Mage::getSingleton('core/resource');
12+
$readConnection = $resource->getConnection('core_read');
13+
14+
$latSearch= Mage::getModel('core/cookie')->get('latSearch');
15+
$longSearch=Mage::getModel('core/cookie')->get('longSearch');
16+
$latSearch= 18.45;
17+
$longSearch=73.80;
18+
19+
$query = 'SELECT vendor_id, ( 6371 * acos( cos( radians('.$latSearch.') ) * cos( radians( SUBSTRING(position,1,17) ) ) * cos( radians( SUBSTRING(position,20,17)) - radians('.$longSearch.') ) + sin( radians('.$latSearch.') ) * sin( radians( SUBSTRING(position,1,17) ) ) ) ) AS distance FROM ag6_ves_vendor_map HAVING distance < 6 ORDER BY distance LIMIT 0 , 6';
20+
$results = $readConnection->fetchAll($query);
21+
$Vendorids=array();
22+
foreach($results as $each){
23+
$Vendorids[]= $each['vendor_id'];
24+
}
25+
$product_ids=Mage::getModel('pricecomparison2/pricecomparison')->getCollection()
26+
->addFieldToFilter('vendor_id',array('in'=>$Vendorids))
27+
->getColumnValues('product_id');
28+
return $product_ids;
29+
30+
}
31+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
@Author Amit Bera
4+
5+
@ Website: www.amitbera.com
6+
*/
7+
8+
class Devamitbera_Searchbylocation_Model_Layer extends Mage_CatalogSearch_Model_Layer
9+
{
10+
11+
/**
12+
* Prepare product collection
13+
*
14+
* @param Mage_Catalog_Model_Resource_Eav_Resource_Product_Collection $collection
15+
* @return Mage_Catalog_Model_Layer
16+
*/
17+
public function prepareProductCollection($collection)
18+
{
19+
$collection
20+
->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
21+
->addSearchFilter(Mage::helper('catalogsearch')->getQuery()->getQueryText())
22+
->setStore(Mage::app()->getStore())
23+
->addMinimalPrice()
24+
->addFinalPrice()
25+
->addTaxPercents()
26+
->addStoreFilter()
27+
->addUrlRewrite();
28+
$CurrentLocationProducts=Mage::helper('searchbylocation')->getVendorProductByLatLong();
29+
$productids = array_filter($CurrentLocationProducts);
30+
if(!empty($productids)):
31+
$collection->addIdFilter($productids);
32+
else:
33+
$condition = '';
34+
$collection->addFieldToFilter('entity_id', $condition);;
35+
endif;
36+
37+
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
38+
Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($collection);
39+
40+
return $this;
41+
}
42+
43+
44+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
/*
3+
@Author Amit Bera
4+
5+
@ Website: www.amitbera.com
6+
*/
7+
class Devamitbera_Searchbylocation_IndexController extends Mage_Core_Controller_Front_Action{
8+
public function saveLastLongAction(){
9+
10+
$cookie = Mage::getSingleton('core/cookie');
11+
$cookie->set('latSearch', Mage::app()->getRequest()->getParam('latSearch') ,time()+8640000);
12+
$cookie->set('longSearch', Mage::app()->getRequest()->getParam('longSearch') ,time()+8640000);
13+
}
14+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
@Author Amit Bera
4+
5+
@ Website: www.amitbera.com
6+
*/-->
7+
<config>
8+
<modules>
9+
<Devamitbera_Searchbylocation>
10+
<version>1.0.0</version>
11+
</Devamitbera_Searchbylocation>
12+
</modules>
13+
<!-- define payment model block, models,helpes
14+
-->
15+
<global>
16+
<blocks>
17+
<searchbylocation>
18+
<class>Devamitbera_Searchbylocation_Block</class>
19+
</searchbylocation>
20+
</blocks>
21+
<helpers>
22+
<searchbylocation>
23+
<class>Devamitbera_Searchbylocation_Helper</class>
24+
</searchbylocation>
25+
</helpers>
26+
<models>
27+
<searchbylocation>
28+
<class>Devamitbera_Searchbylocation_Model</class>
29+
</searchbylocation>
30+
<catalogsearch>
31+
<rewrite>
32+
<layer>Devamitbera_Searchbylocation_Model_Layer</layer>
33+
</rewrite>
34+
</catalogsearch>
35+
</models>
36+
</global>
37+
<!-- define a layout file -->
38+
<frontend>
39+
<routers>
40+
<searchbylocation>
41+
<use>standard</use>
42+
<args>
43+
<module>Devamitbera_Searchbylocation</module>
44+
<frontName>searchbylocation</frontName>
45+
</args>
46+
</searchbylocation>
47+
</routers>
48+
<layout>
49+
<updates>
50+
<searchbylocation>
51+
<file>searchbylocation.xml</file>
52+
</searchbylocation>
53+
</updates>
54+
</layout>
55+
</frontend>
56+
57+
</config>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
@Author Amit Bera
4+
5+
@ Website: www.amitbera.com
6+
*/-->
7+
<layout version="0.1.0">
8+
<!--
9+
Default layout, loads most of the pages
10+
-->
11+
<default>
12+
<reference name="head">
13+
<block type="core/template" name="addgoogle.code" template="searchbylocation/googlejs.phtml" />
14+
</reference>
15+
<reference name="top.search">
16+
<action method="setTemplate"><template>searchbylocation/form.mini.phtml</template></action>
17+
<block type="core/template" name="localsearch" template="searchbylocation/localsearch.phtml" />
18+
</reference>
19+
</default>
20+
</layout>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
* Magento
4+
*
5+
* NOTICE OF LICENSE
6+
*
7+
* This source file is subject to the Academic Free License (AFL 3.0)
8+
* that is bundled with this package in the file LICENSE_AFL.txt.
9+
* It is also available through the world-wide-web at this URL:
10+
* http://opensource.org/licenses/afl-3.0.php
11+
* If you did not receive a copy of the license and are unable to
12+
* obtain it through the world-wide-web, please send an email
13+
* to [email protected] so we can send you a copy immediately.
14+
*
15+
* DISCLAIMER
16+
*
17+
* Do not edit or add to this file if you wish to upgrade Magento to newer
18+
* versions in the future. If you wish to customize Magento for your
19+
* needs please refer to http://www.magento.com for more information.
20+
*
21+
* @category design
22+
* @package base_default
23+
* @copyright Copyright (c) 2006-2015 X.commerce, Inc. (http://www.magento.com)
24+
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
25+
*/
26+
/* @var $this Mage_Core_Block_Template */
27+
/* @var $catalogSearchHelper Mage_Catalogsearch_Helper_Data */
28+
$catalogSearchHelper = $this->helper('catalogsearch');
29+
?>
30+
<?php echo $this->getChildHtml('localsearch');?>
31+
<form id="search_mini_form" action="<?php echo $catalogSearchHelper->getResultUrl() ?>" method="get">
32+
<div class="form-search">
33+
<label for="search"><?php echo $this->__('Search:') ?></label>
34+
<input id="search" type="text" name="<?php echo $catalogSearchHelper->getQueryParamName() ?>" value="<?php echo $catalogSearchHelper->getEscapedQueryText() ?>" class="input-text" maxlength="<?php echo $catalogSearchHelper->getMaxQueryLength();?>" />
35+
<button type="submit" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Search')) ?>" class="button"><span><span><?php echo $this->__('Search') ?></span></span></button>
36+
<div id="search_autocomplete" class="search-autocomplete"></div>
37+
<script type="text/javascript">
38+
//<![CDATA[
39+
var searchForm = new Varien.searchForm('search_mini_form', 'search', '<?php echo Mage::helper('core')->jsQuoteEscape($this->__('Search entire store here...')) ?>');
40+
searchForm.initAutocomplete('<?php echo $catalogSearchHelper->getSuggestUrl() ?>', 'search_autocomplete');
41+
//]]>
42+
</script>
43+
</div>
44+
</form>
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=places"></script>
2+
<script type="text/javascript">
3+
//var geocoder;
4+
function initialize() {
5+
var address = (document.getElementById('my-address'));
6+
var autocomplete = new google.maps.places.Autocomplete(address);
7+
autocomplete.setTypes(['geocode']);
8+
google.maps.event.addListener(autocomplete, 'place_changed', function() {
9+
var place = autocomplete.getPlace();
10+
if (!place.geometry) {
11+
return;
12+
}
13+
14+
var address = '';
15+
if (place.address_components) {
16+
address = [
17+
(place.address_components[0] && place.address_components[0].short_name || ''),
18+
(place.address_components[1] && place.address_components[1].short_name || ''),
19+
(place.address_components[2] && place.address_components[2].short_name || '')
20+
].join(' ');
21+
}
22+
});
23+
}
24+
function codeAddress() {
25+
geocoder = new google.maps.Geocoder();
26+
var address = document.getElementById("my-address").value;
27+
geocoder.geocode( { 'address': address}, function(results, status) {
28+
if (status == google.maps.GeocoderStatus.OK) {
29+
Mage.Cookies.set('latSearch',results[0].geometry.location.lat());
30+
Mage.Cookies.set('longSearch',results[0].geometry.location.lng())
31+
alert("Latitude: "+results[0].geometry.location.lat());
32+
alert("Longitude: "+results[0].geometry.location.lng());
33+
34+
new Ajax.Request('<?php echo $this->getUrl('searchbylocation/index/saveLastLong')?>', {
35+
method:'get',
36+
parameters: {
37+
latSearch: results[0].geometry.location.lat(),
38+
longSearch: results[0].geometry.location.lng()
39+
},
40+
onSuccess: function(transport) {
41+
//alert("amit");
42+
},
43+
onFailure: function() { console.log('Something went wrong...'); }
44+
});
45+
46+
}
47+
48+
else {
49+
alert("Geocode was not successful for the following reason: " + status);
50+
}
51+
});
52+
}
53+
function FindLocation(currentElement) {
54+
console.log(currentElement.value.length);
55+
if(currentElement.value.length>3){
56+
//document.getElementById("my-address").setAttribute("disabled", false);
57+
//document.getElementById('my-address').removeAttribute('disabled');
58+
// document.getElementById("my-address").value='';
59+
initialize();
60+
}
61+
//google.maps.event.addDomListener(window, 'load', initialize);
62+
63+
}
64+
65+
66+
function GetGeoLocation() {
67+
google.maps.event.addDomListener(window, 'load', initialize);
68+
if (navigator.geolocation) {
69+
navigator.geolocation.getCurrentPosition(showPosition);
70+
} else {
71+
x.innerHTML = "Geolocation is not supported by this browser.";
72+
}
73+
}
74+
75+
function showPosition(position) {
76+
//x.innerHTML = "Latitude: " + position.coords.latitude +
77+
//"<br>Longitude: " + position.coords.longitude;
78+
codeLatLng(position.coords.latitude,position.coords.longitude)
79+
}
80+
function codeLatLng(lat, lng) {
81+
var geocoder = new google.maps.Geocoder();
82+
var latlng = new google.maps.LatLng(lat, lng);
83+
geocoder.geocode({'latLng': latlng}, function(results, status) {
84+
if (status == google.maps.GeocoderStatus.OK) {
85+
console.log(results)
86+
if (results[1]) {
87+
document.getElementById("my-address").value=results[3].formatted_address;
88+
} else {
89+
alert("No results found");
90+
}
91+
} else {
92+
alert("Geocoder failed due to: " + status);
93+
}
94+
});
95+
}
96+
97+
98+
GetGeoLocation();
99+
100+
</script>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<input type="text" id="my-address" onkeypress="FindLocation(this)">
2+
<button id="getCords" onClick="codeAddress();">getLat&Long</button>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
@Author Amit Bera
4+
5+
@ Website: www.amitbera.com
6+
-->
7+
<config>
8+
<modules>
9+
<Devamitbera_Searchbylocation>
10+
<codePool>community</codePool>
11+
<active>true</active>
12+
<depends>
13+
<Mage_CatalogSearch/>
14+
</depends>
15+
</Devamitbera_Searchbylocation>
16+
</modules>
17+
</config>

0 commit comments

Comments
 (0)