Skip to content

Instantly share code, notes, and snippets.

@todiadiyatmo
Created June 22, 2017 13:38
Show Gist options
  • Select an option

  • Save todiadiyatmo/1827ffd65f2d04141139b6bb98c08907 to your computer and use it in GitHub Desktop.

Select an option

Save todiadiyatmo/1827ffd65f2d04141139b6bb98c08907 to your computer and use it in GitHub Desktop.
Hitung Jarak Long Lat Range Query
set @meterR = 111195;
set @range = 2300 / @meterR;

select id,( st_distance(shape,point(-7.747347,110.404036)) * @meterR  ) as distance from locations 
where st_distance(shape,point(-7.747347,110.404036)) < @range
and locationable_type = 'App\\Models\\Perawat'
$query->havingRaw("distance < locations.range*1000");

CREATE TABLE IF NOT EXISTS `locations` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `shape` point NOT NULL,
  `locationable_type` varchar(100) NOT NULL,
  `locationable_id` int(10) unsigned NOT NULL,
  `alamat` varchar(2000) NOT NULL,
  `range` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `id` (`id`),
  SPATIAL KEY `point_spatial` (`shape`),
  KEY `locations_locationable_id_index` (`locationable_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=22 ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment