2016-05-20 6 views
1

スタティックポイントからGoogleマップに円を描く必要があります。私はこれを達成しようとしましたを使用して描画された後に円の設定centercenter: new google.maps.LatLng(-34.397, 150.644)のように設定しました。Google Maps JS API - スタティックセンターでサイズ変更可能なサークルを描画する

しかし、ここで問題は、円がマウスクリックの位置から描画を開始し、一度フィドルにデモ版として与えられlatlngに収まる円をやっている:私は必要何http://jsfiddle.net/hjkety80/1/

にかかわらず、です私が円をクリックする場所は、与えられた静的ポイントから描画を開始する必要があります。説明があいまいであり、必要であれば詳細を明らかにすることを喜んで謝ります。この問題に関するヘルプ/リファレンスは非常に高く評価されています。

おかげ

EDIT

円の中心は、マーカーに結合する場合であっても、私はあなたのための作業例を作成している可能

+0

固定円が必要な場合は、なぜ描画ツールを使用していますか? – geocodezip

+0

私は円をドローすることができるようにしたいですが、地図上のマーカーの周りのみ..これは、円の中心がマーカーにあるように、アプリの店舗の配送範囲を指定するためです:) –

+0

あなたは使用することができます[私の質問への答え:Google Mapsで編集可能なサークルコントロールのスタイルを設定する方法](http://stackoverflow.com/questions/25474683/how-to-style-editable-circle-controls-in-google)の「DistanceWidget」 -maps/25475156#25475156)、円の中心マーカーをドラッグすることはできません。 – geocodezip

答えて

3

場合も良い回避策です。 DrawingManagerを使用していません。これは達成したいものには適していないため、代わりに円の半径をプログラム的に更新します。

したがって、この例ではマップがロードされると、ユーザは、ポインタカーソルが提示されて、彼はマウスボタンを押したときに、彼はあなたのstatic_positionと新しいcircleradius中心とした円を描き始めますが決定されクリックした場所に基づいて

例にはジオメトリライブラリ(GoogleマップAPIを取得する際に&libraries=geometryという通知が含まれています)を使用して、google.maps.geometry.spherical.computeDistanceBetween()機能を利用して、あなたの中心とユーザーがちょうど半径を取得するためにクリックした場所の距離をメートルで計算します。ユーザーがすぐにボタンを放してマウスを動かしていない場合、カーソルはユーザーのカーソルの移動に基づいて半径を自動的に調整します。ユーザーがマウスを離した後、彼は地図をドラッグすることができます。また、円は調整のために編集可能なままです。

例(ジャストfull pageモードでより良い経験の実行では描画を開始するために地図上でマウスの左ボタンを押してください。):

var map = null; 
 

 
function initialize(){ 
 
\t 
 
    var static_position = new google.maps.LatLng(-34.397, 150.644); 
 
    var the_circle = null; 
 
    
 
    map = new google.maps.Map(document.getElementById('map-canvas'), { 
 
     center: static_position, 
 
     zoom: 8, 
 
     draggable: false, 
 
     draggableCursor:'pointer' 
 
    }); 
 
    
 
    var mousemove_handler; 
 
    
 
    google.maps.event.addListener(map, 'mouseup', function(e) {    
 
     if(mousemove_handler) google.maps.event.removeListener(mousemove_handler); 
 
     map.setOptions({draggable:true, draggableCursor:''}); //allow map dragging after the circle was already created 
 
     the_circle.setOptions({clickable:true}); 
 
    }); 
 
    
 
    google.maps.event.addListenerOnce(map, 'mousedown', function (mousedown_event) { 
 
\t var radius = google.maps.geometry.spherical.computeDistanceBetween(static_position, mousedown_event.latLng); //get distance in meters between our static position and clicked position, which is the radius of the circle 
 
\t the_circle = createCircle(static_position, radius); //create circle with center in our static position and our radius 
 
\t mousemove_handler = google.maps.event.addListener(map, 'mousemove', function(mousemove_event) { //if after mousedown user starts dragging mouse, let's update the radius of the new circle 
 
\t \t var new_radius = google.maps.geometry.spherical.computeDistanceBetween(static_position, mousemove_event.latLng); 
 
\t \t console.log(new_radius); 
 
\t \t the_circle.setOptions({radius:new_radius}); 
 
\t }); 
 
    }); 
 
    
 
} 
 

 
google.maps.event.addDomListener(window, 'load', initialize); 
 

 
function createCircle(center, radius) { 
 

 
    var circle = new google.maps.Circle({ 
 
     fillColor: '#ffffff', 
 
     fillOpacity: .6, 
 
     strokeWeight: 1, 
 
     strokeColor: '#ff0000', 
 
     draggable: false, 
 
     editable: true, 
 
     map: map, 
 
     center: center, 
 
     radius: radius, 
 
     clickable:false 
 
    }); 
 

 
    google.maps.event.addListener(circle, 'radius_changed', function (event) { 
 
     console.log('circle radius changed'); 
 
    }); 
 

 
    google.maps.event.addListener(circle, 'center_changed', function (event) { 
 
     if(circle.getCenter().toString() !== center.toString()) circle.setCenter(center); 
 
    }); 
 
\t 
 
    return circle; 
 
}
<script src="//maps.google.com/maps/api/js?sensor=false&libraries=geometry&dummy=.js"></script> 
 
<body style="margin:0px; padding:0px;"> 
 
\t <div id="map-canvas" style="height:400px; width:500px;"></div> 
 
</body>

+0

これはかなり解決策であり、私が探していたものに対処しています:) thanks alot –

1

一つのオプション(私の答えからにこの質問:How to style editable circle controls in Google Maps)。固定された中心マーカーを持つDistanceWidget。

関連の質問:

コードスニペット:

function init() { 
 
    var mapDiv = document.getElementById('map-canvas'); 
 
    var map = new google.maps.Map(mapDiv, { 
 
    center: new google.maps.LatLng(-34.397, 150.644), 
 
    zoom: 8, 
 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
 
    }); 
 
    var distanceWidget = new DistanceWidget(map); 
 
    google.maps.event.addListener(distanceWidget, 'distance_changed', function() { 
 
    displayInfo(distanceWidget); 
 
    }); 
 

 
    google.maps.event.addListener(distanceWidget, 'position_changed', function() { 
 
    displayInfo(distanceWidget); 
 
    }); 
 
} 
 

 
google.maps.event.addDomListener(window, 'load', init); 
 

 
/** 
 
* A distance widget that will display a circle that can be resized and will 
 
* provide the radius in km. 
 
* 
 
* @param {google.maps.Map} map The map on which to attach the distance widget. 
 
* 
 
* @constructor 
 
*/ 
 
function DistanceWidget(map) { 
 
    this.set('map', map); 
 
    this.set('position', map.getCenter()); 
 

 
    var marker = new google.maps.Marker({ 
 
    draggable: false, 
 
    icon: { 
 
     url: "https://maps.gstatic.com/intl/en_us/mapfiles/markers2/measle_blue.png", 
 
     size: new google.maps.Size(7, 7), 
 
     anchor: new google.maps.Point(4, 4) 
 
    }, 
 
    title: 'Move me!' 
 
    }); 
 

 
    // Bind the marker map property to the DistanceWidget map property 
 
    marker.bindTo('map', this); 
 

 
    // Bind the marker position property to the DistanceWidget position 
 
    // property 
 
    marker.bindTo('position', this); 
 

 
    // Create a new radius widget 
 
    var radiusWidget = new RadiusWidget(); 
 

 
    // Bind the radiusWidget map to the DistanceWidget map 
 
    radiusWidget.bindTo('map', this); 
 

 
    // Bind the radiusWidget center to the DistanceWidget position 
 
    radiusWidget.bindTo('center', this, 'position'); 
 

 
    // Bind to the radiusWidgets' distance property 
 
    this.bindTo('distance', radiusWidget); 
 

 
    // Bind to the radiusWidgets' bounds property 
 
    this.bindTo('bounds', radiusWidget); 
 
} 
 
DistanceWidget.prototype = new google.maps.MVCObject(); 
 

 
/** 
 
* A radius widget that add a circle to a map and centers on a marker. 
 
* 
 
* @constructor 
 
*/ 
 
function RadiusWidget() { 
 
    var circle = new google.maps.Circle({ 
 
    strokeWeight: 2 
 
    }); 
 

 
    // Set the distance property value, default to 50km. 
 
    this.set('distance', 50); 
 

 
    // Bind the RadiusWidget bounds property to the circle bounds property. 
 
    this.bindTo('bounds', circle); 
 

 
    // Bind the circle center to the RadiusWidget center property 
 
    circle.bindTo('center', this); 
 

 
    // Bind the circle map to the RadiusWidget map 
 
    circle.bindTo('map', this); 
 

 
    // Bind the circle radius property to the RadiusWidget radius property 
 
    circle.bindTo('radius', this); 
 

 
    this.addSizer_(); 
 
} 
 
