Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "_id" : ObjectId("58da18fb5f2caa20fffa8cf8"),
- "name" : "Barbara Clark",
- "status" : true,
- "location" : {
- "type" : "Point",
- "coordinates" : [
- 108.12309125955089,
- -7.421944444444445
- ]
- }
- }
- // insert dummy location from latitude and longitude.
- func insertDummyMarkLocation(cityName string, city *cityModel.City) {
- // some location in Bandung
- lat := -6.8647721
- lon := 107.553501
- var locations []location.Location
- // geneerate location with distance 1 km in every point and limit lenght 50 km.
- // so it will be (50/1)^2 = 2500 district
- newLocations = location.GenerateLocation(lat, lon, 1, 50)
- }
- Bandung_district_58da0f8a5f2caa20ff61c1a7
- type City struct {
- Id bson.ObjectId `json:"id" bson:"_id,omitempty"`
- Name string `json:"name"`
- District int `json:"district"`
- Location GeoJson `json:"location"`
- }
- // get Near district in the city with given distance in meters
- func (c *City) GetNearestDistrict(cityName string, lat, lon float64, distance int64) (City, error) {
- var err error
- var city City
- err = checkMongoConnection(mongo)
- if err != nil {
- logger.Println(err)
- return city, err
- }
- collection := mongo.DB("Driver").C(cityName)
- err = collection.Find(bson.M{
- "location": bson.M{
- "$nearSphere": bson.M{
- "$geometry": bson.M{
- "type": "Point",
- "coordinates": []float64{lon, lat}, // lon,lat in order is the rule from mongodb
- },
- "$maxDistance": distance,
- },
- },
- }).One(&city)
- if err != nil {
- logger.Println(err)
- return city, err
- }
- return city, nil
- }
- type DriverData struct {
- Id bson.ObjectId `json:"id" bson:"_id,omitempty"`
- Name string `json:"name"`
- Status bool `json:"status"`
- Location GeoJson `json:"location"`
- }
- // struct for storing geo location in mongodb
- type GeoJson struct {
- Type string `json:"type"`
- Coordinates []float64 `json:"coordinates"`
- }
- // Get available driver from given cityName and idDistrict/marked location.
- func (d *DriverData) GetAvailableDriver(city, idDistrict string) []DriverData {
- collectionKey := getFormatDistrict(city, idDistrict)
- collection := mongo.DB("Driver").C(collectionKey)
- var drivers []DriverData
- err := collection.Find(bson.M{
- "status": true,
- }).Limit(100).All(&drivers)
- if err != nil {
- logger.Println(err)
- }
- return drivers
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement