0
votes

JavaScript:

<script async defer src="https://maps.googleapis.com/maps/api/js?key=mykey"type="text/javascript"></script>

JavaScript Function:

<script type="text/javascript">
  var geocoder;
  var map;
  var latlng;

  function codeAddress(address) {
    geocoder = new google.maps.Geocoder();
    if (geocoder) {
      geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            latlng=results[0].geometry.location;
            latlng = new google.maps.LatLng(latlng);
            var myOptions = {
              zoom: 13,
              center: latlng,
              mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
                  map.setCenter(results[0].geometry.location);
                  var marker = new google.maps.Marker({
                      map: map, 
                      position: results[0].geometry.location
              });

        } else {
          alert("Geocode was not successful for the following reason: " + status);
        }
      });
    }
  }
</script>

I reads following questions but my problem is not solved.

Google Maps JavaScript API RefererNotAllowedMapError

Google maps API referrer not allowed

Google Maps API error: Google Maps API error: RefererNotAllowedMapError

The problem is , when i open my website like this.

http://mysiste.com/contactus

it is not showing map and showing following error.

Google Maps API error: RefererNotAllowedMapError https://developers.google.com/maps/documentation/javascript/error-messages#referer-not-allowed-map-error Your site URL to be authorized: http://comprettainsurance.com/contactus"

while if i open my site like this.

http://www.mysiste.com/contactus

the maps are working perfectly.

My Credential Page where i add domain like this.

enter image description here

so why it is not showing maps without www?

2

2 Answers

3
votes

*.comprettainsurance.com/* does not match http://comprettainsurance.com/contactus (it matches any subdomain of comprettainsurance.com, but not the domain itself).

You need to add an additional referrer line:

comprettainsurance.com/*
1
votes

Use simple like this this worked for me everywhere

<script type="text/javascript" src="http://maps.google.com/maps/api/js?key=key&libraries=places,drawing,geometry"></script>

when you use defer the script will be loaded when the document is closed- the content has been loaded. Furthermore external deffered scripts will be parsed after inline defferred scripts.