JK0124
JK0124

Reputation: 395

Add Marker - Google Maps API v3

I have the following Javascript where I modified the standard Google Maps API initialize() function and the standard addMarker function. The map loads and centers fine, but the marker does not get added to the map. I looked at the previous question , but I'm not sure what I'm doing wrong..

<script type="text/javascript">
// Note that using Google Gears requires loading the Javascript
// at http://code.google.com/apis/gears/gears_init.js

var siberia = new google.maps.LatLng(60, 105);
var newyork = new google.maps.LatLng(40.69847032728747, -73.9514422416687); 
var usa = new google.maps.LatLng(44.58, -95.46); //middle of the US w 50 states     

var browserSupportFlag =  new Boolean();

function initialize() {
    var myOptions = {
    zoom: 4,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions)
  map.setCenter(usa);   
}

function getMyLocation() {
    var initialLocation= newyork;
    var myOptions = {
    zoom: 10,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

    // Try W3C Geolocation (Preferred)
    if(navigator.geolocation) {
        browserSupportFlag = true;
        navigator.geolocation.getCurrentPosition(function(position) {
        initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
        map.setCenter(initialLocation);
        addMarker(initialLocation);
    }, function() {
    handleNoGeolocation(browserSupportFlag);
    });
    // Try Google Gears Geolocation
    } else if (google.gears) {
        browserSupportFlag = true;
        var geo = google.gears.factory.create('beta.geolocation');
        geo.getCurrentPosition(function(position) {
        initialLocation = new google.maps.LatLng(position.latitude,position.longitude);
        map.setCenter(initialLocation);
        addMarker(initialLocation);
    }, function() {
        handleNoGeoLocation(browserSupportFlag);
    });
    // Browser doesn't support Geolocation
    } else {
        browserSupportFlag = false;
        handleNoGeolocation(browserSupportFlag);
    }

    function handleNoGeolocation(errorFlag) {
        if (errorFlag == true) {
            alert("Geolocation service failed.");
            initialLocation = newyork;
        } else {
             alert("Your browser doesn't support geolocation. We've placed you in Siberia.");
             initialLocation = siberia;
        }
        map.setCenter(usa);
    }

}

function addMarker(location) {
    marker = new google.maps.Marker({
    position: location,
    map: map, 
    title: "Hello World!"
    });
    //markersArray.push(marker);
}
</script>

Upvotes: 0

Views: 14910

Answers (1)

Flukey
Flukey

Reputation: 6555

So your problem is to do with scope:

Add this above your initilize function:

var map;

And in your initialize function change this:

  var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions)

to this:

  map = new google.maps.Map(document.getElementById("map_canvas"), myOptions)

You don't need to re-create the map in your getlocation function:

so remove these lines:

   var myOptions = {
    zoom: 10,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

Here is a jsfiddle with the changes:

http://jsfiddle.net/bQ3AG/

EDIT: I've just noticed when you pin point their location, you want to set the zoom level to 10.

To do this you can use: map.setZoom(10); instead of passing through new options.

Upvotes: 0

Related Questions