簡體   English   中英

計算當前位置經緯度

[英]Calculate the current location longitude and latitude

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

然后,從中,我獲得緯度/經度。 我想做的是在瀏覽器中獲取用戶的當前位置,然后使用這個緯度/經度。

您可以使用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 瀏覽器和一些手機瀏覽器。 這是它工作的設備列表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM