Why is my Google Maps Marker not displaying? [closed] - javascript

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last month.
Improve this question
Why can't I add a marker to Google Maps on my page? I am working with Google Maps API. I don't know what the problem is. Here is my code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=API&callback=initMap" ></script>
</head>
<body>
<div class="location"style="height: 300px; width: 600px;"></div>
<script>
let loc = document.querySelector(".location");
window.onload=loadMap
function loadMap(){
const myLatLng = { lat: 50.488057309872424, lng: 30.47287431851951 };
let options={
center:myLatLng,
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
let map=new google.maps.Map(loc,options)
let marker= new google.maps.Marker({
positon: myLatLng,
map:map,
title: "Это ВЫ!"
});
}
</script>
</body>
</html>
I read Google documentation but I can't find my mistake.

A corrected example that will display the marker once the correct apikey is used. The issue was the incorrect spelling of position... also removed the window.onload=loadMap as that was redundant if using the callback parameter in the script url. Adding async and defer to the script url should help ensure that the dom is loaded before attempting to run the javascript code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
.location {
width: 600px;
height: 300px;
}
</style>
</head>
<body>
<div class="location"></div>
<script>
function loadMap() {
const myLatLng = {
lat: 50.488057309872424,
lng: 30.47287431851951
};
let options = {
center: myLatLng,
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
let map = new google.maps.Map(document.querySelector(".location"), options);
let marker = new google.maps.Marker({
position: myLatLng,
map: map,
title: ""
})
}
</script>
<script async defer src='//maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=loadMap'></script>
</body>
</html>

Related

How to add an array of markers using Lat/Lng - google maps api

Would someone be able to explain what I need to do to add an array of markers using javascript. I understand that basics of displaying the map and adding a marker or even multiple markers but they have to be hard coded in. I want to display a list of markers from an API.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h3>test</h3>
<div id="map"></div>
<style>
#map {
height: 800px;
width: 100%;
}
</style>
<script>
// Initialize and add the map
function initMap() {
// The location of L.A
var L.A = {
lat: 34.0503743841965,lng: -118.24525401223457
};
// The map, centered at L.A
var map = new google.maps.Map(document.getElementById("map"), {
zoom: 8,
center: L.A,
mapId: "hidden"
});
//here is where i am having trouble
//how can i turn this into an array of lat/lng that add a marker for each lat/lng?
var markerView = new google.maps.marker.AdvancedMarkerView({
map: map,
position: {
lat: 37.4239163,
lng: -122.0947209
},
});
}
window.initMap = initMap;
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=hiddenE&v=beta&libraries=marker&callback=initMap">
</script>
</body>
</html>

Javascript use onclick and geolocation to retrieve current location

I have to add a click handler to the button provided that will retrieve the users current location using the geolocation api.
Here's my code, I'm trying to use geolocation to set the current location of the user to the map but the button for some reasons does not work. Can anyone point out what I'm doing wrong and help me out?
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://unpkg.com/leaflet#1.3.4/dist/leaflet.css" integrity="sha512-puBpdR0798OZvTTbP4A8Ix/l+A4dHDD0DGqYW6RQ+9jxkRFclaxxQb/SJAWZfWAkuyeQUytO7+7N4QKrDh+drA==" crossorigin=""/>
<script src="https://unpkg.com/leaflet#1.3.4/dist/leaflet.js" integrity="sha512-nMMmRyTVoLYqjP9hrbed9S+FzjZHW5gY1TWCHA5ckwXZBadntCNs8kEqAWdrb9O7rxbCaA4lKTIWjDXZxflOcA==" crossorigin=""></script>
<style>
#mapid { height: 600px; }
</style>
<script>
const Mapping = {
map : null,
initializeMap : () => {
Mapping.map = L.map('mapid').setView([51.505, -0.09], 13);
L.tileLayer( 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© OpenStreetMap',
subdomains: ['a','b','c']
}).addTo( Mapping.map );
},
resetLocation : ({lat,lon}) => {
Mapping.map.setView([lat,lon], 13);
}
}
window.onload = () => {
Mapping.initializeMap();
userCode();
}
function userCode() {
// JS CODE START
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
let pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
Mapping.setPosition(pos);
Mapping.open(Mapping.map);
Mapping.map.setPosition(pos);
})
}
// JS CODE END
}
</script>
</head>
<body>
<!-- HTML CODE GOES HERE-->
<button onclick="userCode()">Get Location</button>
<div id="mapid" style="width: 600px; height: 400px;"></div>
</body>
</html>
Use
Mapping.resetLocation({lat:pos.lat,lon:pos.lng});
Instead of
Mapping.setPosition(pos);

