2016-11-08 10 views
-1

google api mit Javascript

var map; 
 

 
function initMap() { 
 
    map = new google.maps.Map(document.getElementById('map'), { 
 
    center: { 
 
     lat: 30.3434, 
 
     lng: 30.234 
 
    }, 
 
    zoom: 8 
 
    }); 
 
}
/* Always set the map height explicitly to define the size of the div 
 
     * element that contains the map. */ 
 

 
#map { 
 
    height: 100%; 
 
} 
 
/* Optional: Makes the sample page fill the window. */ 
 

 
html, 
 
body { 
 
    height: 100%; 
 
    margin: 0; 
 
    padding: 0; 
 
}
<div id="map"></div> 
 
<script src="https://maps.googleapis.com/maps/api/js?key=&callback=initMap" async defer></script>

zur Eingabe von Koordinaten automatisch api Java Script to google? hier ist der Code

<!DOCTYPE html> 
<html> 
    <head> 
    <title>Simple Map</title> 
    <meta name="viewport" content="initial-scale=1.0"> 
    <meta charset="utf-8"> 
    <style> 
     /* Always set the map height explicitly to define the size of the div 
     * element that contains the map. */ 
     #map { 
     height: 100%; 
     } 
     /* Optional: Makes the sample page fill the window. */ 
     html, body { 
     height: 100%; 
     margin: 0; 
     padding: 0; 
     } 
    </style> 
    </head> 
    <body> 
    <div id="map"></div> 
    <script> 
     var map; 
     function initMap() { 
     map = new google.maps.Map(document.getElementById('map'), { 
      center: {lat:30.3434, lng:30.234}, 
      zoom: 8 
     }); 
     } 
    </script> 
    <script src="https://maps.googleapis.com/maps/api/js?key=&callback=initMap" 
    async defer></script> 
    </body> 
</html> 

i auf die „Mitte“ will die Koordinaten aus einer txt-Datei auf meinem PC zu nehmen um als Test

+1

Können Sie Ihre Frage klären? Sind Sie bereit zu wissen, wo Sie Ihre Koordinaten schreiben sollten? –

+0

durch automatisch meinst du ** dynamisch ** oder? –

+1

Wissen Sie, wie man eine Datei lädt? – epascarello

Antwort

0

Angenommen, Ihre Datei ist location.txt und der Inhalt ist so etwas wie: lat;long, z 30.3434;30.234

Sie können tun, was Sie wollen mit jQuery:

function initMap() { 
    var myLat, myLng, myMap; 
    $.ajax({ 
     url : "location.txt", 
     dataType: "text", 
     success : function (data) { 
      var location = data.split(";"); 
      myLat=location[0]; 
      myLng=location[1] 
     }, 
     error: function(data) { 
      console.log("unable to retrieve data from location.txt") 
     }, 
     complete: function(data) { 
      //Called after success and error callbacks are executed. 
      myMap = new google.maps.Map(document.getElementById('map'), { 
       center: {lat:myLat, lng:myLng}, 
       zoom: 8 
      }); 
     } 
    }); 
}