Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. {
  2. "_id" : ObjectId("58da18fb5f2caa20fffa8cf8"),
  3. "name" : "Barbara Clark",
  4. "status" : true,
  5. "location" : {
  6. "type" : "Point",
  7. "coordinates" : [
  8. 108.12309125955089,
  9. -7.421944444444445
  10. ]
  11. }
  12. }
  13.  
  14. // insert dummy location from latitude and longitude.
  15. func insertDummyMarkLocation(cityName string, city *cityModel.City) {
  16. // some location in Bandung
  17. lat := -6.8647721
  18. lon := 107.553501
  19. var locations []location.Location
  20.  
  21. // geneerate location with distance 1 km in every point and limit lenght 50 km.
  22. // so it will be (50/1)^2 = 2500 district
  23. newLocations = location.GenerateLocation(lat, lon, 1, 50)
  24. }
  25.  
  26. Bandung_district_58da0f8a5f2caa20ff61c1a7
  27.  
  28. type City struct {
  29. Id bson.ObjectId `json:"id" bson:"_id,omitempty"`
  30. Name string `json:"name"`
  31. District int `json:"district"`
  32. Location GeoJson `json:"location"`
  33. }
  34.  
  35. // get Near district in the city with given distance in meters
  36. func (c *City) GetNearestDistrict(cityName string, lat, lon float64, distance int64) (City, error) {
  37. var err error
  38. var city City
  39.  
  40. err = checkMongoConnection(mongo)
  41. if err != nil {
  42. logger.Println(err)
  43. return city, err
  44. }
  45.  
  46. collection := mongo.DB("Driver").C(cityName)
  47.  
  48. err = collection.Find(bson.M{
  49. "location": bson.M{
  50. "$nearSphere": bson.M{
  51. "$geometry": bson.M{
  52. "type": "Point",
  53. "coordinates": []float64{lon, lat}, // lon,lat in order is the rule from mongodb
  54. },
  55. "$maxDistance": distance,
  56. },
  57. },
  58. }).One(&city)
  59. if err != nil {
  60. logger.Println(err)
  61. return city, err
  62. }
  63.  
  64. return city, nil
  65. }
  66.  
  67. type DriverData struct {
  68. Id bson.ObjectId `json:"id" bson:"_id,omitempty"`
  69. Name string `json:"name"`
  70. Status bool `json:"status"`
  71. Location GeoJson `json:"location"`
  72. }
  73.  
  74. // struct for storing geo location in mongodb
  75. type GeoJson struct {
  76. Type string `json:"type"`
  77. Coordinates []float64 `json:"coordinates"`
  78. }
  79.  
  80. // Get available driver from given cityName and idDistrict/marked location.
  81. func (d *DriverData) GetAvailableDriver(city, idDistrict string) []DriverData {
  82. collectionKey := getFormatDistrict(city, idDistrict)
  83.  
  84. collection := mongo.DB("Driver").C(collectionKey)
  85.  
  86. var drivers []DriverData
  87. err := collection.Find(bson.M{
  88. "status": true,
  89. }).Limit(100).All(&drivers)
  90.  
  91. if err != nil {
  92. logger.Println(err)
  93. }
  94.  
  95. return drivers
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement