顯示包含「google map」標籤的文章。顯示所有文章
顯示包含「google map」標籤的文章。顯示所有文章

Google Map ~ map move event trigger on distance above 10 km

想寫一個程式是

當移動google map之後~~~~~對server端要求一些數據

但又不希望過多的小距離移動~~~而產生不必要的要求次數太多

這時就可以用經緯度來判斷上次要求數據的經緯度以及現在地圖中心的經緯度來計算距離

在判斷要不要做要求


以下是example~~~(needs jQuery)

GEvent.addListener(map, "moveend", function () {

if($(window).data('prevGetNearbyLat') != null && $(window).data('prevGetNearbyLng') != null){

var currentLat = map.getCenter().lat();
var currentLng = map.getCenter().lng();
var prevLat = $(window).data('prevGetNearbyLat');
var prevLng = $(window).data('prevGetNearbyLng');

if(calculateDistance(prevLat, prevLng, currentLat, currentLng, 'K') < 10){
return;
}

}


//do something
var lat = map.getCenter().lat();
var lng = map.getCenter().lng();

$(window).data('prevGetNearbyLat', lat);
$(window).data('prevGetNearbyLng', lng);

});



//calculate two points distance

function calculateDistance(lat1, lon1, lat2, lon2, unit) {
var radlat1 = Math.PI * lat1/180;
var radlat2 = Math.PI * lat2/180;
var radlon1 = Math.PI * lon1/180;
var radlon2 = Math.PI * lon2/180;
var theta = lon1-lon2;
var radtheta = Math.PI * theta/180;
var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
dist = Math.acos(dist);
dist = dist * 180/Math.PI;
dist = dist * 60 * 1.1515;
if (unit=="K") { dist = dist * 1.609344 }
if (unit=="N") { dist = dist * 0.8684 }
return dist;
}


distance calculator reference
http://www.zipcodeworld.com/samples/distance.js.html

Google Map ~ marker on drag end event

var goodPoint = null;


goodPoint = new GMarker(point, { draggable: true });

GEvent.addListener(goodPoint, "dragend", function() {
alert(goodPoint.getPoint().lat());
alert(goodPoint.getPoint().lng());
});

map.addOverlay(goodPoint);


reference
http://code.google.com/intl/zh-TW/apis/maps/documentation/javascript/v2/overlays.html

PHP ~ get Address from google map and get location example

這範例是用PHP從google抓地址~~~~~並把JSON轉成物件~~取值

-----------------------------------------------------------------------------------------
function getAddressFromLatLng($lat, $lng)
{
$key = GMAP_KEY;
$address = urlencode($address);
$url = "http://maps.google.com/maps/geo?ll=".$lat.",".$lng."&output=json&key=".$key;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER,0); //Change this to a 1 to return headers
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}


$data = getAddressFromLatLng(25.0482805352365, 121.56269073486328);
$data = json_decode($data);

$qqq = $data->Placemark[0]->AddressDetails->Country->AdministrativeArea->AdministrativeAreaName;
$yyy = $data->Placemark[0]->AddressDetails->Country->AdministrativeArea->Locality->LocalityName;
-----------------------------------------------------------------------------------------

$qqq = 台北市
$yyy = 松山區


reference:
http://code.google.com/intl/zh-TW/apis/maps/documentation/javascript/v2/services.html

http://php.net/manual/en/function.json-decode.php

Google Map ~ get address by latitude, longitude by URL

this url return the location's json data

http://maps.google.com/maps/geo?ll=40.479581,-117.773438&output=json

the result:
------------------------------------------------------------------------------------------------

{
"name": "40.479581,-117.773438",
"Status": {
"code": 200,
"request": "geocode"
},
"Placemark": [ {
"id": "p1",
"address": "Unnamed Rd, 伊姆利, 內華達州, 美國",
"AddressDetails": {
"Accuracy" : 6,
"Country" : {
"AdministrativeArea" : {
"AdministrativeAreaName" : "NV",
"SubAdministrativeArea" : {
"Locality" : {
"LocalityName" : "伊姆利",
"Thoroughfare" : {
"ThoroughfareName" : "Unnamed Rd"
}
},
"SubAdministrativeAreaName" : "潘興縣"
}
},
"CountryName" : "USA",
"CountryNameCode" : "US"
}
},
"ExtendedData": {
"LatLonBox": {
"north": 40.5357461,
"south": 40.5238159,
"east": -117.7105151,
"west": -117.7364980
}
},
"Point": {
"coordinates": [ -117.7228985, 40.5286167, 0 ]
}
}, {
"id": "p2",
"address": "伊姆利, 內華達州, 美國",
"AddressDetails": {
"Accuracy" : 4,
"Country" : {
"AdministrativeArea" : {
"AdministrativeAreaName" : "NV",
"SubAdministrativeArea" : {
"Locality" : {
"LocalityName" : "伊姆利"
},
"SubAdministrativeAreaName" : "潘興縣"
}
},
"CountryName" : "USA",
"CountryNameCode" : "US"
}
},
"ExtendedData": {
"LatLonBox": {
"north": 40.9611589,
"south": 40.0010190,
"east": -117.2999130,
"west": -119.3386920
}
},
"Point": {
"coordinates": [ -118.3929417, 40.5186096, 0 ]
}
}, {
"id": "p3",
"address": "潘興縣, 內華達州, 美國",
"AddressDetails": {
"Accuracy" : 3,
"Country" : {
"AdministrativeArea" : {
"AdministrativeAreaName" : "NV",
"SubAdministrativeArea" : {
"SubAdministrativeAreaName" : "潘興縣"
}
},
"CountryName" : "USA",
"CountryNameCode" : "US"
}
},
"ExtendedData": {
"LatLonBox": {
"north": 40.9611589,
"south": 39.9982940,
"east": -117.2999130,
"west": -119.3392960
}
},
"Point": {
"coordinates": [ -118.4863963, 40.5685952, 0 ]
}
}, {
"id": "p4",
"address": "內華達州, 美國",
"AddressDetails": {
"Accuracy" : 2,
"Country" : {
"AdministrativeArea" : {
"AdministrativeAreaName" : "NV"
},
"CountryName" : "USA",
"CountryNameCode" : "US"
}
},
"ExtendedData": {
"LatLonBox": {
"north": 42.0022070,
"south": 35.0018570,
"east": -114.0396480,
"west": -120.0064730
}
},
"Point": {
"coordinates": [ -116.4193890, 38.8026097, 0 ]
}
}, {
"id": "p5",
"address": "美國",
"AddressDetails": {
"Accuracy" : 1,
"Country" : {
"CountryName" : "USA",
"CountryNameCode" : "US"
}
},
"ExtendedData": {
"LatLonBox": {
"north": 71.5388000,
"south": 18.7763000,
"east": -66.8850749,
"west": 170.5957000
}
},
"Point": {
"coordinates": [ -95.7128910, 37.0902400, 0 ]
}
} ]
}

------------------------------------------------------------------------------------------------


source
http://code.google.com/intl/zh-TW/apis/maps/documentation/javascript/v2/services.html

Google Map ~ get address by latitude and longitude

this example is get address by lat, lng

---------------------------------------------------------------------------------------------------

GEvent.addListener(map, "click", getAddress);
geocoder = new GClientGeocoder();


function getAddress(overlay, latlng) {
if (latlng != null) {
address = latlng;
geocoder.getLocations(latlng, showAddress);
}
}

