简体   繁体   English

Google地图居中或标记定位问题

[英]Google Maps centering or marker positioning issue

I develop a simple Google Maps application. 我开发了一个简单的Google Maps应用程序。 I need just to place one marker on the map. 我只需要在地图上放置一个标记即可。 For whatever reason, the marker is placed outside of the visible area of the map. 无论出于何种原因,标记都放置在地图的可见区域之外。 It's a little bit strange, because the map is centered to the marker's coordinates. 有点奇怪,因为地图以标记的坐标为中心。 Here is the code: 这是代码:

var point1 = new GLatLng(location1.lat,location1.lon);              
    map1.setCenter(point1, 15);
var marker1 = new GMarker(point1);
map1.addOverlay(marker1);
map1.setCenter(point1);

When we drag the map a little bit, we can see the marker. 当我们稍微拖动地图时,我们可以看到标记。 What do I need is to center the map in the way the marker will be visible without map dragging. 我需要的是以无需标记拖动即可显示标记的方式将地图居中。

Can anyone help me? 谁能帮我?

I believe the GLatLng object would accept String arguments as well - but to be safe I would ensure that they are integers - try using: 我相信GLatLng对象也将接受String参数-但是为了安全起见,我将确保它们是整数-尝试使用:

new GLatLng(parseInt(location.lat), parseInt(location.lon));

I also noticed you call map.setCenter a second time which ought to not be necessary. 我还注意到您没有必要第二次调用map.setCenter。

Using the following code really ought to do it 使用以下代码确实应该做到这一点

  map=new GMap(document.getElementById("map"));
  var point = new GLatLng(parseInt(location.lat), parseInt(location.lon));
  map.setCenter(point,5);
  var marker = new GMarker(point);
  map.addOverlay(marker);

If you still are having issues I would check that "location" object to make sure the .lat and .lon values are being populated correctly. 如果仍然有问题,我将检查该“位置”对象,以确保正确填充.lat和.lon值。

Check this code out: 检查此代码:

var map = new GMap(document.getElementById("map"));
/* -- snip -- */
map.centerAndZoom(new GPoint(-1.2736, 53.0705), 8);

From a website I made a while ago. 从我前一阵子的网站上。 Feel free to check the source: 随时检查源:

http://www.primrose-house.co.uk/localattractions http://www.primrose-house.co.uk/localattractions

Just click the link in the top right to switch to the map view. 只需单击右上角的链接即可切换到地图视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM