#javascript #google-geolocation
#javascript #google-geolocation
Вопрос:
как я могу указать, чтобы браузер находил местоположение пользователей altomatico, я хотел бы, чтобы вы показывали уведомление в браузере, чтобы можно было фиксировать местоположение моих пользователей без необходимости вводить их.
Я хотел бы, чтобы вы показали уведомление в браузере, чтобы можно было зафиксировать местоположение моих пользователей без необходимости вводить текст.
function initMap() { // The location var loc = {lat: latitude, lng: longitude}; // The map, centered at location | draggable: false, scrollwheel: false, var map = new google.maps.Map(document.getElementById('map'), {zoom: 4, center: loc, disableDoubleClickZoom: true, zoomControl: true, scaleControl: false, mapTypeControl: false, streetViewControl: false, fullscreenControl: false}); // The marker, positioned at location var marker = new google.maps.Marker({position: loc, map: map, draggable: true}); var geocoder; geocoder = new google.maps.Geocoder(); codeLatLng(loc.lat, loc.lng); google.maps.event.addListener(marker, 'dragend', function(evt) { codeLatLng(evt.latLng.lat().toFixed(3), evt.latLng.lng().toFixed(3)); map.setCenter(marker.position); marker.setMap(map); }); google.maps.event.addListener(map, 'dblclick', function(evt) { codeLatLng(evt.latLng.lat().toFixed(3), evt.latLng.lng().toFixed(3)); marker.setPosition(evt.latLng); map.setCenter(marker.position); marker.setMap(map); }); map.addListener("click", function (evt) { }); function codeLatLng(lat, lng) { var latlng = new google.maps.LatLng(lat, lng); var itemPolitical = ""; var itemCountry = ""; var itemCity = ""; geocoder.geocode({latLng: latlng}, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { if (results[1]) { var arrAddress = results; console.log(results); $.each(arrAddress, function(i, address_component) { if (address_component.types[0] == "locality") { console.log("City: " address_component.address_components[0].long_name); itemCity = address_component.address_components[0].long_name; } if (address_component.types[0] == "country") { console.log("Country: " address_component.address_components[0].long_name); itemCountry = address_component.address_components[0].long_name; } if (address_component.types[1] == "political") { if (typeof address_component.address_components[1] !== 'undefined') { if (typeof address_component.address_components[1].long_name !== 'undefined') { console.log("Political: " address_component.address_components[1].long_name); itemPolitical = address_component.address_components[1].long_name; } else { if (typeof address_component.address_components[1].short_name !== 'undefined') { console.log("Political: " address_component.address_components[1].short_name); itemPolitical = address_component.address_components[1].short_name; } } } } setGeolocation(lat, lng, itemCountry, itemCity, itemPolitical); }); } else { setGeolocation(lat, lng, itemCountry, itemCity, itemPolitical); } } else { setGeolocation(lat, lng, itemCountry, itemCity, itemPolitical); } }); } } function setGeolocation(lat, lng, country, city, political) { var location = ""; if (country.length == 0) { country = political; } if (country.length gt; 0) { location = country; } if (city.length gt; 0) { if (location.length gt; 0) { location = location ", " city; } else { location = location city; } } $("input[name=lat]").val(lat); $("input[name=lng]").val(lng); $("input[name=country]").val(country); $("input[name=city]").val(city); if (location.length gt; 0) { $('i.ad-location').removeClass('icon-map-pin'); $('i.ad-location').addClass('icon-check-circle'); $('i.ad-location').addClass('text-green'); } else { $('i.ad-location').removeClass('icon-check-circle'); $('i.ad-location').removeClass('text-green'); $('i.ad-location').addClass('icon-map-pin'); location = strings.szLocationError; } $("p#location").text(location); }
PHP
lt;?php if (!defined("APP_SIGNATURE")) { header("Location: /"); exit; } class geo extends db_connect { private $requestFrom = 0; public function __construct($dbo = NULL) { parent::__construct($dbo); } private function getMaxId() { $stmt = $this-gt;db-gt;prepare("SELECT MAX(id) FROM users"); $stmt-gt;execute(); return $number_of_rows = $stmt-gt;fetchColumn(); } private function getMaxItemId() { $stmt = $this-gt;db-gt;prepare("SELECT MAX(id) FROM items"); $stmt-gt;execute(); return $number_of_rows = $stmt-gt;fetchColumn(); } public function getItemsNearby($itemId, $lat, $lng, $distance = 30) { if ($itemId == 0) { $itemId = $this-gt;getMaxItemId(); $itemId ; } $result = array("error" =gt; false, "error_code" =gt; ERROR_SUCCESS, "itemId" =gt; $itemId, "items" =gt; array()); $tableName = "items"; $origLat = $lat; $origLon = $lng; $dist = $distance; // this is max distance (in miles) away from $origLat, $origLon in which to search $sql = "SELECT id, lat, lng, 3956 * 2 * ASIN(SQRT( POWER(SIN(($origLat - lat)*pi()/180/2),2) COS($origLat*pi()/180 )*COS(lat*pi()/180) *POWER(SIN(($origLon-lng)*pi()/180/2),2))) as distance FROM $tableName WHERE lng between ($origLon-$dist/cos(radians($origLat))*69) and ($origLon $dist/cos(radians($origLat))*69) and lat between ($origLat-($dist/69)) and ($origLat ($dist/69)) and (id lt; $itemId) and (id lt;gt; $this-gt;requestFrom) and (removeAt = 0) and (lat lt;gt; 0.000000) and (lng lt;gt; 0.000000) having distance lt; $dist ORDER BY id DESC limit 20"; $stmt = $this-gt;db-gt;prepare($sql); if ($stmt-gt;execute()) { if ($stmt-gt;rowCount() gt; 0) { while ($row = $stmt-gt;fetch()) { $item = new items($this-gt;db); $item-gt;setRequestFrom($this-gt;requestFrom); $itemInfo = $item-gt;info($row['id']); $itemInfo['distance'] = round($this-gt;getDistance($lat, $lng, $itemInfo['lat'], $itemInfo['lng']), 1); unset($item); array_push($result['items'], $itemInfo); $result['itemId'] = $row['id']; } } } return $result; } public function getPeopleNearbyCount($lat, $lng, $distance = 30) { $tableName = "users"; $origLat = $lat; $origLon = $lng; $dist = $distance; // this is the max distance (in miles) away from $origLat, $origLon in which to search $sql = "SELECT count(*), 3956 * 2 * ASIN(SQRT( POWER(SIN(($origLat - lat)*pi()/180/2),2) COS($origLat*pi()/180 )*COS(lat*pi()/180) *POWER(SIN(($origLon-lng)*pi()/180/2),2))) as distance FROM $tableName WHERE lng between ($origLon-$dist/cos(radians($origLat))*69) and ($origLon $dist/cos(radians($origLat))*69) and lat between ($origLat-($dist/69)) and ($origLat ($dist/69)) and (id lt;gt; $this-gt;requestFrom) and (state = 0) having distance lt; $dist"; $stmt = $this-gt;db-gt;prepare($sql); $stmt-gt;execute(); return $number_of_rows = $stmt-gt;fetchColumn(); } public function getPeopleNearby($itemId, $lat, $lng, $distance = 30) { if ($itemId == 0) { $itemId = $this-gt;getMaxId(); $itemId ; } $result = array("error" =gt; false, "error_code" =gt; ERROR_SUCCESS, "itemId" =gt; $itemId, "items" =gt; array()); $tableName = "users"; $origLat = $lat; $origLon = $lng; $dist = $distance; // This is the maximum distance (in miles) away from $origLat, $origLon in which to search $sql = "SELECT id, lat, lng, 3956 * 2 * ASIN(SQRT( POWER(SIN(($origLat - lat)*pi()/180/2),2) COS($origLat*pi()/180 )*COS(lat*pi()/180) *POWER(SIN(($origLon-lng)*pi()/180/2),2))) as distance FROM $tableName WHERE lng between ($origLon-$dist/cos(radians($origLat))*69) and ($origLon $dist/cos(radians($origLat))*69) and lat between ($origLat-($dist/69)) and ($origLat ($dist/69)) and (id lt; $itemId) and (id lt;gt; $this-gt;requestFrom) and (state = 0) having distance lt; $dist ORDER BY id DESC limit 20"; $stmt = $this-gt;db-gt;prepare($sql); if ($stmt-gt;execute()) { if ($stmt-gt;rowCount() gt; 0) { while ($row = $stmt-gt;fetch()) { $profile = new profile($this-gt;db, $row['id']); $profile-gt;setRequestFrom($this-gt;requestFrom); $profileInfo = $profile-gt;get(); $profileInfo['distance'] = round($this-gt;getDistance($lat, $lng, $profileInfo['lat'], $profileInfo['lng']), 1); unset($profile); array_push($result['items'], $profileInfo); $result['itemId'] = $row['id']; unset($profile); } } } return $result; } public function getDistance($fromLat, $fromLng, $toLat, $toLng) { $latFrom = deg2rad($fromLat); $lonFrom = deg2rad($fromLng); $latTo = deg2rad($toLat); $lonTo = deg2rad($toLng); $delta = $lonTo - $lonFrom; $alpha = pow(cos($latTo) * sin($delta), 2) pow(cos($latFrom) * sin($latTo) - sin($latFrom) * cos($latTo) * cos($delta), 2); $beta = sin($latFrom) * sin($latTo) cos($latFrom) * cos($latTo) * cos($delta); $angle = atan2(sqrt($alpha), $beta); return ($angle * 6371000) / 1000; } public function info($ip_addr) { $info = helper::getContent('http://www.geoplugin.net/json.gp?ip='.$ip_addr); return json_decode($info, true); } public function setRequestFrom($requestFrom) { $this-gt;requestFrom = $requestFrom; } public function getRequestFrom() { return $this-gt;requestFrom; } }