douwei1921 2016-11-29 22:57
浏览 9
已采纳

带有golang和mongodb的storelocator

Hey i am trying to build a store-locator using golang and mongodb . I am new to both . I tried to search but was not able to find a code in golang which can help me insert store with name and co-ordinates into mongodb and then search for stores in a radius of 3000 meters of provided latitude and longitude value .

Note : i want to provide id by myself so that i can easily fetch stores if i have the id .

I tried as given below but nothing happens , no error or store insertion in db

test.go is as below :

package main

import (
    "encoding/json"
    "fmt"
    "gopkg.in/mgo.v2"
    "gopkg.in/mgo.v2/bson"
    "log"
)

type Store struct {
    ID       string  `bson:"_id,omitempty" json:"shopid"`
    Name     string  `bson:"name" json:"name"`
    Location GeoJson `bson:"location" json:"location"`
}

type GeoJson struct {
    Type        string    `json:"-"`
    Coordinates []float64 `json:"coordinates"`
}

func main() {
    cluster := "localhost" // mongodb host

    // connect to mongo
    session, err := mgo.Dial(cluster)
    if err != nil {
        log.Fatal("could not connect to db: ", err)
        panic(err)
    }
    defer session.Close()
    session.SetMode(mgo.Monotonic, true)

    // search criteria
    long := 39.70
    lat := 35.69
    scope := 3000 // max distance in metres

    var results []Store // to hold the results

    // query the database
    c := session.DB("test").C("stores")

    // insert
    man := Store{}
    man.ID = "1"
    man.Name = "vinka medical store"
    man.Location.Type = "Point"
    man.Location.Coordinates = []float64{lat, long}

    // insert
    err = c.Insert(man)
    fmt.Printf("%v
", man)
    if err != nil {
        fmt.Println("There is insert error")
        panic(err)
    }

    // ensure
    // Creating the indexes
    index := mgo.Index{
        Key:  []string{"$2dsphere:location"},
        Bits: 26,
    }
    err = c.EnsureIndex(index)
    if err != nil {
        fmt.Println("There is index error")
        panic(err)
    }

    //find
    err = c.Find(bson.M{
        "location": bson.M{
            "$nearSphere": bson.M{
                "$geometry": bson.M{
                    "type":        "Point",
                    "coordinates": []float64{long, lat},
                },
                "$maxDistance": scope,
            },
        },
    }).All(&results)

    //err = c.Find(bson.M{"_id": "1"}).All(&results)
    if err != nil {
        panic(err)
    }

    //convert it to JSON so it can be displayed
    formatter := json.MarshalIndent
    response, err := formatter(results, " ", "   ")

    fmt.Println(string(response))
}
  • 写回答

1条回答 默认 最新

  • dongshiru5913 2016-12-01 18:46
    关注

    @user7227958

    Looks like Go is playing tricks with you due to (lack of) float64 preciseness of your coordinates.

    Check this: with long := 39.70 and lat := 35.69 you need a scope=3000000 for your record to be selected. However if you'll use more precise coordinates, say long := 39.6910592 lat := 35.6909623 then you'll be able to find your store even with a scope of 1 meter (which is expected).

    Cheers, -D

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ROS Turtlebot3 多机协同自主探索环境时遇到的多机任务分配问题,explore节点
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题