function showAddress(response) {
map.clearOverlays();
if (!response || response.Status.code != 200) {
alert("Status Code:" + response.Status.code);
} else {
place = response.Placemark[0];
point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
marker = new GMarker(point);
map.addOverlay(marker);
marker.openInfoWindowHtml(
'<b>orig latlng:</b>' + response.name + '<br/>' +
'<b>latlng:</b>' + place.Point.coordinates[0] + "," + place.Point.coordinates[1] + '<br>' +
'<b>Status Code:</b>' + response.Status.code + '<br>' +
'<b>Status Request:</b>' + response.Status.request + '<br>' +
'<b>Address:</b>' + place.address + '<br>' +
'<b>Accuracy:</b>' + place.AddressDetails.Accuracy + '<br>' +
'<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode);
}
}


/*

try this in console
geocoder.getLocations(map.getCenter(), showAddress);

*/


---------------------------------------------------------------------------------------------------

source:
http://code.google.com/intl/zh-TW/apis/maps/documentation/javascript/v2/services.html

example:
http://code.google.com/intl/zh-TW/apis/maps/documentation/javascript/v2/examples/geocoding-reverse.html

Google Map ~ trigger marker click event

use function to trigger marker's onclick event

---------------------------------------------------------------------------------------------------
var markers = {};

var point = new GLatLng(x.lat, x.lng);
markers[1] = new GMarker(point);

GEvent.addListener(markers[1], "click", function () {
alert('xxxxxxxxx');
});


GEvent.trigger(markers[id], 'click');
---------------------------------------------------------------------------------------------------

reference
http://stackoverflow.com/questions/2730929/how-to-trigger-the-onclick-event-of-a-marker-on-a-google-maps-v3

Google Map ~ on map moved event

this is an example, when move map alert map's latitude, longitude

GEvent.addListener(map, "moveend", function() {
var lat = map.getCenter().lat();
var lng = map.getCenter().lng();

alert(lat);
alert(lng);
});


reference
http://code.google.com/intl/zh-TW/apis/maps/documentation/javascript/v2/events.html

Google Map ~ get current map's latitude, longitude, getCenter()

var lat = map.getCenter().lat();

var lng = map.getCenter().lng();

reference
http://code.google.com/intl/zh-TW/apis/maps/documentation/javascript/v2/introduction.html

Google Map ~ get latitude, longitude by address or city

This is an example ~

get latitude, longitude and set marker by address, city .....etc
---------------------------------------------------------------------------------------------------

var geocoder = new GClientGeocoder();
function showAddress(address)
{
if (geocoder)
{
geocoder.getLatLng(address,function(point)
{
if (!point) {
alert(address + " not found");
} else {

map.setCenter(point,10);
var marker = new GMarker(point);

GEvent.addListener(marker, "click", function() { marker.openInfoWindowTabsHtml('Your start Address'); });
map.addOverlay(marker);
}
});
}
}
---------------------------------------------------------------------------------------------------

call function like this

showAddress('los angeles');


reference
http://itouchmap.com/latlong.html

Google Map ~ GDirections.load on failed, error Event

var gdir = new GDirections(map);

GEvent.addListener( gdir, "error", function() {
alert("Failed to direction, please re-type address."); } );

var start = $('#start').val();
var end = $('#end').val();

gdir.load("from:" + start + " to:" + end);


refernce
http://groups.google.com/group/google-maps-api/browse_thread/thread/69b71853ab6cf7a7

Google Map ~ mouse wheel scroll to zoom in, out

//enable
map.enableScrollWheelZoom();

//disable
map.disableScrollWheelZoom();

http://googlemapsapi.blogspot.com/2007/04/v278-go-ahead-scroll-your-mouse-wheels.html

Google Map ~ get Directions(Route) by GDirections and GLatLng(lat, lon)

Photobucket

this is an example to get directions by latitude, longitude

------------------------------------------------------------------------------------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=true_or_false&amp;key=YOUR_KEY" type="text/javascript"></script>
<script type="text/javascript">
function initialize() {
if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("map"));

//gdir = new GDirections(map);
gdir = new GDirections(map, document.getElementById("directions"));

var q1 = new GLatLng(24.918822,121.140747);
var q2 = new GLatLng(25.112958,121.599426);

var a = "from: "+q1.toUrlValue(6) + " to: " + q2.toUrlValue(6);
gdir.load(a, {getPolyline:true});
}
}
</script>
</head>
<body onload="initialize()">
<div id="map" style="width:600px;height:600px"></div>
<div id="directions"></div>
</body>
</html>


------------------------------------------------------------------------------------------------

reference
http://econym.org.uk/gmap/example_multi2.htm

Google Map ~ auto set Zoom and center map by markers

 var bounds = new GLatLngBounds();
 for (var i = 0; i < markers.length; i++) {
  var point = markers[i].getPoint();
  bounds.extend(point);
 }
 
 map.setZoom(map.getBoundsZoomLevel(bounds));
 map.setCenter(bounds.getCenter());


http://www.mail-archive.com/google-maps-api@googlegroups.com/msg44894.html

Google Map ~ remove/clean up all makers and add marker

//clean
map.clearOverlays();

//add
var point = new GLatLng(5566.06161,5566.29978);
var marker = createMarker(point,"markerName","markerName", 5, "");
map.addOverlay(marker);