Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 谷歌地图+地理位置w/SQL_Php_Sql_Maps_Location - Fatal编程技术网

Php 谷歌地图+地理位置w/SQL

Php 谷歌地图+地理位置w/SQL,php,sql,maps,location,Php,Sql,Maps,Location,我有一个脚本,它在一个SQL数据库中显示一组地址,它们都是地理编码的,并将它们放在谷歌地图上。我似乎不知道如何使用移动设备GPS将地图中心定位在用户的位置上。我有一个脚本可以做到这一点,但我不知道如何结合这两个功能。任何人请帮助我,失去睡眠 这是我的密码: <?php require_once('/home/session_data.php'); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN

我有一个脚本,它在一个SQL数据库中显示一组地址,它们都是地理编码的,并将它们放在谷歌地图上。我似乎不知道如何使用移动设备GPS将地图中心定位在用户的位置上。我有一个脚本可以做到这一点,但我不知道如何结合这两个功能。任何人请帮助我,失去睡眠

这是我的密码:

    <?php
    require_once('/home/session_data.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
  <meta name="viewport" content="width=480px"/>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>TEST</title>
<script src="http://www.google.com/jsapi?key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"></script>
<script>
    (function () {
        google.load("maps", "2");
        google.setOnLoadCallback(function () {

            // Create map
            var map = new google.maps.Map2(document.getElementById("map")),
                markerText = "<?php echo $_SESSION['SESS_FULL_NAME']?> - #<?php echo $_SESSION['SESS_MEMBER_ID']?><br><?php echo $_SESSION['SESS_DEPT']?> - <?php echo $_SESSION['SESS_REGION']?>",
                markOutLocation = function (lat, long) {
                    var latLong = new google.maps.LatLng(lat, long),
                        marker = new google.maps.Marker(latLong);
                    map.setCenter(latLong, 13);
                    map.addOverlay(marker);
                    marker.openInfoWindow(markerText);
                    google.maps.Event.addListener(marker, "click", function () {
                        marker.openInfoWindow(markerText);
                    });
                };
                map.setUIToDefault();

            // Check for geolocation support    
            if (navigator.geolocation) {
                // Get current position
                navigator.geolocation.getCurrentPosition(function (position) {
                        // Success!
                        markOutLocation(position.coords.latitude, position.coords.longitude);
                    }, 
                    function () {
                        // Gelocation fallback: Defaults to New York, US
                        markerText = "<p>Please accept geolocation for me to be able to find you. <br>I've put you in New York for now.</p>";
                        markOutLocation(40.714997,-74.006653);
                    }
                );
            }
            else {
                // No geolocation fallback: Defaults to Eeaster Island, Chile
                markerText = "<p>No location support. Try Easter Island for now. :-)</p>";
                markOutLocation(-27.121192, -109.366424);
            }
        }); 
    })();
</script>
<script type="text/javascript"> 

//<![CDATA[

      // this variable will collect the html which will eventually be placed in the side_bar 

      var side_bar_html = ""; 



      // arrays to hold copies of the markers and html used by the side_bar 

      // because the function closure trick doesnt work there 

      var gmarkers = []; 



     // global "map" variable

      var map = null;

// A function to create the marker and set up the event window function 

function createMarker(latlng, name, html) {

    var contentString = html;

    var marker = new google.maps.Marker({

        position: latlng,

        map: map,

        zIndex: Math.round(latlng.lat()*-100000)<<5

        });



    google.maps.event.addListener(marker, 'click', function() {

        infowindow.setContent(contentString); 

        infowindow.open(map,marker);

        });

    // save the info we need to use later for the side_bar

    gmarkers.push(marker);

    // add a line to the side_bar html

    side_bar_html += '<a href="javascript:myclick(' + (gmarkers.length-1) + ')">' + name + '<\/a><br>';

}



// This function picks up the click and opens the corresponding info window

function myclick(i) {

  google.maps.event.trigger(gmarkers[i], "click");

}



function initialize() {

  // create the map

  var myOptions = {

    zoom: 8,

    center: new google.maps.LatLng(43.907787,-79.359741),

    mapTypeControl: true,

    mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},

    navigationControl: true,

    mapTypeId: google.maps.MapTypeId.ROADMAP

  }

  map = new google.maps.Map(document.getElementById("map_canvas"),

                                myOptions);



  google.maps.event.addListener(map, 'click', function() {

        infowindow.close();

        });

      // Read the data from example.xml

      downloadUrl("phpsqlajax_genxml2.php", function(doc) {

        var xmlDoc = xmlParse(doc);

        var markers = xmlDoc.documentElement.getElementsByTagName("marker");

        for (var i = 0; i < markers.length; i++) {

          // obtain the attribues of each marker

          var lat = parseFloat(markers[i].getAttribute("lat"));

          var lng = parseFloat(markers[i].getAttribute("lng"));

          var point = new google.maps.LatLng(lat,lng);

          var html = markers[i].getAttribute("html");

          var label = markers[i].getAttribute("label");

          // create the marker

          var marker = createMarker(point,label,html);

        }

        // put the assembled side_bar_html contents into the side_bar div

        document.getElementById("side_bar").innerHTML = side_bar_html;

      });

    }



