计算当前位置经纬度

2024-10-03 13:22:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个特定地点的硬编码地址,但我想计算当前位置经度和纬度(使用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'

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


Tags: 编码addressrequest地址printlnglatschool
1条回答
网友
1楼 · 发布于 2024-10-03 13:22:57

您可以在JavaScript中使用W3 geolocation API在客户端执行此操作。

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浏览器和一些手机浏览器。这是devices where it does work的列表。

相关问题 更多 >