2011-05-04 65 views
-1

我有一個特定地方的硬編碼地址,但我想計算當前位置的經度和緯度(使用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'

然後,從這個,我獲得的緯度/經度。我想要做的是在瀏覽器中獲取用戶的當前位置,然後使用此經度/緯度。任何幫助將不勝感激。

+2

你需要澄清你的問題多一點。我不確定你在問什麼。我看到的只是代碼。 – Pwnna 2011-05-04 14:44:52

回答

2

您可以使用W3 geolocation 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瀏覽器和一些手機瀏覽器。這裏有一個devices where it does work的列表。