Search the Site


bottom corner

Google Maps with input events

The following sample illustrates interaction with Google Maps using mouse clicks and text field updates.

This page is filed under keyword(s): googlemaps.

This example code updates coordinate fields based on mouse clicks on the map. Also, it can also redraw map markers upon typing in new coordinates.

    <style type="text/css">
      html { height: 100% }
      body { height: 100%; margin: 10; padding: 10 }
      #map_canvas { height: 100% }
    <script type="text/javascript" src=""></script>
    <script type="text/javascript">
      var map;
      var currMarker = new google.maps.Marker();

      function initialize() {
        var myLatLng = new google.maps.LatLng(0, 0);
        var mapOptions = {
          center: myLatLng,
          zoom: 1,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        map = new google.maps.Map(document.getElementById("map_canvas"),

        google.maps.event.addListener(map, 'click', function(event) {

      function placeMarker(location) {
        var marker = new google.maps.Marker({
            position: location,
            map: map


      function updateMarker() {
        var lat = document.getElementById("lat").value;
        var lng = document.getElementById("lng").value;
        var newLatLng = new google.maps.LatLng(lat, lng);
        var marker = new google.maps.Marker({
            position: newLatLng,
            map: map
  <body onload="initialize()">
    <div id="map_canvas" style="width:600px; height:400px"></div>
    <div id="coordinates">
      lat: <input id="lat" type="text" onchange="updateMarker()" value="0" />
      lng: <input id="lng" type="text" onchange="updateMarker()" value="0" />

Below is the code in action:

lat: lng:

Did you find this page useful? Please consider browsing other articles or subscribing to the RSS feed to keep up with latest.

This page is filed under keyword(s): googlemaps.
Author: J. Kim
Last updated: 31 Dec 2012

bottom corner