问题描述
此行不起作用,并且控制台中没有错误.
this line is not working and no error in console.
position: new google.maps.LatLng( coordsArray[i].location ),
通过alert(coordsArray[i].location);
位置确认的数据是数据库字段,返回LatLng -43.59670,172.38247作为字符串.
data confirmed by alert(coordsArray[i].location);
location been database field returning LatLng -43.59670,172.38247 as a string.
此行有效:position: new google.maps.LatLng( -43.59670,172.38247 ),
与上述相同的数据是什么,我的代码有什么问题吗?
This line works: position: new google.maps.LatLng( -43.59670,172.38247 ),
What is the same data as above, any ideas what wrong with my code?
var list_location = localStorage.getItem('myHouse');
var obj = JSON.parse(list_location);
var coordsArray = obj;
var marker;
var locX;
var image = 'http://apppics.weebly.com....png';
var map = Appery("googlemap_6").options.mapElement.gmap('get', 'map');
var CreateMarker = function(coordsArray, i){
var marker = new google.maps.Marker({
position: new google.maps.LatLng( coordsArray[i].location ),
//position: new google.maps.LatLng( -43.59670,172.38247 ),
title: coordsArray[i].storeName,
map: Appery("googlemap_6").gmap,
});
for (var i = 0, j = coordsArray.length; i < j-1; i++)
alert(coordsArray[i].location);
CreateMarker(coordsArray, i);
推荐答案
这是错误的:new google.maps.LatLng(coordsArray[i].location)
.
您说:位置已在数据库字段中返回LatLng -43.59670,172.38247作为字符串."
You state: "location been database field returning LatLng -43.59670,172.38247 as a string."
google.maps.LatLng 构造函数需要两个数字作为参数(不是逗号分隔的字符串).
the google.maps.LatLng constructor takes two numbers as arguments (not a comma separated string).
如果coordsArray[i].location
是逗号分隔的字符串,请将其转换为两个数字.
If coordsArray[i].location
is a comma separated string, convert it to two numbers.
var coords = coordsArray[i].location.split(",");
var latLng = new google.maps.LatLng(parseFloat(coords[0]), parseFloat(coords[1]));
var marker = new google.maps.Marker({
position: latLng,
map: map
});
代码段:
var geocoder;
var map;
function initialize() {
var map = new google.maps.Map(
document.getElementById("map_canvas"), {
center: new google.maps.LatLng(37.4419, -122.1419),
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var location = "-43.59670,172.38247";
coords = location.split(",");
var latLng = new google.maps.LatLng(parseFloat(coords[0]), parseFloat(coords[1]));
var marker = new google.maps.Marker({
position: latLng,
map: map
});
map.setCenter(latLng);
}
google.maps.event.addDomListener(window, "load", initialize);
html,
body,
#map_canvas {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?"></script>
<div id="map_canvas"></div>
这篇关于Google Map在数据库中解决地址问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!