Skip to content

Instantly share code, notes, and snippets.

@luckydevilru
Last active August 15, 2018 10:41
Show Gist options
  • Select an option

  • Save luckydevilru/ee74ebcd9deb79b736eedf3c6dcd16ac to your computer and use it in GitHub Desktop.

Select an option

Save luckydevilru/ee74ebcd9deb79b736eedf3c6dcd16ac to your computer and use it in GitHub Desktop.
Геопозиционирование яндекс определение города по ip php js. +редирект на определившийся город.
<script src="http://api-maps.yandex.ru/2.0/?load=package.standard,package.geoObjects&lang=<? echo($map_lang); ?>"></script> // lang=ru_RU или lang=en_US
$(document).ready(function(){
ymaps.ready(init);
// seo
function init(){
<?
// echo $city_;
if ($city_=='москва') {echo "var php_city = 'москва';";}
elseif ($city_=='moscow') {echo "var php_city = 'moscow'";}
else {echo "var php_city = 'none'";}
?>
var geolocation = ymaps.geolocation;
var geocity = geolocation.city.toLowerCase();
var url_null = window.location.toString();
if (php_city=='москва'&&geocity!=='москва') {
if (window.location.toString().indexOf("/ru/%D0%BC%D0%BE%D1%81%D0%BA%D0%B2%D0%B0/") > -1) { // /ru/москва/
window.location = url_null.replace(/%D0%BC%D0%BE%D1%81%D0%BA%D0%B2%D0%B0/, geolocation.city.toLowerCase().replace(" ", "-") ); // /москва/
}
}else if(php_city=='moscow'&&geocity!=='moscow'){
if (window.location.toString().indexOf("/en/moscow/") > -1) { // /ru/москва/
window.location = url_null.replace(/moscow/, geolocation.city.toLowerCase().replace(" ", "-") ); // /москва/
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment