谷歌地图在网页上的观点

时间:2015-01-14 07:23:06

标签: javascript mysql google-maps

我从未使用Google Map对数据库驱动的地址进行Google Map API集成。我已经获得了此功能的代码,但没有在网页上获得预期的视图。我想,这个对于以前处理过谷歌地图API的人来说,这是一个非常简单的问题。

请注意,我已经在谷歌开发者网站,SO和其他博客上研究了几个小时。但没有成功,因为我不确定哪些因素导致了这个问题。

This view is what I am getting,

enter image description here 而且,预期的观点是

enter image description here

这是代码

<script src="http://maps.google.com/maps/api/js?v=3&sensor=false" type="text/javascript">   </script>
 <script type="text/javascript">
 //Sample code written by August Li
  var icon = new google.maps.MarkerImage("http://maps.google.com/mapfiles/ms/micons/blue.png",
  new google.maps.Size(32, 32), new google.maps.Point(0,0),
  new google.maps.Point(16, 32));
  var center = null;
  var map = null;
  var currentPopup;
  var bounds = new google.maps.LatLngBounds();
  function addMarker(lat, lng, info) {
  var pt = new google.maps.LatLng(lat, lng);
  bounds.extend(pt);
  var marker = new google.maps.Marker({
  position: pt,
  icon: icon,
  map: map
  });
  var popup = new google.maps.InfoWindow({
  content: info,
  maxWidth: 300

  });
  google.maps.event.addListener(marker, "click", function() {
    if (currentPopup != null) {
      currentPopup.close();
      currentPopup = null;
    }
  popup.open(map, marker);
  currentPopup = popup;
  });
  google.maps.event.addListener(popup, "closeclick", function() {
  map.panTo(center);

  currentPopup = null;
  });
  }
  function initMap() {
  map = new google.maps.Map(document.getElementById("map"), {
  //map.panBy(0,30);
   //map.setZoom(15);

  center: new google.maps.LatLng(0,0),
  zoom: 14,
  mapTypeId: google.maps.MapTypeId.ROADMAP,
  mapTypeControl: false,
  mapTypeControlOptions: {
  style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR
  },
  navigationControl: true,
  navigationControlOptions: {
  style: google.maps.NavigationControlStyle.SMALL
  }
  });
  <?php
   if(isset($_GET['user_map'])){
     $mapusrx=$_GET['user_map'];

     $query = mysqli_query($connecti,"SELECT * FROM map WHERE user_id='$mapusrx'");
       while ($row = mysqli_fetch_assoc($query)){
         $name=$row['title'];
         $lat=$row['lat'];
         $lon=$row['lon'];
         $desc=$row['address'];
         echo ("addMarker($lat, $lon,'<b>$name</b><br/>$desc');\n");
        }
    }
  ?>
  center = bounds.getCenter();
  map.fitBounds(bounds);

  }

CSS

#map { 
width: 240px; 
height: 700px; 
border: 0px;
padding: 0px;
position:absolute;
top:0px;
right:0px;
}

非常感谢您的帮助。

3 个答案:

答案 0 :(得分:0)

使用此代码,

<script src="http://maps.google.com/maps/api/js?v=3&sensor=false" type="text/javascript">   </script>
 <script type="text/javascript">
 //Sample code written by August Li
  var icon = new google.maps.MarkerImage("http://maps.google.com/mapfiles/ms/micons/blue.png",
  new google.maps.Size(240, 700), new google.maps.Point(0,0),
  new google.maps.Point(16, 32));
  var center = null;
  var map = null;
  var currentPopup;
  var bounds = new google.maps.LatLngBounds();
  function addMarker(lat, lng, info) {
  var pt = new google.maps.LatLng(lat, lng);
  bounds.extend(pt);
  var marker = new google.maps.Marker({
  position: pt,
  icon: icon,
  map: map
  });
  var popup = new google.maps.InfoWindow({
  content: info,
  maxWidth: 300

  });
  google.maps.event.addListener(marker, "click", function() {
    if (currentPopup != null) {
      currentPopup.close();
      currentPopup = null;
    }
  popup.open(map, marker);
  currentPopup = popup;
  });
  google.maps.event.addListener(popup, "closeclick", function() {
  map.panTo(center);

  currentPopup = null;
  });
  }
  function initMap() {
  map = new google.maps.Map(document.getElementById("map"), {
  //map.panBy(0,30);
   //map.setZoom(15);

  center: new google.maps.LatLng(0,0),
  zoom: 4,
  mapTypeId: google.maps.MapTypeId.ROADMAP,
  mapTypeControl: false,
  mapTypeControlOptions: {
  style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR
  },
  navigationControl: true,
  navigationControlOptions: {
  style: google.maps.NavigationControlStyle.SMALL
  }
  });
  <?php
   if(isset($_GET['user_map'])){
     $mapusrx=$_GET['user_map'];

     $query = mysqli_query($connecti,"SELECT * FROM map WHERE user_id='$mapusrx'");
       while ($row = mysqli_fetch_assoc($query)){
         $name=$row['title'];
         $lat=$row['lat'];
         $lon=$row['lon'];
         $desc=$row['address'];
         echo ("addMarker($lat, $lon,'<b>$name</b><br/>$desc');\n");
        }
    }
  ?>
  center = bounds.getCenter();
  map.fitBounds(bounds);

  }


**CSS**

#map { 
width: 240px; 
height: 700px; 
border: 0px;
padding: 0px;
position:absolute;
top:0px;
right:0px;
}

答案 1 :(得分:0)

特别感谢Barryhunter,他解决了这个问题@ Google Maps Forum

只需在<?php ?> block

之后使用以下代码即可
center = bounds.getCenter();
map.setCenter(center);
map.setZoom(15);

Changing setZoom() value will give desired view on the map.

答案 2 :(得分:-1)

也许你可以尝试减少 缩放:14级