RadiusWidget.prototype = new google.maps.MVCObject(); 
 

 

 
/** 
 
* Update the radius when the distance has changed. 
 
*/ 
 
RadiusWidget.prototype.distance_changed = function() { 
 
    this.set('radius', this.get('distance') * 1000); 
 
}; 
 
/** 
 
* Add the sizer marker to the map. 
 
* 
 
* @private 
 
*/ 
 
RadiusWidget.prototype.addSizer_ = function() { 
 
    var sizer = new google.maps.Marker({ 
 
    draggable: true, 
 
    icon: { 
 
     url: "https://maps.gstatic.com/intl/en_us/mapfiles/markers2/measle_blue.png", 
 
     size: new google.maps.Size(7, 7), 
 
     anchor: new google.maps.Point(4, 4) 
 
    }, 
 
    title: 'Drag me!' 
 
    }); 
 

 
    sizer.bindTo('map', this); 
 
    sizer.bindTo('position', this, 'sizer_position'); 
 

 
    var me = this; 
 
    google.maps.event.addListener(sizer, 'drag', function() { 
 
    // Set the circle distance (radius) 
 
    me.setDistance(); 
 
    }); 
 
}; 
 

 
/** 
 
* Update the center of the circle and position the sizer back on the line. 
 
* 
 
* Position is bound to the DistanceWidget so this is expected to change when 
 
* the position of the distance widget is changed. 
 
*/ 
 
RadiusWidget.prototype.center_changed = function() { 
 
    var bounds = this.get('bounds'); 
 

 
    // Bounds might not always be set so check that it exists first. 
 
    if (bounds) { 
 
    var lng = bounds.getNorthEast().lng(); 
 

 
    // Put the sizer at center, right on the circle. 
 
    var position = new google.maps.LatLng(this.get('center').lat(), lng); 
 
    this.set('sizer_position', position); 
 
    } 
 
}; 
 

 
/** 
 
* Calculates the distance between two latlng locations in km. 
 
* @see http://www.movable-type.co.uk/scripts/latlong.html 
 
* 
 
* @param {google.maps.LatLng} p1 The first lat lng point. 
 
* @param {google.maps.LatLng} p2 The second lat lng point. 
 
* @return {number} The distance between the two points in km. 
 
* @private 
 
*/ 
 
RadiusWidget.prototype.distanceBetweenPoints_ = function(p1, p2) { 
 
    if (!p1 || !p2) { 
 
    return 0; 
 
    } 
 

 
    var R = 6371; // Radius of the Earth in km 
 
    var dLat = (p2.lat() - p1.lat()) * Math.PI/180; 
 
    var dLon = (p2.lng() - p1.lng()) * Math.PI/180; 
 
    var a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(p1.lat() * Math.PI/180) * Math.cos(p2.lat() * Math.PI/180) * Math.sin(dLon/2) * Math.sin(dLon/2); 
 
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); 
 
    var d = R * c; 
 
    return d; 
 
}; 
 

 

 
/** 
 
* Set the distance of the circle based on the position of the sizer. 
 
*/ 
 
RadiusWidget.prototype.setDistance = function() { 
 
    // As the sizer is being dragged, its position changes. Because the 
 
    // RadiusWidget's sizer_position is bound to the sizer's position, it will 
 
    // change as well. 
 
    var pos = this.get('sizer_position'); 
 
    var center = this.get('center'); 
 
    var distance = this.distanceBetweenPoints_(center, pos); 
 

 
    // Set the distance property for any objects that are bound to it 
 
    this.set('distance', distance); 
 
}; 
 

 
function displayInfo(widget) { 
 
    var info = document.getElementById('info'); 
 
    info.innerHTML = 'Position: ' + widget.get('position').toUrlValue(3) + ', distance: ' + widget.get('distance').toFixed(3); 
 
}
html, 
 
body, 
 
#map-canvas { 
 
    height: 100%; 
 
    margin: 0px; 
 
    padding: 0px 
 
}
<script src="https://maps.googleapis.com/maps/api/js?v=3&libraries=geometry"></script> 
 
<div id="info"></div> 
 
<div id="map-canvas"></div>

+0

この回答は非常に役に立ち、私が持っていた問題の初期の回避策として私に来ました。 Thansk alot –

+0

@Matej Pの答えは私の問題に答えているので、回答としてマークしていますが、答えが+1されてからも、私は道に沿って助けてくれます –

関連する問題