如何使用Google Maps JavaScript API v3解析反向地理编码的响应.
geocoder.geocode({'latLng': latlng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
infowindow.setContent(results[0].formatted_address);
infowindow.open(map, marker);
}
}
Run Code Online (Sandbox Code Playgroud)
这会在弹出窗口中显示格式化的地址,但我正在尝试从响应中取出其他位,最好是街道名称或路径(如果找不到街道名称).但是在使用时 obj = JSON.parse(json);我一直在控制台中收到此错误.
SyntaxError:JSON.parse:意外字符
如果它是PHP我会做一堆for each循环.是否可以在JavaScript中执行类似的操作?
这是一个样本
{
"results" : [
{
"address_components" : [
{
"long_name" : "131",
"short_name" : "131",
"types" : [ "street_number" ]
},
{
"long_name" : "Stubbington Avenue",
"short_name" : "Stubbington Ave",
"types" : [ "route" ]
},
{
"long_name" : "Portsmouth",
"short_name" : "Portsmouth",
"types" : [ …Run Code Online (Sandbox Code Playgroud) 如何在地图上生成对象,而不占用HTML5 Canvas上的相同空间或重叠?
在某种程度上随机生成X坐标.我想在数组内查看它是否已经存在,然后是接下来的20个值(考虑到宽度),没有运气.
var nrOfPlatforms = 14,
platforms = [],
platformWidth = 20,
platformHeight = 20;
var generatePlatforms = function(){
var positiony = 0, type;
for (var i = 0; i < nrOfPlatforms; i++) {
type = ~~(Math.random()*5);
if (type == 0) type = 1;
else type = 0;
var positionx = (Math.random() * 4000) + 500 - (points/100);
var duplicatetest = 21;
for (var d = 0; d < duplicatetest; d++) {
var duplicate = $(jQuery.inArray((positionx + d), platforms)); …Run Code Online (Sandbox Code Playgroud)