Google maps api 3 如何在谷歌地图API中添加文本标签
我目前正在开发一个谷歌地图API,并使其正常工作。不幸的是,我有一个小问题。我似乎找不到一种方法把标签或文字放在标记旁边。这是我的密码。我下载了一个JavaScriptMapLabel,但它似乎不起作用,或者可能是我把它放错地方了 我还在学习Javascript和谷歌API,希望你能帮助我 谢谢Google maps api 3 如何在谷歌地图API中添加文本标签,google-maps-api-3,Google Maps Api 3,我目前正在开发一个谷歌地图API,并使其正常工作。不幸的是,我有一个小问题。我似乎找不到一种方法把标签或文字放在标记旁边。这是我的密码。我下载了一个JavaScriptMapLabel,但它似乎不起作用,或者可能是我把它放错地方了 我还在学习Javascript和谷歌API,希望你能帮助我 谢谢 var地理编码器; 函数初始化(){ geocoder=新的google.maps.geocoder(); var latlng=new google.maps.latlng(58.5452824,
var地理编码器;
函数初始化(){
geocoder=新的google.maps.geocoder();
var latlng=new google.maps.latlng(58.5452824,-92.4986259);
变量myOptions={
缩放:15,
中心:拉特林,
mapTypeControl:false
}
map=new google.maps.map(document.getElementById(“map_canvas”),myOptions);
代码地址();
}
变量图标={
url:'images/map_icon.png'
};
var mapLabel=新的mapLabel({
文本:“测试”,
位置:新google.maps.LatLng(34.515233,-100.918565),
地图:地图,
尺码:35,
对齐:“右”
});
函数代码地址(){
var address=“地址测试”;
geocoder.geocode({'address':address},函数(结果,状态){
if(status==google.maps.GeocoderStatus.OK){
map.setCenter(结果[0].geometry.location);
var marker=new google.maps.marker({
地图:地图,
地址:地址:,
图标:图标,
位置:结果[0]。几何体。位置
});
}
否则{
警报(“地理编码因以下原因未成功:“+状态”);
}
});
}
jQuery(文档).ready(函数(){
initialize();//运行映射
});
#地图画布{
宽度:100%;
高度:400px;
}
联系我们
代码的问题是,您在初始化功能之外创建地图标签,因此它是在定义地图之前创建的(创建地图标签之后,在页面加载时初始化运行)。将该代码移到initialize函数中
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(58.5452824, -92.4986259);
var myOptions = {
zoom: 15,
center: new google.maps.LatLng(34.515233, -100.918565), // latlng,
mapTypeControl: false
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var mapLabel = new MapLabel({
text: 'Test',
position: new google.maps.LatLng(34.515233, -100.918565),
map: map,
fontSize: 35,
align: 'right'
});
codeAddress();
}
工作代码段:
var地理编码器,map;
函数初始化(){
geocoder=新的google.maps.geocoder();
var latlng=new google.maps.latlng(58.5452824,-92.4986259);
变量myOptions={
缩放:15,
中心:新的google.maps.LatLng(34.515233,-100.918565),//LatLng,
mapTypeControl:false
};
map=new google.maps.map(document.getElementById(“map_canvas”),myOptions);
//代码地址();
var mapLabel=新的mapLabel({
文本:“测试”,
位置:新google.maps.LatLng(34.515233,-100.918565),
地图:地图,
尺码:35,
对齐:“右”
});
}
变量图标={
url:'images/map_icon.png'
};
函数代码地址(){
var address=“地址测试”;
地理编码({
“地址”:地址
},功能(结果、状态){
if(status==google.maps.GeocoderStatus.OK){
map.setCenter(结果[0].geometry.location);
var marker=new google.maps.marker({
地图:地图,
地址:地址:,
图标:图标,
位置:结果[0]。几何体。位置
});
}否则{
警报(“地理编码因以下原因未成功:“+状态”);
}
});
}
jQuery(文档).ready(函数(){
initialize();//运行映射
});代码>
#地图#画布{
宽度:100%;
高度:400px;
}
联系我们
@geocodezip:我认为Infobox是一个更好的解决方案。更新了maplabel第三方库当前位置的示例(并添加google测试键,以便map工作)。这个问题是在API支持标记之前提出的。