var infowindow = new google.maps.InfoWindow(

  { 

    size: new google.maps.Size(150,50)

  });

//]]>

</script> 
</head>

  <body onload="load()" onunload="Unload()">
    <div id="map" style="width: 480px; height: 300px"></div>
  </body>
</html>

您只提供了map,但一半的代码处理map\u canvas

在第一个脚本中声明了一个map变量,在第二个脚本中,在initialize下,再次声明它,我认为它覆盖了第一个map,这对于将var markOutLocation提供的latlong居中是至关重要的


我建议在initialize函数中重命名地图及其关系,我假设它将控制另一个地图元素。

看起来您使用的是旧版本的google maps API。第3版是最新版本

为了指示GoogleMaps,javascript加载了sensor=true。请注意,在V3中,传感器参数是必需的,因此如果您不希望它使用GPS,则必须将其显式设置为false:

#
# Example using sensor when loading the Maps JavaScript API
#
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=true"></script>

是在指定的纬度/经度上使地图居中的位置,在本例中,该纬度/经度是用户的位置

// Note that using Google Gears requires loading the Javascript
// at http://code.google.com/apis/gears/gears_init.js

var initialLocation;
var siberia = new google.maps.LatLng(60, 105);
var newyork = new google.maps.LatLng(40.69847032728747, -73.9514422416687);
var browserSupportFlag =  new Boolean();

function initialize() {
  var myOptions = {
    zoom: 6,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

  // Try W3C Geolocation (Preferred)
  if(navigator.geolocation) {
    browserSupportFlag = true;
    navigator.geolocation.getCurrentPosition(function(position) {
      initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
      map.setCenter(initialLocation);
    }, function() {
      handleNoGeolocation(browserSupportFlag);
    });
  // Try Google Gears Geolocation
  } else if (google.gears) {
    browserSupportFlag = true;
    var geo = google.gears.factory.create('beta.geolocation');
    geo.getCurrentPosition(function(position) {
      initialLocation = new google.maps.LatLng(position.latitude,position.longitude);
      map.setCenter(initialLocation);
    }, function() {
      handleNoGeoLocation(browserSupportFlag);
    });
  // Browser doesn't support Geolocation
  } else {
    browserSupportFlag = false;
    handleNoGeolocation(browserSupportFlag);
  }

  function handleNoGeolocation(errorFlag) {
    if (errorFlag == true) {
      alert("Geolocation service failed.");
      initialLocation = newyork;
    } else {
      alert("Your browser doesn't support geolocation. We've placed you in Siberia.");
      initialLocation = siberia;
    }
    map.setCenter(initialLocation);
  }
}
map.setCenter(initialLocation);