I was able to put markers on the map, but since I’m using GMap2 I cant put markers on the map anymore. I want to use GMap2 so I can make lat lng out of an address.
<script src="https://maps.googleapis.com/maps/api/js?sensor=true"></script>
<script src="http://maps.google.com/maps?file=api&v=2&key=AIzaSyDrwris-0kRdY16oIQoGc_1qsdOgHbJrXo"
type="text/javascript"></script>
<script type="text/javascript">
var map = new GMap2(document.getElementById("chart"));
map.setUIToDefault();
navigator.geolocation.getCurrentPosition(function(position) {
var pos = new google.maps.LatLng(position.coords.latitude,
position.coords.longitude);
map.setCenter(pos, 14);
//until now it works fine, but when I add a marker it does nothing
var marker = new GMarker({
position: new GLatLng(52.15664,5.40567),
map: map,
title: 'NAME'
});
There are a few issues here.
Firstly, you’re including two versions of the mapping API, one of which has escaped parameters for some reason. This may be because it was copied from somewhere else. If you require the V2 API, you only need to include that one. Be aware that the API is due to be retired by Google next year.
Also, your marker instantiation code is incorrect. You should pass the latlng position as the first parameter, then the options as the second in object literal format, e.g.:
Finally, you aren’t adding the marker itself to the map. You use the
addOverlaymethod to to this.Here’s a worked solution. Press the ‘Add Marker’ button to add a marker in a random position to the map:
Note that the above assumes that geolocation works. In a real world application, you should be testing for errors as illustrated by the API documentation here:
https://developers.google.com/maps/articles/geolocation