var gmap; var geocoder; function initialize() { var mapOptions = { zoom: 13, center: new google.maps.LatLng(35.023319, 135.763292), mapTypeId: google.maps.MapTypeId.ROADMAP, mapTypeControl: true, mapTypeControlOptions: { style: google.maps.MapTypeControlStyle.DROPDOWN_MENU }, scaleControl: true, zoomControl: true, zoomControlOptions: { style: google.maps.ZoomControlStyle.SMALL } }; gmap = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); geocoder = new google.maps.Geocoder(); getGeolocation(true); $("#map-canvas").css('height', $("#container").css('height')); $("#address").on("focus", function() { if ($("#address").val() == "Address in English / 中文地址") { //"Enter an address in English / 请中文输入地址" $("#address").val(""); } }); $("#address").smartenter(function() { callGeodosuGeocoder(); }); $("#search").on("click", callGeodosuGeocoder); setInterval(function() { getGeolocation(false) }, 10000); } function getGeolocation(move) { return; //temporaly disabled if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(function(position) { var pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); if (gmap.currentMarker != null) { gmap.currentMarker.setMap(null); gmap.currentMarker = null; } var image = { url: './img/blue_circle.png', origin: new google.maps.Point(0, 0), anchor: new google.maps.Point(10, 10), scaledSize: new google.maps.Size(20, 20) }; gmap.currentMarker = new google.maps.Marker({ position: pos, map: gmap, icon: image, title: "You are Here" }); if (move) { gmap.setCenter(pos); } }, function() { $("#currentlocation").html("GeoLocation Unavailable/"); }); } else { $("#currentlocation").html("GeoLocation Unavailable/"); } } function callGeodosuGeocoder() { var s = document.getElementById('__universal_script_get'); if (s != null) { s.parentNode.removeChild(s); //prevent a lot of script elements created s = null; } s = document.createElement("script"); s.setAttribute("id", "__universal_script_get"); s.setAttribute("type", "text/javascript"); s.setAttribute("charset", "utf-8"); s.setAttribute("onload", "geodosuCallback()"); s.setAttribute("src", "./callGeodosu.js?address=" + encodeURI($("#address").val())); document.body.appendChild(s); } function geodosuCallback() { if (gmap.jsonret["lat"] == "" || gmap.jsonret["lon"] == "") { //Call Google Geocoder callGoogleGeocoder(gmap.jsonret["query_address"], null, null, null); } else { //Succeed if (gmap.jsonret["CompletePostalAddress"] == "banchi") { //alert(gmap.jsonret["pre_processed_postal_address"]); //Call Google Geocoder for precise result callGoogleGeocoder(gmap.jsonret["query_address"], gmap.jsonret["lat"] - 0, gmap.jsonret["lon"] - 0, gmap.jsonret["detectedType"]); } else { //Use Geodosu's result placeMarker(gmap.jsonret["lat"], gmap.jsonret["lon"], gmap.jsonret["query_address"]); gmap.jsonret = null; } } var s = document.getElementById('__universal_script_get'); if (s != null) { s.parentNode.removeChild(s); //prevent a lot of script elements created s = null; } } function callGoogleGeocoder(address, geodosulat, geodosulon, geodosulevel) { geocoder.geocode({ 'address': address }, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { var bestPos = results[0].geometry.location; var bestType = results[0].geometry.location_type; for (var i = 0; i < results.length; i++) { if (results[i].geometry.location_type == google.maps.GeocoderLocationType.ROOFTOP) { bestPos = results[i].geometry.location; bestType = results[i].geometry.location_type; } else if (bestType == google.maps.GeocoderLocationType.APPROXIMATE && results[i].geometry.location_type == google.maps.GeocoderLocationType.GEOMETRIC_CENTER) { bestPos = results[i].geometry.location; bestType = results[i].geometry.location_type; } else if (bestType == google.maps.GeocoderLocationType.GEOMETRIC_CENTER && results[i].geometry.location_type == google.maps.GeocoderLocationType.RANGE_INTERPOLATED) { bestPos = results[i].geometry.location; bestType = results[i].geometry.location_type; } } //Choose Best if (geodosulat == null || geodosulon == null) { placeMarker(bestPos.lat(), bestPos.lng(), gmap.jsonret["query_address"]); } else if (bestPos == google.maps.GeocoderLocationType.ROOFTOP) { placeMarker(bestPos.lat(), bestPos.lng(), gmap.jsonret["query_address"]); } else if (bestPos != google.maps.GeocoderLocationType.ROOFTOP && (gmap.jsonret["detectedType"] == "twoStreets" || gmap.jsonret["detectedType"] == "crossingName")) { placeMarker(geodosulat, geodosulon, gmap.jsonret["query_address"]); } else { placeMarker(bestPos.lat(), bestPos.lng(), gmap.jsonret["query_address"]); } gmap.jsonret = null; } else { if (geodosulat == null || geodosulon == null) { alert('Address Not Found / 地址未发现'); } else { placeMarker(geodosulat, geodosulon, gmap.jsonret["query_address"]); gmap.jsonret = null; } } }); } function placeMarker(lat, lon, address) { var pos = new google.maps.LatLng(lat, lon); gmap.setCenter(pos); gmap.setZoom(17); if (gmap.targetMarker != null) { gmap.targetMarker.setMap(null); gmap.targetMarker = null; } var image = { url: './img/red_marker.png', origin: new google.maps.Point(0, 0), anchor: new google.maps.Point(10, 34), scaledSize: new google.maps.Size(20, 34) }; gmap.targetMarker = new google.maps.Marker({ position: pos, map: gmap, icon: image, title: address }); google.maps.event.addListener(gmap.targetMarker, 'click', function() { if (confirm("Open Map Application? \n 打开地图应用?")) { var pos = gmap.targetMarker.getPosition(); window.open("http://maps.apple.com/?q=" + pos.lat() + "," + pos.lng()); } }); } google.maps.event.addDomListener(window, 'load', initialize);