LoginSignup
5
3

More than 5 years have passed since last update.

GoogleMapAPIv3でよく使うやつメモ。

Posted at

住所、郵便番号からマップの位置変更

// 中心変更
var changeCenter = function (address) {
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({'address': address}, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var location = results[0].geometry.location;
            var lat = location.lat();
            var lng = location.lng();
            var latlng = {lat: lat, lng: lng};
            map.setCenter(latlng);
        } else {
            console.log("Geocode was not successful for the following reason: " + status);
        }
    });
};
changeCenter('富山市');

現在地追跡

/**
 * 現在地追跡
 * @param {Function} func
 * @return {Void}
 */
watchCurrentPosition: function (func) {
    var self = this;
    if (navigator.geolocation) {
        navigator.geolocation.watchPosition(
            function (info) {
                var lat = info.coords.latitude;
                var lng = info.coords.longitude;
                var center = new google.maps.LatLng(lat, lng);
                func(center);
            },
            function (info) {
                //alert('現在地取得エラー' + info.code);
                return;
            },
            {enableHighAccuracy: true, timeout: 6000, maximumAge: 600000}
        );
    } else {
        //alert('本ブラウザではGeolocationが使えません');
        return;
    }
}

マップオプション取得

 /**
 * マップオプション取得
 * @param {google.maps.LatLng} latLng
 * @return {Object} options
 */
getMapOptions: function (latLng) {
    var options = {
        zoom: 15,
        minZoom: 12,
        maxZoom: 20,
        center: latLng,
        disableDoubleClickZoom: true,
        draggable: true,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        zoomControl: false,
        streetViewControl: false,
        mapTypeControl: false,
        mapTypeControlOptions: {
            style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
            position: google.maps.ControlPosition.TOP_RIGHT
        },
        panControl: false
    };
    return options;
}

スマホ、モバイル判定

/**
 * スマホ判定
 * @return {Boolean}
 */
isSmartPhone: function () {
    var userAgent = navigator.userAgent;
    if (userAgent.indexOf('iPhone') != -1 ||
        userAgent.indexOf('iPod') != -1 ||
        (userAgent.indexOf('Android') != -1 && userAgent.indexOf('Mobile') != -1)) {
        return true;
    }
    return false;
}
,
/**
 * モバイル判定
 * @return {Boolean}
 */
isMobile: function () {
    var userAgent = navigator.userAgent;
    if (userAgent.indexOf('iPhone') != -1 ||
        userAgent.indexOf('iPad') != -1 ||
        userAgent.indexOf('iPod') != -1 ||
        userAgent.indexOf('Android') != -1) {
        return true;
    }
    return false;
}
5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3