简体   繁体   English

获取谷歌地图的坐标纬度

[英]get coordinates Latlng for google map

i have this code:我有这个代码:

 function initialize() { var myLatlng = new google.maps.LatLng(0.0, 0.0); var myOptions = { zoom:7, center: myLatlng, mapTypeId: google.maps.MapTypeId.ROADMAP } var map = new google.maps.Map(document.getElementById("cluster_map"), myOptions); refreshMap(); } function refreshMap() { if (markerClusterer) { markerClusterer.clearMarkers(); } $.getJSON(__cfg('json_data_url'), function(data) { if (data) { for (var i = 0; i < data['Properties']['Count']; i ++ ) { updateClusterMarker(data['Properties'][i]['Property'].latitude, data['Properties'][i]['Property'].longitude, data['Properties'][i]['Property'].id, i, 'Property'); } for (var i = 0; i < data['Requests']['Count']; i ++ ) { updateClusterMarker(data['Requests'][i]['Request'].latitude, data['Requests'][i]['Request'].longitude, data['Requests'][i]['Request'].id, i, 'Request'); } var zoom = null; var size = null; var style = null; markerClusterer = new MarkerClusterer(map, markers, { maxZoom: zoom, gridSize: size, styles: styles[style] }); } }); } function updateClusterMarker(lat, lang, id, count, type) { var imageUrl = __cfg('path_relative') + 'img/R.png'; if (type == 'Property') { var imageUrl = __cfg('path_relative') + 'img/P.png'; } var markerImage = new google.maps.MarkerImage(imageUrl, new google.maps.Size(32, 32)); var latLng = new google.maps.LatLng(lat, lang); eval('var marker' + count + ' = new google.maps.Marker({position: latLng,draggable: false,icon: markerImage});'); eval('marker' + count + '.count=1'); markers.push(eval('marker' + count)); var embed_url = __cfg('path_relative') + 'requests/get_info/' + id; if (type == 'Property') { var embed_url = __cfg('path_relative') + 'properties/get_info/' + id; } var contentString = '<iframe src="' + embed_url + '" width="279" height="120" frameborder = "0" scrolling="no">Loading...</iframe>'; eval('var infowindow' + count + ' = new google.maps.InfoWindow({ content: contentString, maxWidth: 300});'); var infowindow_obj = eval('infowindow' + count); var marker_obj = eval('marker' + count); google.maps.event.addListener(marker_obj, 'click', function() { infowindow_obj.open(map, marker_obj); }); }

It works fine as it is but when i'm trying to center the map same position as the marker it won't work.它可以正常工作,但是当我尝试将地图与标记相同的位置居中时,它将无法正常工作。 i tried this code:我试过这个代码:

 var myLatlng = new google.maps.LatLng(lat, lang);

Does anyone knows how to center the map same position as the marker?有谁知道如何将地图与标记相同的位置居中? any help would be much appreciated.任何帮助将非常感激。

thanks谢谢

You can get the marker position with its .getPosition() method (it returns a LatLng class object).您可以使用其.getPosition()方法(它返回一个LatLng类对象)获取标记位置。 So, you could use something like map.panTo(marker.getPosition());所以,你可以使用类似map.panTo(marker.getPosition());

Example ( jsFiddle ):示例( jsFiddle ):

window.onload = function(){
    var map = new google.maps.Map(document.getElementById('map'), { 
        center: new google.maps.LatLng(22.669, 77.709),
        zoom: 5,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    var marker = new google.maps.Marker({
        position: new google.maps.LatLng(22.669, 77.709),
        map: map
    });

    $('button').on('click', function(){
        map.panTo(marker.getPosition());
    });
};

I finally got it:我终于明白了:

 var latLng = new google.maps.LatLng(lat, lang); var myOptions = { minZoom:4, zoom: 19, center: latLng, zoomControl: true, draggable: true, disableDefaultUI: true, mapTypeId: google.maps.MapTypeId.ROADMAP } map = new google.maps.Map(document.getElementById('cluster_map'), myOptions); refreshMap(); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM