百度地图坐标定位

使用须知:
1. 百度账号注册
2. 获取开发者ak

location.json 地点集合

[
  {
    "longitude": 111,
    "altitude": 38.99999
  },
  {
    "longitude": 111,
    "altitude": 39.9011
  },
  {
    "longitude": 111,
    "altitude": 38.900999
  },
  {
    "longitude": 110,
    "altitude": 39.99999
  },
  {
    "longitude": 111.69999,
    "altitude": 39.99999
  },
  {
    "longitude": 111.533,
    "altitude": 39.99999
  }
]
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
        body, html{width: 100%;height: 100%;margin:0;font-family:"微软雅黑";}
        #allmap{height:500px;width:100%;}
        #r-result{width:100%; font-size:14px;}
    style>
    <script src="../js/jquery.js">script>
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=yourAK">script>
    <title>城市名定位title>
head>
<body>
<div id="allmap">div>
<div id="r-result">
    经度: <input id="longitude" type="text" style="width:100px; margin-right:10px;" />
    纬度: <input id="latitude" type="text" style="width:100px; margin-right:10px;" />
    <input type="button" value="查询" onclick="theLocation()" />
div>
body>
html>


<script type="text/javascript">
    // 百度地图API功能
    var map = new BMap.Map("allmap");
    map.centerAndZoom(new BMap.Point(116.331398,39.897445),11);
    map.enableScrollWheelZoom(true);

    $.getJSON("../json/location.json", function(data){
        $.each(data, function(infoIndex, info){
            var longitude = info['longitude'];
            var altitude = info['altitude'];

            var new_point = new BMap.Point(longitude, altitude);
            var marker = new BMap.Marker(new_point);  // 创建标注
            map.addOverlay(marker);              // 将标注添加到地图中
            map.panTo(new_point);
        });
    });

    // 用经纬度设置地图中心点
    function theLocation(){
        if(document.getElementById("longitude").value != "" && document.getElementById("latitude").value != ""){
            map.clearOverlays();
            var new_point = new BMap.Point(document.getElementById("longitude").value,document.getElementById("latitude").value);
            var marker = new BMap.Marker(new_point);  // 创建标注
            map.addOverlay(marker);              // 将标注添加到地图中
            map.panTo(new_point);
        }
    }
script>

运行效果:
百度地图坐标定位_第1张图片

更多百度Map demo链接:
更多百度Map demo链接

你可能感兴趣的:(百度地图)