Я хочу создать страницу, на которой пользователь продолжает вводить значения широты и долготы, и эти места отображаются на карте, и получается маршрут между ними.
Изначально я пытаюсь нанести маркеры на карта, принимая значения широты и долготы от пользователя.
Мой html выглядит так:
<div>
<label for = "lat">Enter Latitude</label>
<input id="lat" type="text" name="lat">
<label for = "lat">Enter Longitude</label>
<input id="lng" type="text" name="lng">
<button id ="submit">Submit</button>
</div>
<div id="map"></div>
Мой css выглядит так:
<style>
/*Set the height explicitly.*/
#map {
height: 400px;
width: 100%
}
</style>
И я использую Google Map javascript вот так.
<script src="https://maps.googleapis.com/maps/api/js?key=myAPIKey&callback=initMap" async defer></script>
И мой скрипт такой:
<script>
var map=0;
function initMap(){
var pesCollege = {lat:12.9345, lng:77.5345};
map = new google.maps.Map(document.getElementById('map'), {
center:pesCollege,
zoom: 10
});
console.log("Inside initMap");
console.log(map); //this is printing map properties
return map;
// var marker = new google.maps.Marker({position: pesCollege, map : map});
//This marker works if uncommented!
}
plot();
function plot(){
console.log("inside plot function")
console.log(map); //this is printing 0, i.e. global variable map is not
//loaded with map inside initMap
// var latlng = new google.maps.LatLng(latitude, longitude);
var pesCollege = {lat:12.9345, lng:77.5345};
var marker = new google.maps.Marker({position: pesCollege, map :map});
//I want this marker to work.
// var marker = new google.maps.Marker({position: latlng,setMap: map});
console.log("Marker set")
}
Если это сработает, то я думаю, что могу передать широту и значения долготы для построения маркеров путем добавления прослушивателя событий, например:
var button = document.getElementById('submit')
button.addEventListener("click", function(){
var dict = {};
var latitude = document.getElementById('lat').value;
var longitude = document.getElementById('lng').value;
x = parseInt(latitude);
y = parseInt(longitude);
dict['lat']=latitude;
dict['lng']=longitude;
var cdns = JSON.stringify(dict);
// alert("hi");
alert(cdns);
console.log(dict);
console.log(cdns);
plotPoint(x,y); //this is a similar funtion to plot() but i send l&l values, not sure if i can pass them like this.
plot();
});
, а мой plotPoint (x, y) - вот так
function plotPoint(latitude,longitude){
var latlng = new google.maps.LatLng(latitude, longitude);
var marker = new google.maps.Marker({position: latlng,map: map});
console.log("Marker set")
}
Я уже много читал git ответы, но никто не напоминал мою проблему. Любая помощь будет большой. Заранее спасибо.