sogou map API usage example tutorial_javascript skills
The example in this article describes the application of sogou map API, which is a very practical technique. Share it with everyone for your reference. The specific implementation method is as follows:
Map initialization
1. Add the API file referencing the map:
<script src="http://xiazai.jb51.net/201409/other/api_v2.5.1.js" type="text/javascript"></script>
2. Website initialization loading event:
window.onload = function () { var map = new sogou.maps.Map(document.getElementById("map_canvas"), {}); }
Create a div with the ID map_canvas, customize the div style, and the map will be automatically loaded when the website is running;
The specific code is as follows
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <style type="text/css"> html {height: auto;} body {height: auto;margin: 0;padding: 0;} #map_canvas {width:1000px;height: 500px;position: absolute;} @media print {#map_canvas {height: 950px;}} </style> <script src="http://xiazai.jb51.net/201409/other/api_v2.5.1.js" type="text/javascript"></script> <script> window.onload = function () { var map = new sogou.maps.Map(document.getElementById("map_canvas"), {}); } </script> </head> <body> <form id="form1" runat="server"> <div id="map_canvas"></div> </form> </body> </html>
Specify to display Mo city map
The key code is as follows:
window.onload = function () { var myOptions = { zoom: 10,center: new sogou.maps.Point(12956000, 4824875) };//城市坐标,本坐标为北京坐标 var map = new sogou.maps.Map(document.getElementById("map_canvas"), myOptions); }
Understanding map attributes
List some commonly used attributes such as: map movement, map type conversion, jumping to a designated city
The specific code is as follows
<html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title></title> <style type="text/css"> html {height: auto;} body {height: auto;margin: 0;padding: 0;} #map_canvas {width:1000px;height: 500px;position: absolute;} @media print {#map_canvas {height: 950px;}} </style> <script src="http://xiazai.jb51.net/201409/other/api_v2.5.1.js" type="text/javascript"></script> <script> var map;//创建全局变量 window.onload = function () { var myOptions = { zoom: 10, center: new sogou.maps.Point(12956000, 4824875) };//指定城市 map = new sogou.maps.Map(document.getElementById("map_canvas"), myOptions);//创建地图 } //setMapTypeId方法示例 function setMapTypeId(num) { //设置地图类型,如: //sogou.maps.MapTypeId.ROADMAP 普通地图 //sogou.maps.MapTypeId.SATELLITE 卫星地图 //sogou.maps.MapTypeId.HYBRID 卫星和路网混合地图 //map.setMapTypeId(sogou.maps.MapTypeId.HYBRID) switch (num) { case 1: map.setMapTypeId(sogou.maps.MapTypeId.ROADMAP); break; //普通地图 case 2: map.setMapTypeId(sogou.maps.MapTypeId.SATELLITE); break; //卫星地图 case 3: map.setMapTypeId(sogou.maps.MapTypeId.HYBRID); break; //卫星和路网混合地图 } } //panBy方法示例地图手动移动 function panBy(a, b) { map.panBy(a, b) } //setOptions方法示例显示指定地区 function setOptions() { //同时设置地图中心、级别、类型 map.setOptions({ center: new sogou.maps.Point(13522000, 3641093), zoom: 12, mapTypeId: sogou.maps.MapTypeId.ROADMAP }) } //setCenter方法示例 显示指定的地区 a、b为地图坐标,C为地图级别 function setCenter(a, b, c) { map.setCenter(new sogou.maps.Point(a, b), c) } //fitBounds方法示例 跳转到指定的范围内 function fitBounds() { //设置一个故宫附近的范围 var bounds = new sogou.maps.Bounds(12955101, 4824738, 12958355, 4827449); //将地图设置为可全部显示这个范围 //注:不是设置bounds为这个值,而是调整到合适的位置 map.fitBounds(bounds) } </script> </head> <body> <form id="form1" runat="server"> <input value="普通地图" onclick="setMapTypeId(1)" type="button"/> <input value="卫星地图" onclick="setMapTypeId(2)" type="button"/> <input value="卫星和路网混合地图" onclick="setMapTypeId(3)" type="button"/> <input value="向左移动" onclick="panBy(200,0)" type="button"/> <input value="向右移动" onclick="panBy(-200,0)" type="button"/> <input value="向上移动" onclick="panBy(0,200)" type="button"/> <input value="向下移动" onclick="panBy(0,-200)" type="button"/> <input value="向左上移动" onclick="panBy(200,200)" type="button"/> <input value="上海" onclick="setOptions()" type="button"/> <input value="天津" onclick="setCenter(13046000,4714250,10)" type="button"/> <input value="故宫" onclick="fitBounds()" type="button"/> <div id="map_canvas" ></div> </form> </body> </html>
Map point attributes
A very important attribute on the map. Adding drawing points to the map is a commonly used method attribute.
Sogou API provides two forms of filling in tracing points: default tracing points and dynamically added tracing points
Default tracing point added:
var location = new sogou.maps.Point(12956000, 4824875); //指定描点位置 var map = new sogou.maps.Map(document.getElementById("map_canvas"), {});//初始化地图 var marker = new sogou.maps.Marker({ position: location,//描点坐标 title: "描点",//描点名称 label: { visible: true, align: "BOTTOM" },//描点显示形式 map: map, });//添加描点到地图
Add dynamic drawing points
window.onload = function () { //初始化地图 map = new sogou.maps.Map(document.getElementById("map_canvas"), {}); //为地图添加点击事件 sogou.maps.event.addListener(map, 'click', function (event) { var marker1 = new sogou.maps.Marker({ position: event.point, map: map }); }); }
Measure distance based on two tracing points
//获取类的唯一示例 function getInstance(a) { a.hasOwnProperty("_instance") || (a._instance = new a); return a._instance } //两点相连 function Lines(myLatlng, myPoint) { var convertor = getInstance(sogou.maps.Convertor); var distance = convertor.distance(myLatlng, myPoint); //两点链接 var line = new sogou.maps.Polyline({ path: [myLatlng, myPoint], strokeColor: "#FF0000", strokeOpacity: 1.0, strokeWeight: 1, title: parseInt(distance) + "米", map: map }); }
I made a small module based on the above attributes. The code for dynamic ranging on the map is as follows:
<html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title></title> <style type="text/css"> html {height: auto;} body {height: auto;margin: 0;padding: 0;} #map_canvas {width:1000px;height: 500px;position: absolute;} @media print {#map_canvas {height: 950px;}} </style> <script src="http://xiazai.jb51.net/201409/other/api_v2.5.1.js" type="text/javascript"></script> <script> var map;var num;var Listener; //获取类的唯一示例 function getInstance(a) { a.hasOwnProperty("_instance") || (a._instance = new a); return a._instance } window.onload = function () { //初始化地图 map = new sogou.maps.Map(document.getElementById("map_canvas"), {}); } function AddCj() { var mypointh; var myPoint; num = 0; //为地图添加点击事件、点击后显示当前坐标并添加点击描点 Listener = sogou.maps.event.addListener(map, 'click', function (event) { if (num == 0) { mypointh = myPoint = event.point; //获取点击位置的坐标 } else { myPoint = mypointh; mypointh = event.point; //获取点击位置的坐标 } Lines(mypointh, myPoint); num++; }); } function DelCj() { sogou.maps.event.removeListener(Listener) } //两点相连 function Lines(myLatlng, myPoint) { var convertor = getInstance(sogou.maps.Convertor); var distance = convertor.distance(myLatlng, myPoint); //两点链接 var line = new sogou.maps.Polyline({ path: [myLatlng, myPoint], strokeColor: "#FF0000", strokeOpacity: 1.0, strokeWeight: 1, title: parseInt(distance) + "米", map: map }); placeMarker(myLatlng, parseInt(distance)); } //动态添加描点,根据指定的坐标创建描点 function placeMarker(location,jl) { var clickedLocation = location; var marker1 = new sogou.maps.Marker({ position: location, title: jl+"米", label:{visible:true,align:"BOTTOM"}, map: map }); } function Mapclear() { num = 0; map.clearAll(); } </script> </head> <body> <form id="form1" runat="server"> <input type="button" value="测距" onclick="AddCj()" /> <input type="button" value="取消测距" onclick="DelCj()" /> <input type="button" value="清空" onclick="Mapclear()" /> <div id="map_canvas" ></div> </form> </body> </html>
I hope this article will be helpful to everyone’s sogou map development

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment