Does not show google maps marker

0

Good morning!

I hope you can help me, the php file shows me the google map with the correct latitude and longitude, this data comes from MySQL. If the map is displayed according to this data, there is only one DETAIL that does not show the marker (indicator) on the map. Annex the code that I have .. Thank you!

<?php
if($data->latitud=="" && $data->longitud==""){
	$data->latitud="27.470182279368984";
	$data->longitud="-99.50761556625366";
}else{


}
?>
<script src="http://maps.googleapis.com/maps/api/js?key=AIzaSyDY0kkJiTPVd2U7aTOAwhc9ySH6oHxOIYM&sensor=false"></script>
<script>
var map;
var myCenter = new google.maps.LatLng(<?php echo $data->latitud; ?>, <?php echo $data->longitud; ?>);
var marker = new Array();
var infowindow;
function initialize(){
	var mapProp = {
  	center:myCenter,
  	zoom:16,
  	mapTypeId:google.maps.MapTypeId.ROADMAP
  };

  map = new google.maps.Map(document.getElementById("googleMap"),mapProp);

  google.maps.event.addListener(map, 'click', function(event) {
	  /*map.removeMarker();
	  map.clearMarkers();*/
    placeMarker(event.latLng);
  });
}

// para poner un marcador
function placeMarker(location) {
 if (!marker || !marker.setPosition) {
    marker = new google.maps.Marker({
      position: location,
      map: map,

   });
  } else {
    marker.setPosition(location);
  }
   
if (!!infowindow && !!infowindow.close) {
    infowindow.close();
  }
  infowindow = new google.maps.InfoWindow({
    content: 'Latitud: ' + location.lat() + '<br>Longitud: ' + location.lng()
  });
  infowindow.open(map, marker);

  $("#latitud").val(location.lat());
  $("#longitud").val(location.lng());
}

google.maps.event.addDomListener(window, 'load', initialize);
</script>
    
asked by Viri 02.12.2016 в 22:41
source

2 answers

1

Error:

  

The marker is added only by doing click on the map and you are indicating that the coordinates are relative to the position where the user made click .

Solution:

Call placeMarker immediately after creating the map and indicate that your coordinates are myCenter .

For example:

<?php
if($data->latitud=="" && $data->longitud==""){
    $data->latitud="27.470182279368984";
    $data->longitud="-99.50761556625366";
}else{


}
?>
<script src="http://maps.googleapis.com/maps/api/js?key=AIzaSyDY0kkJiTPVd2U7aTOAwhc9ySH6oHxOIYM&sensor=false"></script>
<script>
var map;
var myCenter = new google.maps.LatLng(<?php echo $data->latitud; ?>, <?php echo $data->longitud; ?>);
var marker = new Array();
var infowindow;
function initialize(){
    var mapProp = {
    center:myCenter,
    zoom:16,
    mapTypeId:google.maps.MapTypeId.ROADMAP
  };

  map = new google.maps.Map(document.getElementById("googleMap"),mapProp);
  placeMarker(myCenter);
}

// para poner un marcador
function placeMarker(location) {
 if (!marker || !marker.setPosition) {
    marker = new google.maps.Marker({
      position: location,
      map: map,

   });
  } else {
    marker.setPosition(location);
  }

if (!!infowindow && !!infowindow.close) {
    infowindow.close();
  }
  infowindow = new google.maps.InfoWindow({
    content: 'Latitud: ' + location.lat() + '<br>Longitud: ' + location.lng()
  });
  infowindow.open(map, marker);

  $("#latitud").val(location.lat());
  $("#longitud").val(location.lng());
}

google.maps.event.addDomListener(window, 'load', initialize);
</script>

Demo here

    
answered by 03.12.2016 в 12:13
0

Remove only addlistener and place placeMarker (event.latLng) if you want to add events to the scoreboard you should check the official documentation

link

<?php
if($data->latitud=="" && $data->longitud==""){
	$data->latitud="27.470182279368984";
	$data->longitud="-99.50761556625366";
}else{


}
?>
<script src="http://maps.googleapis.com/maps/api/js?key=AIzaSyDY0kkJiTPVd2U7aTOAwhc9ySH6oHxOIYM&sensor=false"></script>
<script>
var map;
var myCenter = new google.maps.LatLng(<?php echo $data->latitud; ?>, <?php echo $data->longitud; ?>);
var marker = new Array();
var infowindow;
function initialize(){
	var mapProp = {
  	center:myCenter,
  	zoom:16,
  	mapTypeId:google.maps.MapTypeId.ROADMAP
  };

  map = new google.maps.Map(document.getElementById("googleMap"),mapProp);

  placeMarker(event.latLng);
 
}

// para poner un marcador
function placeMarker(location) {
 if (!marker || !marker.setPosition) {
    marker = new google.maps.Marker({
      position: location,
      map: map,

   });
  } else {
    marker.setPosition(location);
  }
   
if (!!infowindow && !!infowindow.close) {
    infowindow.close();
  }
  infowindow = new google.maps.InfoWindow({
    content: 'Latitud: ' + location.lat() + '<br>Longitud: ' + location.lng()
  });
  infowindow.open(map, marker);

  $("#latitud").val(location.lat());
  $("#longitud").val(location.lng());
}

google.maps.event.addDomListener(window, 'load', initialize);
</script>
    
answered by 02.12.2016 в 23:18