谷歌地图api地理定位和地方搜索

时间:2013-06-24 02:06:54

标签: javascript google-maps search google-maps-api-3 geolocation

我想在我的网络应用中加入Google地图,因为我使用谷歌地图API。

我在地图上包括地点搜索和地理位置(我有一个地理定位按钮),但是我注意到一旦按下地理定位按钮,地点搜索无法正常工作。

有什么想法吗?

这是我的代码

        <html>
      <head>
        <meta charset="utf-8" />
        <link rel="stylesheet" type="text/css" href="theme.css">

        <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
        <meta charset="utf-8">
        <link href='http://fonts.googleapis.com/css?family=Source+Sans+Pro:200&subset=latin,latin-ext' rel='stylesheet' type='text/css'>


        <style type="text/css" media="screen">

        </style>
        <style>
          html, body, #map-canvas {
            margin: 0;
            padding: 0;
            height: 100%;
          }
          #target 
          {
            width: 10%; 
          }


        </style>
        <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
        <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=places"></script>

        <script>



          /*map*/
          // Enable the visual refresh
    google.maps.visualRefresh = true;
    var map;
    function initialize() {
      var mapOptions = {
        zoom: 8,
        center: new google.maps.LatLng(-34.397, 150.644),
        mapTypeId: google.maps.MapTypeId.ROADMAP
      };
      map = new google.maps.Map(document.getElementById('map-canvas'),
          mapOptions);
    }

    google.maps.event.addDomListener(window, 'load', initialize);
          /*map end*/

                /*search*/

          function initialize() {

      var map = new google.maps.Map(document.getElementById('map-canvas'), {
        mapTypeId: google.maps.MapTypeId.ROADMAP
      });
      var defaultBounds = new google.maps.LatLngBounds(
          new google.maps.LatLng(-33.8902, 151.1759),
          new google.maps.LatLng(-33.8474, 151.2631));
      map.fitBounds(defaultBounds);

      var input = /** @type {HTMLInputElement} */(document.getElementById('target'));
      var searchBox = new google.maps.places.SearchBox(input);
      var markers = [];

      google.maps.event.addListener(searchBox, 'places_changed', function() {
        var places = searchBox.getPlaces();

        for (var i = 0, marker; marker = markers[i]; i++) {
          marker.setMap(null);
        }

        markers = [];
        var bounds = new google.maps.LatLngBounds();
        for (var i = 0, place; place = places[i]; i++) {
          var image = {
            url: place.icon,
            size: new google.maps.Size(71, 71),
            origin: new google.maps.Point(0, 0),
            anchor: new google.maps.Point(17, 34),
            scaledSize: new google.maps.Size(25, 25)
          };

          var marker = new google.maps.Marker({
            map: map,
            icon: image,
            title: place.name,
            position: place.geometry.location
          });

          markers.push(marker);

          bounds.extend(place.geometry.location);
        }

        map.fitBounds(bounds);
      });

      google.maps.event.addListener(map, 'bounds_changed', function() {
        var bounds = map.getBounds();
        searchBox.setBounds(bounds);
      });
    }

    google.maps.event.addDomListener(window, 'load', initialize);
          /*end search*/
        </script>
        <script>
          /*geolocation*/
          var map;

    function geolocate() {
      var mapOptions = {
        zoom: 6,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      };
      map = new google.maps.Map(document.getElementById('map-canvas'),
          mapOptions);

      // Try HTML5 geolocation
      if(navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) {
          var pos = new google.maps.LatLng(position.coords.latitude,
                                           position.coords.longitude);

          var infowindow = new google.maps.InfoWindow({
            map: map,
            position: pos,
            content: 'Current Location'
          });

          map.setCenter(pos);
        }, function() {
          handleNoGeolocation(true);
        });
      } else {
        // Browser doesn't support Geolocation
        handleNoGeolocation(false);
      }
    }

    function handleNoGeolocation(errorFlag) {
      if (errorFlag) {
        var content = 'Error: The Geolocation service failed.';
      } else {
        var content = 'Error: Your browser doesn\'t support geolocation.';
      }

      var options = {
        map: map,
        position: new google.maps.LatLng(60, 105),
        content: content
      };

      var infowindow = new google.maps.InfoWindow(options);
      map.setCenter(options.position);
    }

    google.maps.event.addDomListener(window, 'load', initialize);
          /*/geolocation*/


        </script>

      </head>
      <body>
     <!--search dialog-->
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
    <script>
      $(function() {

        $( "#dialog-message" ).dialog({ autoOpen: false });
        $(' #dialog-message ').dialog({dialogClass:'search'});
        $( "#dialog-message" ).dialog({
        modal: true,
        });
    });
    </script>
    <style type="text/css" media="screen">
      .search { background:rgba(240, 255, 255, 0.4);}
      .ui-widget-header {font-family: source sans pro; background: white; border: none; border-radius: 0px}
      .pac-container {font-family: source sans pro !important; font-size: 20px !important; min-height: 20% !important}
    </style>

    <div style="font-family: source sans pro;" id="dialog-message" title="Download complete" class="draggable" >
        <p>
           <div id="panel" width="100%" height="100%">
             <input type="text" id="target" placeholder="Search Box" style="width: 100%; font-family: source sans pro; font-size: 20px; height: auto">
            </div>
    </div>
    <!--/search dialog--> 
   <table>
    <div id="map-canvas"></div>

    <table id="commandbar" width="100%" height="5%" cellspacing="0" valign="center" style="background-color:rgba(240, 255, 255, 0.4) ;position:fixed; bottom:0;">
        <tr height="2.5%">
          <td width="2.5%" height="2.5%" >
            <img src="browser_resources/close.png" class="button" onclick="window.close()" width="10em">
          </td>
          <td width="2.5%" height="2.5%" >
            <img src="browser_resources/new_window.png" class="button" onclick="window.open('maps.html','_blank', 'location=no, menubar=no, status=no, titlebar=no' )" width="100%">
          </td>
          <td width="1px"></td>
          <td width="1px" id="divider" bgcolor="#999999">
          </td>
          <td width="1px"></td>
          <td width="2.5%">
            <img src="maps_resources/Search.png" class="button" onclick="$( '#dialog-message' ).dialog( 'open' ); " width="100%">
          </td>
          <td width="2.5%">
            <img src="maps_resources/Search.png" class="button" onclick="geolocate()" width="100%">
          </td>
          <td width="95%"></td>
      </tr>
    </table>

  </body>
</html>

1 个答案:

答案 0 :(得分:1)

您正在使用新的Maps-instance覆盖geolocate内的可见地图。但是用于与地点相关的代码的Maps-instance仍然是在initialize中创建的实例(它仍然作为对象存在,但不再绑定到您看到的映射)。因此,您的位置代码仍然有效,但您没有看到任何更改。

解决方案:而不是在geolocate内创建新的Maps-instance,使initialize内创建的Maps-instance全局可访问(通过删除var-keyword)

  1. 删除var中的initialize() - 关键字:        
      var map = new google.maps.Map(document.getElementById('map-canvas'), {
            mapTypeId: google.maps.MapTypeId.ROADMAP
          });

    注意:你已经定义了函数initialize() 2次,只有第2个函数在这里很重要,第一个函数将被覆盖。

  2. geolocate()
     map = new google.maps.Map(document.getElementById('map-canvas'),
              mapOptions);
    
  3. 中删除此内容,同时从文档中删除此内容:
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"><script>
  4. 这不是问题,但是当您多次包含API脚本时可能会出现问题