1

I want to define the longitude latitude once. Right now it only works if it's defined twice.

I've moved things around but all I've managed is a broken map. The font icon only shows when long and lat are in there twice.

Here's my code:

function initialize() {

  var mapOptions = {
    center: new google.maps.LatLng(27.86336,-101.130738),
    zoom: 16,
    disableDefaultUI: true,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };

  var map = new google.maps.Map(document.getElementById("google-map"), mapOptions);

  new google.maps.Marker({
    map: map,
    icon: {
      path: fontawesome.markers.EXCLAMATION,
      scale: 0.5,
      strokeWeight: 0,
      strokeColor: 'transparent',
      strokeOpacity: 0,
      fillColor: 'red',
      fillOpacity: 1,
    },
    clickable: false,
    position: new google.maps.LatLng(27.86336,-101.130738)
  });

}

google.maps.event.addDomListener(window, "load", initialize);

I've used the font marker solution from here: Using Icon Fonts as Markers in Google Maps V3 if that helps

Community
  • 1
  • 1
toast
  • 660
  • 1
  • 5
  • 12
  • What do you mean by "twice"? Are saying in both the map options and the marker position? If so, that lat Lon is merely an object; you can create it once and pass it to both options. – Paul Richter Feb 28 '14 at 15:52
  • 1
    just use a variable for `google.maps.LatLng`, or two variables for latitude and longitude values. – Alex Shesterov Feb 28 '14 at 15:56

1 Answers1

1

You can merely do:

function initialize() {
  var myLatLng = new google.maps.LatLng(27.86336. -101.130738);

  var mapOptions = {
    center: myLatLng,
    zoom: 16,
    disableDefaultUI: true,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };

  var map = new google.maps.Map(document.getElementById("google-map"), mapOptions);

  new google.maps.Marker({
    map: map,
    icon: {
      path: fontawesome.markers.EXCLAMATION,
      scale: 0.5,
      strokeWeight: 0,
      strokeColor: 'transparent',
      strokeOpacity: 0,
      fillColor: 'red',
      fillOpacity: 1,
    },
    clickable: false,
    position: myLatLng
  });

}

google.maps.event.addDomListener(window, "load", initialize);

As long as you declare the variable within scope you shouldn't have any issues?

ChrisSwires
  • 2,713
  • 1
  • 15
  • 28