Google Maps Javascript : Drag one finger doesn't work on ipad with ios 9 and older

I am using Google Maps API to build a website, and using option "greedy" to pan the maps with one finger. But, it doesn't work on an iPad with iOS 9 or older. The others work well.
I also tried to listen to the map event, and only the 'click' event is detected, 'drag' event isn't listened.
<!DOCTYPE html>
<html lang="vi"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0">
</head>
<body style="overflow: hidden;margin:0;">
<div id="map" style="height: 100%;width: 100%;position: absolute;overflow: hidden;"></div>
<script>
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 10.8527, lng: 106.7476},
mapTypeControl:true,
mapTypeControlOptions:{mapTypeIds: "roadmap", style: 2, position: 3},
scaleControl:true,
zoom:13,
draggable: true,
gestureHandling:"greedy",
});
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB0QGNZHfTyzSc1piB4n3wuHRP2L4RdLys&callback=initMap">
</script>
</body>
</html>

Getting App Inventor 2 variable values in an HTML/Javascript file

I am setting up a simple map program for a user to see locations on an embedded Google map. I have the map working, but I was wondering if there is a way for the user to input coordinates in AI2 then have the map center there.
Here is the HTML file I am using to display the map.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0; padding: 0 }
#map_canvas { height: 100% }
</style>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?&sensor=true&language=en"></script>
<script type="text/javascript">
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(-34.397, 150.644),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
// Add a listener for the click event
google.maps.event.addListener(map, 'click', showPosition);
}
function showPosition(event) {
// display a marker on the map
marker = new google.maps.Marker({
position: event.latLng,
map: map,
icon: "./marker.png"
});
// print the selected position to the page title
var position = event.latLng.lat().toFixed(6) + ", " + event.latLng.lng().toFixed(6);
window.document.title = position;
}
</script>
</head>
<body onload="initialize()">
<div id="map_canvas" style="width:100%; height:100%"></div>
</body>
</html>
Yes, that's possible.
You can use WebViewString to communicate values back and forth between your App and the WebViewer. In your App, you get and set the WebViewer.WebViewString properties. In your webviewer, you open to a page that has Javascript that references the window.AppInventor object, using its getWebViewString() and setWebViewString(text) methods.
See also the following snippet for a complete example.
<!doctype html>
<head>
<meta name="author" content="puravidaapps.com">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Test</title>
</head>
<body>
<script>
document.write("The value from the app is<br />" + window.AppInventor.getWebViewString());
window.AppInventor.setWebViewString("hello from Javascript")
</script>
</body>
</html>

Google Map for Android

I am new to mobile programming and trying out the google map v3 tutorial,
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<script type="text/javascript">
function initialize() {
alert('init');
var latlng = new google.maps.LatLng(-34.397, 150.644);
var myOptions = {
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
}
var myKey = "mykey";
function loadScript() {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = "https://maps.googleapis.com/maps/api/js?key="+myKey+"&sensor=false&callback=initialize";
document.body.appendChild(script);
}
</script>
</head>
<body onload="loadScript()">
<div id="map_canvas" style="width: 400px; height: 400px">
</div>
</body>
</html>
I tried this using my computer in netbean, everything works, but when i download it into my android device (as an application), the callback function ( initialize ) is never called. Anyone know what is the problem to this?
Thanks
First off all download phone gap plugin
<!DOCTYPE html>
<html>
<head>
<title>Simple Map</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
html, body, #map-canvas {
height: 100%;
margin: 0px;
padding: 0px
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
var map;
function initialize() {
var mapOptions = {
zoom: 8,
center: new google.maps.LatLng(-34.397, 150.644),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map-canvas"></div>
</body>
</html>
try to run the above code in the mobile/emulator ,then you can have sample map displayed

Categories