Chủ Nhật, 11 tháng 1, 2015

Google Map - Creat a map

Google Map


---o0o---


Original link

I. Creates a Google Map - when the page is loaded

1. Call API


<script src="http://maps.googleapis.com/maps/api/js"></script>
        

2. Init js function to set map properties

<script>
    function initialize() {
        var mapProp = {
            center:new google.maps.LatLng(51.508742,-0.120850),
            zoom:5,
            mapTypeId:google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById("googleMap"), mapProp);
    }
    google.maps.event.addDomListener(window, 'load', initialize);
</script>
        

3. Creat div to contain the map

<div id="googleMap" style="width:500px;height:200px;"<>/div>
        
  • initialize : Init function
  • mapProp: Creat an object to define the properties for the map
  • google.maps.Map: Creat new map object which have the mapProp properties
  • google.maps.event.addDomListener: Add an event listener to execute the initialize() function when the page is loaded

I. Creates a Google Map - after the page is loaded

1. Creat js function

 <script>
     function init()
     {
         var mapProp = {
             center: new google.maps.LatLng(51.508742,-0.120850),
             zoom:7,
             mapTypeId: google.maps.MapTypeId.ROADMAP
         };
         var map = new google.maps.Map(document.getElementById("googleMap1"),mapProp);
     }

     function loadScript()
     {
         var script = document.createElement("script");
         script.type = "text/javascript";
         script.src = "http://maps.googleapis.com/maps/api/js?callback=init";
         document.body.appendChild(script);
     }
     window.onload = loadScript;
 </script>
        
  • The loadScript() function creates the Google Maps API <script> tag
  • callback=init: this parameter to execute init() function after the API is fully loaded
  • window.onload: load the Google Maps API after the page has fully loaded

2 nhận xét:

  1. Very nice article! I'm Preeti, I write for educational blogs. I make a collection of wonderful educational blogs from where I could take inspiration for writing. This article really inspires me though it is a little different from my domain but nonetheless it is a good writing. I sometime write for a education site blogs www.clearexam.ac.in
    Let me know your thoughts if I could contribute to your blog too.

    Trả lờiXóa
  2. "This is a wonderful article. I can see that you have done a lot of research and your subject knowledge is good and impressive.
    This is Nitin, I also like to write for an education blog https://www.cleariitmedical.com/ - This is an online blog for students who want to prepare for India's most difficult entrance exams - IIT JEE and NEET This is a free blog and provide free study material, quiz and notes to those who can't afford to pay hefty fees of coaching institutes. I write free of cost for the good cause. I know you must be busy with your work but if you could take out some time from your busy schedule and have a look at it. I saw a good collection of articles. If you only write a few words about this blog then it can help a lot of students out there. Thanks!"

    Trả lờiXóa