简体   繁体   English

如何按纬度和经度设置谷歌地图标记并提供信息泡泡

[英]How to set google map marker by latitude and longitude and provide information bubble

The following sample code provided by google maps api 以下示例代码由google maps api提供

          var geocoder;
          var map;
          function initialize() {
            geocoder = new google.maps.Geocoder();
            var latlng = new google.maps.LatLng(40.77627, -73.910965);
            var myOptions = {
              zoom: 8,
              center: latlng,
              mapTypeId: google.maps.MapTypeId.ROADMAP
            }
            map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
          }

the following only shows google map of the location without a marker. 以下仅显示没有标记的位置的谷歌地图。 I was wondering how I can place a marker by giving latitude/longitude parameters? 我想知道如何通过给出纬度/经度参数来放置标记? And how is it possible to store my own information pulled from a database on that marker? 如何将从数据库中提取的自己的信息存储在该标记上?

Here is a JSFiddle Demo that shows you how to set a google map marker by Lat Lng and also when click would give you an information window (bubble): 这是一个JSFiddle演示 ,向您展示如何通过Lat Lng设置谷歌地图标记,以及点击时会给您一个信息窗口(气泡):

Here is our basic HTML with 3 hyperlinks when clicked adds a marker onto the map: 这是我们的基本HTML,点击时有3个超链接,在地图上添加一个标记:

<div id="map_canvas"></div>

<a href='javascript:addMarker("usa")'>Click to Add U.S.A</a><br/>
<a href='javascript:addMarker("brasil")'>Click to Add Brasil</a><br/>
<a href='javascript:addMarker("argentina")'>Click to Add Argentina</a><br/>

First we set 2 global variables. 首先,我们设置2个全局变量。 one for map and another an array to hold our markers: 一个用于地图,另一个用于保存我们的标记:

var map;
var markers = [];

This is our initialize to create a google map: 这是我们初始化创建谷歌地图:

function initialize() {
    var latlng = new google.maps.LatLng(40.77627, -73.910965);
    var myOptions = {
        zoom: 1,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
}

We then create 3 lat lng locations where we would like to place our markers: 然后我们创建3个lat lng位置,我们想放置我们的标记:

var usa = new google.maps.LatLng(37.09024, -95.712891);
var brasil = new google.maps.LatLng(-14.235004, -51.92528);
var argentina = new google.maps.LatLng(-38.416097, -63.616672);

Here we create a function to add our markers based on whatever is passed onto it. 在这里,我们创建一个函数,根据传递给它的内容添加我们的标记。 myloc will be either usa, brasil or argentina and we then create the marker based on the passed param. myloc将是美国,巴西或阿根廷,然后我们根据传递的参数创建标记。 With in the addMarker function we check and make sure we don't create duplicate marker on the map by calling the for loop and if we the passed param has already been created then we return out of the function and do nothing, else we create the marker and push it onto the global markers array. 在addMarker函数中,我们检查并确保我们不通过调用for循环在地图上创建重复标记,如果我们已经创建了传递的param,那么我们返回函数并且什么都不做,否则我们创建标记并将其推送到全局标记数组。 After the marker is created we then attach an info window with it's associated marker by doing markers[markers.length-1]['infowin'] markers.length-1 is just basically getting the newly pushed marker on the array. 创建标记后,我们通过markers[markers.length-1]['infowin'] markers.length-1基本上获取阵列上新推的标记来附加信息窗口及其相关标记。 Within the info window we set the content using html. 在信息窗口中,我们使用html设置内容。 This is basically the information you put into the bubble or info window (it can be weather information which you can populate using a weather API and etc). 这基本上是您放入气泡或信息窗口的信息(它可以是您可以使用天气API等填充的天气信息)。 After info window is attached we then attach an onclick event listener using the Google Map API's addListener and when the marker is clicked we want to open the info window that is associated with it by calling this['infowin'].open(map, this) where the map is our global map and this is the marker we are currently associating the onclick event with. 在附上信息窗口后,我们使用Google Map API的addListener附加一个onclick事件监听器,当点击标记时,我们想通过调用this['infowin'].open(map, this)与之关联的信息窗口this['infowin'].open(map, this)地图是我们的全球地图,这是我们当前将onclick事件与之关联的标记。

function addMarker(myloc) {
    var current;
    if (myloc == 'usa') current = usa;
    else if (myloc == 'brasil') current = brasil;
    else if (myloc == 'argentina') current = argentina;
    for (var i = 0; i < markers.length; i++)
    if (current.lat() === markers[i].position.lat() && current.lng() === markers[i].position.lng()) return;

    markers.push(new google.maps.Marker({
        map: map,
        position: current,
        title: myloc
    }));

    markers[markers.length - 1]['infowin'] = new google.maps.InfoWindow({
        content: '<div>This is a marker in ' + myloc + '</div>'
    });

    google.maps.event.addListener(markers[markers.length - 1], 'click', function() {
        this['infowin'].open(map, this);
    });
}

When all is done we basically attach window.onload event and call the initialize function: 完成所有操作后,我们基本上附加了window.onload事件并调用initialize函数:

window.onload = initialize;

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

相关问题 如何在Google地图上将标记的像素位置映射到经度和纬度 - How to map the pixel location of a marker on Google Map to Latitude and Longitude 如何在一个 map 中显示 3 个纬度和经度(标记)? - how to display 3 latitude & longitude (Marker) in one map? 谷歌地图添加标记到地图,然后在Ruby on Rails中存储纬度和经度 - Google Maps Add a Marker to Map, then Store Latitude and Longitude in Ruby on Rails 从数据库中获取多个经度并在Google地图上显示其标记 - fetch multiple latitude and longitude from database and display their marker on google map 避免在Google地图上加载相同纬度和经度的标记 - Avoid loading marker with the same latitude and longitude on google map Laravel和谷歌地图:foreach纬度/经度显示标记或地图 - Laravel and google maps : foreach latitude/longitude show marker or map 移动谷歌地图标记点时,使用纬度和经度更新数据库 - Update db with latitude and longitude when google map marker point is move 将自定义的Google地图标记移至纬度和经度上方 - Move customized google map marker above the latitude and longitude 使用标记从Google地图获取纬度/经度坐标 - Get latitude/longitude coordinates from a Google map using a marker 如何在反应中使用 Highcharts 绘制带有经度和纬度的气泡 map - How to draw a bubble map with longitude and latitude using Highcharts in react
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM