我有一个来自json文件的地理坐标
{"location": "5.554565999999999,-0.2302829999999858"}
我想知道在谷歌地图中使用它之前我怎么能把它转换成浮动。
如果有人能帮助我,我会很高兴的,谢谢你。
答案 0 :(得分:3)
单向(有很多):
var coords = json.location.split(',');
var latLng = new google.maps.LatLng(parseFloat(coords[0]), parseFloat(coords[1]));
代码段
var geocoder;
var map;
var json = {
"location": "5.554565999999999,-0.2302829999999858"
};
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 coords = json.location.split(',');
var latLng = new google.maps.LatLng(parseFloat(coords[0]), parseFloat(coords[1]));
map.setCenter(latLng);
var marker = new google.maps.Marker({
position: latLng,
map: map
});
}
google.maps.event.addDomListener(window, "load", initialize);

html,
body,
#map_canvas {
height: 500px;
width: 500px;
margin: 0px;
padding: 0px
}

<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map_canvas" style="width:750px; height:450px; border: 2px solid #3872ac;"></div>
&#13;