计算当前位置的经纬度

发布于 2024-11-05 05:05:34 字数 1279 浏览 0 评论 0原文

我有一个特定地点的硬编码地址,但我想计算当前位置的经度和纬度(使用 jquery 或 python 脚本)。这是我目前的 Django 视图:

def maps(request):
    school = School.objects.get(id=1)
    dictte={'latitude':school.latitute,'longitude':school.longitude}
    print dictte
    print dictte.keys()
    print dictte['latitude'],'-----'
    school_latitute = float(dictte['latitude'])
    print school_latitute,'raj'
    print dictte['longitude'],'===='
    school_longtitude = float(dictte['longitude'])
    print school
    api_key="aa"
    gmaps = GoogleMaps(api_key)
    address = 'Veerannapalya,Bangalore,Karnataka, India - 560043'
    lat, lng = gmaps.address_to_latlng(address)
    print lat, lng

    if lat == school_latitute and lng == school_longtitude:

        schoo = {'Name of the school':school.name, 'address':school.address.address}
        strf= str(schoo)
        print strf

        return HttpResponse(simplejson.dumps(strf),content_type = 'application/json; charset=utf8')
    else:
        print 'nothing'    
    return render_to_response('staff.html',{'lat':lat,'lng':lng},context_instance=RequestContext(request))

如您所见,地址当前在行中进行硬编码:

address = 'Veerannapalya,Bangalore,Karnataka, India - 560043'

然后,从中,我获得纬度/经度。我想做的是获取用户在浏览器中的当前位置,然后使用这个纬度/经度。

I have a hard-coded address for particular place but i want to calculate current location longitude and latitude (using jquery or python script). This is my Django view at the moment:

def maps(request):
    school = School.objects.get(id=1)
    dictte={'latitude':school.latitute,'longitude':school.longitude}
    print dictte
    print dictte.keys()
    print dictte['latitude'],'-----'
    school_latitute = float(dictte['latitude'])
    print school_latitute,'raj'
    print dictte['longitude'],'===='
    school_longtitude = float(dictte['longitude'])
    print school
    api_key="aa"
    gmaps = GoogleMaps(api_key)
    address = 'Veerannapalya,Bangalore,Karnataka, India - 560043'
    lat, lng = gmaps.address_to_latlng(address)
    print lat, lng

    if lat == school_latitute and lng == school_longtitude:

        schoo = {'Name of the school':school.name, 'address':school.address.address}
        strf= str(schoo)
        print strf

        return HttpResponse(simplejson.dumps(strf),content_type = 'application/json; charset=utf8')
    else:
        print 'nothing'    
    return render_to_response('staff.html',{'lat':lat,'lng':lng},context_instance=RequestContext(request))

As you can see, the address is currently hardcoded in the line:

address = 'Veerannapalya,Bangalore,Karnataka, India - 560043'

Then, from this, I obtain the latitude/longitude. What I'd like to do is rather obtain the current location of the user in the browser, and then use this latitude/longitude.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤独岁月 2024-11-12 05:05:34

您可以使用 W3 地理定位 API 在客户端执行此操作JavaScript。

navigator.geolocation.getCurrentPosition(
  function(pos){
    var lat = pos.coords.latitude;
    var long = pos.coords.longitude;
    // send coordinates to server, or display on map, if you wish
  },
  function(){
    /* Handler if location could not be found */
  }
);

它还不是官方规范,但它确实适用于大多数 PC 浏览器和一些手机浏览器。以下是支持该功能的设备列表。

You can use the W3 geolocation API to do this client-side in JavaScript.

navigator.geolocation.getCurrentPosition(
  function(pos){
    var lat = pos.coords.latitude;
    var long = pos.coords.longitude;
    // send coordinates to server, or display on map, if you wish
  },
  function(){
    /* Handler if location could not be found */
  }
);

It's not an official specification yet, but it does work on most PC browsers and some phone browsers. Here's a list of devices where it does work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文