program define gcmap, sortpreserve

version 9.2
syntax 											///  no vars, works like geocode
	using 										///  filename
	,     										///  required options
	LATitude(varname)							///  variable containing latitude
	LONGitude(varname)							///  variable containing longitude
	[											///  optional options
	replace 									///  replace using output file
	zoom(real 10) 								///  zoom level for map
	name(varname)								///  variable containing name of location
	centerlat(string)							///  latitude to center map on, if not specified mean of latitude
	centerlong(string)							///  longitude to center map on, if not specified mean of longitude
	NORmark(string)                             ///  normal marker
	SELmark(string)								///  selected marker
	]
*open file
tempname   myfile
file open `myfile' `using' , write text `replace'
*write
file write `myfile' ///
		`"<html> "' _n  ///
		`"	  <head> "' _n  ///
		`"		<meta name="viewport" content="initial-scale=1.0, user-scalable=yes"> "' _n  ///
		`"		<meta charset="utf-8"> "' _n  ///
		`"		<style> "' _n  ///
		`"		  html, body, #map-canvas { "' _n  ///
		`"			height: 100%; "' _n  ///
		`"			width: 100%; "' _n  ///
		`"			margin: 1px; "' _n  ///
		`"			padding: 1px "' _n  ///
		`"		  } "' _n  ///
		`"		</style> "' _n  ///
		`" 		<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&amp;sensor=false"></script> "' _n ///
		`" 		<script type="text/javascript" src="https://www.google.com/jsapi"></script> "' _n ///
		`" 		<script type="text/javascript"> "' _n ///
 		`"      google.load("visualization", "1.1", {packages:["map"]}); "' _n ///
		`"      google.setOnLoadCallback(drawChart); "' _n ///
		`"      function drawChart() { "' _n ///
		`"      var data = google.visualization.arrayToDataTable([ "' _n
 *add data
 if "`name'"!="" file write `myfile' ///
         `"		['Lat', 'Long', 'Name'], "' _n
 else file write `myfile' ///
         `"		['Lat', 'Long'], "' _n
 qui count
 local lastrow `r(N)'
 forval i = 1/`lastrow' {
	local Lat = `latitude'[`i']
	local Lng = `longitude'[`i']
	if `"`Lng'"' == "" | `"`Lat'"' == "." | `"`Lat'"' == "" | `"`Lng'"' == "."  {
		disp `"Data in row `i' missing locational data, will not be displayed"'
		}
	else {
		if "`name'"!=""{
			local Nme = `name'[`i']
			file write `myfile'   ///
				`"			[`Lat', `Lng','`Nme'']"'
			}
		if "`name'"==""{
			file write `myfile'   ///
				`"			[`Lat', `Lng']"'
			}
		if `i' < `lastrow'  file write `myfile' ","	_n
	}
 }
* Center map on latitude and longitude
if "`centerlat'"=="" & "`centerlong'"=="" {
	qui summ `latitude'
	local centerlat = r(mean)
	qui summ `longitude'
	local centerlong = r(mean)
}
* Finish writing options
file write `myfile' _n ///
	`"       ]); "' _n ///
		`"  var options = { "' _n ///
		`" zoomLevel: `zoom', "' _n ///
		`" center: new google.maps.LatLng(`centerlat',  `centerlong'), "' _n ///
		`"  showTip: true, "' _n  ///
		`"  mapTypeIds: ['styledMap'], "' _n ///
		`"  useMapTypeControl: false, "' _n ///
		`"  mapType: 'styledMap', "' _n ///
		`"  maps: { "' _n ///
		`"  styledMap: { "' _n ///
		`"  name: 'Styled Map',  "' _n ///
		`"  styles: [ "' _n ///
 		`"              "' _n ///
 		`"       ]}}     "' _n ///
 		`"                        , "' _n ///
		`"		icons: { "' _n ///
		`" default: { "' _n ///
		`" normal: '`normark'', "' _n ///
		`" selected: '`selmark'' "' _n ///
		`"		} "' _n ///
		`"		} "' _n ///
        `" }; "' _n ///
		`" "' _n ///
        `" var map = new google.visualization.Map(document.getElementById('map-canvas2')); "' _n ///
        `" map.draw(data, options); "' _n ///
      `" } "' _n ///
      `" </script> "' _n ///
		`" </head> "' _n ///
		`"  <body> "' _n ///
		`"	<div id="map-canvas2" style = "width: 100%; height: 100%"> </div> "' _n ///
		`" </body> "' _n ///
		`"</html> "' _n
file close `myfile'

* Link to file
local filenm = trim(subinstr(`"`using'"',`"using"',`""',.))
local filenm : list clean local(filenm)
display as text `"Open file: "' `"{browse `"`filenm'"'}"'
end