2013年4月12日金曜日

Google Map で指定した緯度経度からずらしてアイコンを表示したい

カスタムOverlayViewを使うと出来る。
function custom_marker(map, lat, lng) {
  this.lat_ = lat;
  this.lng_ = lng;
  this.setMap(map);
}

// OverlayView を継承
custom_marker.prototype = new google.maps.OverlayView();
// draw 処理
custom_marker.prototype.draw = function() {
  if (!this.div_) {
    // 出力したい要素を生成
    this.div_ = document.createElement("div");
    this.div_.style.position = "absolute";
    this.div_.innerHTML = "<img src='images/custom_marker.png' alt='' />";
    var panes = this.getPanes();
    panes.overlayLayer.appendChild(this.div_);
  }
  // 緯度経度を Pixel に変換
  var point = this.getProjection().fromLatLngToDivPixel(
    new google.maps.LatLng(this.lat_, this.lng_)
  );
  // 100px ずらす
  this.div_.style.left = point.x - 100 + 'px';
  this.div_.style.top  = point.y - 100 + 'px';
}
// remove 処理
custom_marker.prototype.remove = function() {
  if (this.div_) {
    this.div_.parentNode.removeChild(this.div_);
    this.div_ = null;
  }
}

var cm = new custom_marker(map, 35.689487, 139.691706);

PHPとVB.NETでデータを加工してやり取りする

生データが嫌だけどお手軽にやりたいならビット反転して送るとか。

PHP側
$filename = "hoge.txt";
if ($handle = fopen($filename, "r")) {
  while (!feof($handle)) {
    $s = fread($handle, 1024);
    echo ~$s;
  }
}
fclose($handle);

VB.NET側
Dim filename As String = "hoge.txt";
Dim contents As String = "";
Dim bs(1024 - 1) As Byte
Dim readSize As Integer
Dim fs As New System.IO.FileStream(filename, IO.FileMode.Open, IO.FileAccess.Read)

While True
  readSize = fs.Read(bs, 0, bs.Length)
  If readSize = 0 Then
    Exit While
  End If
  For i As Integer = 0 To bs.Length
    bs(i) = Not bs(i)
  Next
  contents += System.Text.Encoding.UTF8.GetString(bs)
End While
fs.Close()

PHPでマルチバイトを含んだディレクトリのリストアップ

PHPでディレクトリをリストアップして、aタグでパラメータとして渡せるようにしてみる。
<?php

$dir = "/hoge/fuga";
if (isset($_REQUEST["dir"]) && $_REQUEST["dir"]) {
  $dir = mb_convert_encoding($_REQUEST["dir"], "SJIS", "UTF-8");
}
$html = "";

if ($dh = opendir($dir)) {
  while ($entry = readdir($dh)) {
    if (is_dir($dir . DIRECTORY_SEPARATOR . $entry) && $entry !== "." && $entry !== "..") {
      $html .= 
        "<a href=\"dir.php?dir=" . urlencode(mb_convert_encoding($entry, "UTF-8", "SJIS")) . "\">" . 
        mb_convert_encoding($entry, , "UTF-8", "SJIS") . 
        "</a><br />";
    }
  }
}

?><html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  </head>
  <body>
    <?php echo $html; ?>
  </body>
</html>

HTMLの特定の場所までスクロール

jQuery使うと以下の感じ。
var target = $("#target_element");
if (target) {
  var targetOffset = target.offset().top;
  $('html,body').animate({scrollTop: targetOffset}, 400);
}

iOS端末の縦横判別

JavaScriptでiOSの縦横判別をする。
if (Math.abs(window.orientation) === 90) {
 // 横
} else {
 // 縦
}

PHPでユーザーエージェント判別

$ua = $_SERVER['HTTP_USER_AGENT'];
switch (true) {
 case (preg_match('/iPhone|iPad/', $ua)):
  // iOS
  break;
 case (preg_match('/Android/', $ua)):
  // Android
  break;
 case (preg_match('/MSIE 6/', $ua)):
  // IE6
  break;
 case (preg_match('/MSIE 7/', $ua)):
  // IE7
  break;
 case (preg_match('/MSIE 8/', $ua)):
  // IE8
  break;
 case (preg_match('/MSIE 9/', $ua)):
  // IE9
  break;
 case (preg_match('/Firefox/', $ua)):
  // Firefox
  break;
 case (preg_match('/Chrome/', $ua)):
  // Chrome
  break;
 case (preg_match('/Opera/', $ua)):
  // Chrome
  break;
 case (preg_match("/Safari/", $agent)) && (preg_match("/Win/", $agent)):
  // Windows Safari
  break;
 case (preg_match("/Safari/", $agent)) && (preg_match("/Mac/", $agent)):
  // Mac Safari
  break;
 case ((preg_match("/MSIE/", $agent)) && (preg_match("/Win/", $agent))):
  // Windows IE
  break;
 case ((preg_match("/MSIE/", $agent)) && (preg_match("/Mac/", $agen))):
  // Mac IE
  break;
 default:
  // Other
  break;
}