如何在javascript中获取变量并将其放在这里的地图中获取地理位置?

时间:2015-11-05 03:42:25

标签: javascript here-api

任何人都可以解决此代码吗? 我有一些javascript代码地理位置和这里的地图脚本,我想把地理位置坐标放在这里的地图脚本.. 在这下面我的代码..

<!DOCTYPE html>
<html>
<head>
 <meta name="viewport" content="initial-scale=1.0, width=device-width" />
<link rel="stylesheet" type="text/css" href="http://js.api.here.com/v3/3.0.12.2/mapsjs-ui.css" />
      <script src="http://js.api.here.com/v3/3.0.12.2/mapsjs-core.js"  type="text/javascript" charset="utf-8"></script>
        <script src="http://js.api.here.com/v3/3.0.12.2/mapsjs-service.js"  type="text/javascript" charset="utf-8"></script>
        <script src="http://js.api.here.com/v3/3.0.12.2/mapsjs-mapevents.js" type="text/javascript" charset="utf-8"></script>
        <script src="http://js.api.here.com/v3/3.0.12.2/mapsjs-pano.js" type="text/javascript" charset="utf-8"></script>
        <script src="http://js.api.here.com/v3/3.0.12.2/mapsjs-ui.js" type="text/javascript" charset="utf-8"></script>

</head>
<body>
<button onclick="getLocation()">My Location</button><br/>
 <input type="text" id="latpos" name="latpos">
 <input type="text" id="longpos" name="longpos">
 <div id="map" style="width: 100%; height: 400px; background: grey" />
 <script  type="text/javascript" charset="UTF-8" >

var x = document.getElementById("demo");
function getLocation() {
            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(showPosition);
        } else {
            x.alert("Geolocation is not supported by this browser.");
        }
    }
    function showPosition(position) {
      var origlat= position.coords.latitude ;
      var origlong= position.coords.longitude ;
      var lat=document.getElementById("latpos");
      lat.value=origlat;
      var lon=document.getElementById("longpos");
      lon.value=origlong;
}


function addMarkerAndSetViewBounds(map) {
  var latitu="-6.811408423530006";
  var longitu= "110.85068956017494";
  var origl= origlat;
  var origlo= origlong;
  var Isone = new H.map.Marker({lat:latitu, lng:longitu}),
      geol = new H.map.Marker({lat:origl, lng:origlo}),
      group = new H.map.Group();

  group.addObjects([Isone , geol]);
  map.addObject(group);

// get geo bounding box for the group and set it to the map
  map.setViewBounds(group.getBounds());

 }
    /Step 1: initialize communication with the platform
  var platform = new H.service.Platform({
  app_id: 'DemoAppId01082013GAL',
  app_code: 'AJKnXv84fjrb0KIHawS0Tg',
    useCIT: false,
  });
var defaultLayers = platform.createDefaultLayers();

//Step 2: initialize a map - this map is centered over Europe
var map = new H.Map(document.getElementById('map'),
  defaultLayers.normal.map,{
  center: {lat:50, lng:5},
  zoom: 15
});

//Step 3: make the map interactive
// MapEvents enables the event system
// Behavior implements default interactions for pan/zoom (also on mobile touch environments)
var behavior = new H.mapevents.Behavior(new H.mapevents.MapEvents(map));

// Create the default UI components
var ui = H.ui.UI.createDefault(map, defaultLayers);

// Now use the map as required...

addMarkerAndSetViewBounds(map);

 </script>
</body>
</html>

但是,该代码没有正常工作.. 如何在变量var origlatvar origlong中获得价值var origlvar origlo? 任何人都可以帮我修复此代码正确运行

1 个答案:

答案 0 :(得分:0)

你应该创建origlat,origlong全局变量,而不是局部变量。

http://www.w3schools.com/js/js_scope.asp