使用地理位置更改货币

时间:2015-09-22 10:10:46

标签: javascript html5 geolocation opencart-module

我正在使用opencart开发一个网站,我计划支持多种货币。货币应根据用户的实际位置而变化;也就是说,如果一个人从美国打开它,则货币可见性设置为美元,如果印度人打开它,则应将其设置为INR。这可能吗?

2 个答案:

答案 0 :(得分:0)

试试这个:

function getLocation() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPosition);
  } else {
    console.log("Geolocation is not supported by this browser.");
  }
}

function showPosition(position) {
  GetAddress(position.coords.latitude, position.coords.longitude)
}

function GetAddress(lat, lng) {
  var latlng = new google.maps.LatLng(lat, lng);
  var geocoder = geocoder = new google.maps.Geocoder();
  geocoder.geocode({
    'latLng': latlng
  }, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      if (results[0]) {
        var address = results[1].address_components;
        for (var i = 0, iLen = address.length; i < iLen; i++) {
          if (address[i].types[0] === 'country') {
            var countryName = address[i].long_name;
            alert(countryName);
          }
        }
      }
    }
  });
}
<button type="button" onclick="getLocation()">Get Location</button>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>

Plunker here

答案 1 :(得分:0)

您可以使用GeoLocation进行操作,这是代码

 $('.currency').text(geoplugin_currencyCode());  
       $('.symbol').html(geoplugin_currencySymbol());  
<script language="JavaScript" src="http://www.geoplugin.net/javascript.gp" type="text/javascript">
    </script>
<h6><span class="symbol"></span>90 <span class="currency"></span></h6>