使用Mapbox static api绘制多边形

时间:2016-01-10 00:01:01

标签: api mapbox

在尝试将多边形添加到mapbox api上的静态地图时,我一直遇到message: "Invalid GeoJSON"错误。

在以下地图上:

  

https://api.mapbox.com/v4/mapbox.streets/geojson(%7B%22type%22%3A%22Feature%22%2C%22properties%22%3A%7B%7D%2C%22geometry%22%3A%7B%22type%22%3A%22LineString%22%2C%22coordinates%22%3A%5B%5B-120.8492%2C39.4916%5D%2C%5B-120.8474%2C39.4896%5D%2C%5B-120.8510%2C39.4864%5D%2C%5B-120.8492%2C39.4916%5D%5D%7D%7D),pin-s-1+f44(-120.849200,39.491600,13),pin-s-2+f44(-120.847400,39.489600,13),pin-s-3+f44(-120.851000,39.486400,13),geojson(%7B%22type%22%3A%22Feature%22%2C%22properties%22%3A%7B%22stroke-width%22%3A4%2C%22stroke%22%3A%22%23ff4444%22%2C%22stroke-opacity%22%3A0.5%7D%2C%22geometry%22%3A%7B%22type%22%3A%22LineString%22%2C%22coordinates%22%3A%5B%5B39.4916%2C%20-120.8492%5D%2C%5B39.4896%2C%20-120.8474%5D%2C%5B39.4864%2C%20-120.8510%5D%2C%5B39.4888%2C%20-120.8496%5D%5D%7D%7D)/-120.849600,39.488800,15/800x400.png?access_token=token

我的LineString示例用于勾勒出我想要绘制的多边形的边框:

geojson({"type":"Feature","properties":{},"geometry":{"type":"LineString","coordinates":[[-120.8492,39.4916],[-120.8474,39.4896],[-120.8510,39.4864],[-120.8492,39.4916]]}})

如果尝试使用以下geojson对象将其转换为多边形:

geojson({"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[-120.8492,39.4916],[-120.8474,39.4896],[-120.8510,39.4864],[-120.8492,39.4916]]}})

它爆炸了:

  

https://api.mapbox.com/v4/mapbox.streets/geojson(%7B%22type%22%3A%22Feature%22%2C%22properties%22%3A%7B%7D%2C%22geometry%22%3A%7B%22type%22%3A%22Polygon%22%2C%22coordinates%22%3A%5B%5B-120.8492%2C39.4916%5D%2C%5B-120.8474%2C39.4896%5D%2C%5B-120.8510%2C39.4864%5D%2C%5B-120.8492%2C39.4916%5D%5D%7D%7D),pin-s-1+f44(-120.849200,39.491600,13),pin-s-2+f44(-120.847400,39.489600,13),pin-s-3+f44(-120.851000,39.486400,13),geojson(%7B%22type%22%3A%22Feature%22%2C%22properties%22%3A%7B%22stroke-width%22%3A4%2C%22stroke%22%3A%22%23ff4444%22%2C%22stroke-opacity%22%3A0.5%7D%2C%22geometry%22%3A%7B%22type%22%3A%22LineString%22%2C%22coordinates%22%3A%5B%5B39.4916%2C%20-120.8492%5D%2C%5B39.4896%2C%20-120.8474%5D%2C%5B39.4864%2C%20-120.8510%5D%2C%5B39.4888%2C%20-120.8496%5D%5D%7D%7D)/-120.849600,39.488800,15/800x400.png?access_token=token

有人能看出我做错了什么吗?我在查找使用静态api绘制多边形的任何示例时遇到问题,但static api doc表示这是可能的。

感谢。

2 个答案:

答案 0 :(得分:2)

您的geojson字符串无效。你错过了坐标周围的两个方括号。这是一个有效的公式。

geojson({
            "type": "Feature",
            "properties": {},
            "geometry": {
                "type": "Polygon",
                "coordinates": [[[-120.8492, 39.4916], [-120.8474, 39.4896], [-120.8510, 39.4864], [-120.8492, 39.4916]]]
            }
    })

如果要使用多边形生成静态地图,则相应的API调用将类似于

  

https://api.mapbox.com/v4/mapbox.satellite/geojson(%7B%0A%20%20%20%20%22type%22%3A%20%22Feature%22%2C%0A%20%20%20%20%22properties%22%3A%20%7B%7D%2C%0A%20%20%20%20%22geometry%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%22type%22%3A%20%22Polygon%22%2C%0A%20%20%20%20%20%20%20%20%22coordinates%22%3A%20%5B%5B%5B-120.8492%2C%2039.4916%5D%2C%20%5B-120.8474%2C%2039.4896%5D%2C%20%5B-120.8510%2C%2039.4864%5D%2C%20%5B-120.8492%2C%2039.4916%5D%5D%5D%0A%20%20%20%20%7D%0A%7D)/-120.8492,39.4890,15/600x300@2x.png?access_token= {{your_access_token}}

您可以在此网站http://staticmapmaker.com/mapbox/

上试用

答案 1 :(得分:1)

我无法弄清楚如何使用geojson做到这一点,但意识到我可以使用polylines和fill属性来做到这一点。

我在Android polyutil class的谷歌地图的帮助下计算了我的折线。