计算当前位置信息的经纬度
我现在有一个固定的地址,但我想计算当前的位置的经度和纬度(可以用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'
然后,我从这个地址获取纬度和经度。我想做的是获取用户在浏览器中的当前位置,然后使用这个纬度和经度。
1 个回答
2
你可以使用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 */
}
);
虽然这还不是一个正式的规范,但它在大多数电脑浏览器和一些手机浏览器上都能正常工作。这里有一份支持这个功能的设备列表。