简体   繁体   English

计算当前位置经纬度

[英]Calculate the current location longitude and latitude

I have a hard-coded address for particular place but i want to calculate current location longitude and latitude (using jquery or python script).我有一个特定地点的硬编码地址,但我想计算当前位置的经度和纬度(使用 jquery 或 python 脚本)。 This is my Django view at the moment:这是我目前的 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))

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.我想做的是在浏览器中获取用户的当前位置,然后使用这个纬度/经度。

You can use the W3 geolocation API to do this client-side in JavaScript.您可以使用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 */
  }
);

It's not an official specification yet, but it does work on most PC browsers and some phone browsers.它还不是官方规范,但它确实适用于大多数 PC 浏览器和一些手机浏览器。 Here's a list of devices where it does work .这是它工作的设备列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 查找当前位置的纬度和经度 - Find latitude and longitude of current location 获取用户当前位置的经纬度 - Fetch latitude and longitude on user's current location 如何在OpenLayers中获取用户的当前位置以及纬度和经度 - how to get the current location of the user along with latitude and longitude in OpenLayers 如何获取当前位置(经度和纬度)并将其发送到反向地理编码API? - How to get current location (latitude and longitude) and send it to an reverse geocoding API? 如何使用谷歌地图查找当前位置(经度和纬度)? - How to find the current location(longitude and latitude) using Google Map? 检测到当前位置后如何在mapbox中获取经度和纬度? - How to get longitude and latitude in mapbox after detect current location? 开放层中当前位置的纬度和经度 - latitude and longitude of current position in openlayer 如何使用我的位置使用经度和纬度计算两点之间的距离 - How to use my location to calculate distance between two points using latitude and longitude 如何获取我当前位置的经度和纬度并显示在两个单独的 HTML 表格上 - How to get longitude and latitude of my current location and display on two separate HTML form 如何计算两个经纬度之间的距离 - How to calculate the distance between two latitude and longitude
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM