﻿var map = null;
var defaultCenter = new GLatLng(23.6, 121);

var markers = new Array();

function loadMap(containerid)
{
    if(containerid == null) containerid = "my_map";
    
    if (GBrowserIsCompatible())
    {
        map = new GMap2($get(containerid), {draggableCursor:'auto', draggingCursor:'move'});
        
        map.setCenter(defaultCenter, 7);            // 初始化地圖
        map.setUIToDefault();
        
        map.disableScrollWheelZoom();				// 取消滑鼠Zoom功能
	}
    else 
    {
        $get(containerid).innerHTML = "您的瀏覽器不支援 Google Map 喔！";
    }
}

function addIcon(square, sizeX, sizeY, icon)
{
    if (icon != null) square.image = icon;
    
    square.iconSize = new GSize(sizeX, sizeY);
    square.dragCrossSize = new GSize(0, 0);
    
    if (sizeX == sizeY)
        square.iconAnchor = new GPoint(sizeX / 2, sizeY / 2);
    else
        square.iconAnchor = new GPoint(sizeX / 2, sizeY - 2);
    
    square.infoWindowAnchor = new GPoint(5, 5);
}

//================================== 定位 ==================================
function Gps(address)
{
    var myGeocoder = new GClientGeocoder();
    myGeocoder.getLatLng(address, function getRequest(point) {
                                    if (!point)
                                    {
                                        alert('這個地址 Google 說不知道喔！\n\n請嘗試移除部份地址資訊，再試試看吧...');
                                    }
                                    else
                                    {
                                        //移動地圖中心點
                                        map.panTo( point, map.setZoom(15) );
                                    }
                                  });
}

function GMapAddress(address, lng, lat)
{
    var myGeocoder = new GClientGeocoder();
    var point = new GLatLng(lng, lat);
    
    myGeocoder.getLocations(point, function getRequest(response) {
                                    if (!response || response.Status.code != 200)
                                    {
                                        address.innerHTML = "Google 查不到地址喔！";
                                    }
                                    else
                                    {
                                        // 取得地址
                                        address.innerHTML = response.Placemark[0].address;
                                    }
                                  });
}

function positionLatLng(lng, lat)
{
    try
    {
        if ( isNaN(lng) || isNaN(lat) )
        {
            alert("經緯度格式錯誤喔！");
            return;
        }
        
        var point = new GLatLng(lng, lat);

        map.setCenter(point, 16);
    }
    catch (e)
    {
        // 不處理
    }
}
//==============================================================================