私はこのデモを持っています:http://jsfiddle.net/eB2RX/182/私が望むのは、ユーザーが右側のアドレスをクリックするとそのアドレスが見つかることです。任意の提案どのように私はこれを行うことができますか?リンク先をクリックすると地図上の住所を取得する方法は?
$(".location_address a").click(function(e){
getLatLong("Travnik");
})
私はこのデモを持っています:http://jsfiddle.net/eB2RX/182/私が望むのは、ユーザーが右側のアドレスをクリックするとそのアドレスが見つかることです。任意の提案どのように私はこれを行うことができますか?リンク先をクリックすると地図上の住所を取得する方法は?
$(".location_address a").click(function(e){
getLatLong("Travnik");
})
あなたがクリックした都市の名前をgetLatLong
の機能に渡す必要があります。したがって、このような何か:
$(".location_address a").click(function(e){
getLatLong($(this).html());
});
あなたはアドレスの座標を持っているときにgetLatLong
機能では、あなたがその場所にマーカーやセンターマップを作成することができます。また、あなたはmap
オブジェクトを持っている
if(map){
var newLatLng = new google.maps.LatLng(myLatLngLan, myLatLngLon);
//add a marker on map to our new location
var marker = new google.maps.Marker({
position: newLatLng,
map: map,
title: address
});
//center the map to the location
map.setCenter(newLatLng);
}
を持っています私はこの例の目的のためにグローバルにしました。
これは、あなたが提供されたコードに基づいた実施例であるが、私は強くあなたがコードをリファクタリングすることをお勧め、それは乱雑に見え、何らかの理由でgetLatLong
修正myLatLng1
とmyLatLng2
変数などの問題があり、すぐにマーカーを配置していませんあなたはあなた自身でそれらの問題に対処しなければならないでしょう、私はあなたが達成したいものの実例を作成しました。
の作業コード(より良い経験のためfull page
をクリックしてください):
var map = null;
getLatLong("Sarajevo");
$(".location_address a").click(function(e){
getLatLong($(this).html());
})
// When the window has finished loading create our google map below
google.maps.event.addDomListener(window, 'load', init);
var myLatLng1 = {};
var myLatLng2 = {};
function init() {
// Basic options for a simple Google Map
// For more options see: https://developers.google.com/maps/documentation/javascript/reference#MapOptions
var mapOptions = {
// How zoomed in you want the map to start at (always required)
zoom: 15,
// The latitude and longitude to center the map (always required)
center: new google.maps.LatLng(myLatLng1, myLatLng2), // New York
// How you would like to style the map.
// This is where you would paste any style found on Snazzy Maps.
styles: [{"featureType":"all","elementType":"labels.text.fill","stylers":[{"saturation":36},{"color":"#000000"},{"lightness":40}]},{"featureType":"all","elementType":"labels.text.stroke","stylers":[{"visibility":"on"},{"color":"#000000"},{"lightness":16}]},{"featureType":"all","elementType":"labels.icon","stylers":[{"visibility":"off"}]},{"featureType":"administrative","elementType":"geometry.fill","stylers":[{"color":"#000000"},{"lightness":20}]},{"featureType":"administrative","elementType":"geometry.stroke","stylers":[{"color":"#000000"},{"lightness":17},{"weight":1.2}]},{"featureType":"landscape","elementType":"geometry","stylers":[{"color":"#000000"},{"lightness":20}]},{"featureType":"poi","elementType":"geometry","stylers":[{"color":"#000000"},{"lightness":21}]},{"featureType":"road.highway","elementType":"geometry.fill","stylers":[{"color":"#000000"},{"lightness":17}]},{"featureType":"road.highway","elementType":"geometry.stroke","stylers":[{"color":"#000000"},{"lightness":29},{"weight":0.2}]},{"featureType":"road.arterial","elementType":"geometry","stylers":[{"color":"#000000"},{"lightness":18}]},{"featureType":"road.local","elementType":"geometry","stylers":[{"color":"#000000"},{"lightness":16}]},{"featureType":"transit","elementType":"geometry","stylers":[{"color":"#000000"},{"lightness":19}]},{"featureType":"water","elementType":"geometry","stylers":[{"color":"#000000"},{"lightness":17}]}]
};
// Get the HTML DOM element that will contain your map
// We are using a div with id="map" seen below in the <body>
var mapElement = document.getElementById('map');
// Create the Google Map using our element and options defined above
map = new google.maps.Map(mapElement, mapOptions);
// Let's also add a marker while we're at it
var marker = new google.maps.Marker({
position: new google.maps.LatLng(myLatLng1, myLatLng2),
map: map,
title: 'Snazzy!'
});
}
function getLatLong(address) {
var geo = new google.maps.Geocoder;
geo.geocode({'address':address},function(results, status){
if (status == google.maps.GeocoderStatus.OK) {
var myLatLngLan = results[0].geometry.location.lat();
var myLatLngLon = results[0].geometry.location.lng();
myLatLng1 = myLatLngLan;
myLatLng2 = myLatLngLon;
// Add some code to work with myLatLng
if(map){
var newLatLng = new google.maps.LatLng(myLatLngLan, myLatLngLon);
//add a marker
var marker = new google.maps.Marker({
position: newLatLng,
map: map,
title: address
});
map.setCenter(newLatLng);
}
\t \t \t \t \t \t \t \t \t \t
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
.card {
\t position: relative;
clear: both;
padding: 15px;
margin-bottom: 1em;
position: relative;
box-shadow: 0 2px 2px rgba(0,0,0,.1);
border-radius: 2px;
-webkit-transition: all .6s cubic-bezier(.165,.84,.44,1);
-o-transition: all .6s cubic-bezier(.165,.84,.44,1);
transition: all .6s cubic-bezier(.165,.84,.44,1);
background-color: #222;
height: 600px;
}
#map {
\t width: 100%;
\t height: 600px;
\t background-color: #222;
}
.location_address {
position: absolute;
right: 15px;
top: 15px;
bottom: 30px;
background-color: #292929;
width: 192px;
}
.location_address ul {
list-style-type: none;
float: left;
width: 100%;
margin: 0;
padding: 0;
}
.location_address li {
float: left;
width: 100%;
text-align: center;
margin: 0;
padding: 5px;
border-bottom: 1px solid #eeeeee;
}
.location_address li a{
\t color: white;
}
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("maps", "3",{other_params:"sensor=false"});
</script>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<div id="map"></div>
<div class="location_address">
<ul>
<li><a href="#">Zenica</a></li>
</ul>
</div>