0
我有這樣的代碼,設置多個標誌,我是新來的谷歌地圖API,並試圖瞭解如何設置地圖上自動顯示當前用戶的位置標記。我收到的教程是在接收到所有標記後設置地圖中心。這裏是我的代碼: -移動用戶位置標記
<script>
var startPos, positionPromise = $.Deferred();
navigator.geolocation.getCurrentPosition(function(position) {
startPos = position;
$('#pok_lat').val(startPos.coords.latitude);
$('#pok_long').val(startPos.coords.longitude);
positionPromise.resolve();
}, function(error) {
alert('Error occurred. Error code: ' + error.code + '');
// error.code can be:
// 0: unknown error
// 1: permission denied
// 2: position unavailable (error response from locaton provider)
// 3: timed out
positionPromise.reject();
});
$(function($) {
// Asynchronously Load the map API
var script = document.createElement('script');
script.src = "//maps.googleapis.com/maps/api/js?key=SOMEAPIKEY&callback=initialize";
document.body.appendChild(script);
});
window["initialize"] = function() {
var map;
var bounds = new google.maps.LatLngBounds();
var mapOptions = {
mapTypeId: 'roadmap'
};
// Display a map on the page
map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
map.setTilt(45);
// Multiple Markers LatLong
$.when(positionPromise).then(function(){
var lats = document.getElementById("pokemon_lat").value;
var longi = document.getElementById("pokemon_long").value;
console.log('Lat:'+lats+' Long:'+longi);
var markers = [
['You are here!', lats,longi],
<?php
for ($x = 0; $x < count($pok_info); $x++) {
if ($x < count($pok_info)) {
$addcomma = ",";
}
else {
$addcomma = "";
}
$pok_array[] = "['".$pok_info[$x]['name']."', ".$pok_info[$x]['lat'].",".$pok_info[$x]['long']."]".$addcomma;
}
$remove_comma = count($pok_array);
$remove_comma = $remove_comma - 1;
$pok_array[$remove_comma] = rtrim($pok_array[$remove_comma],',');
foreach ($pok_array as $value) {
echo $value;
}
?>
];
// Info Window Content
var infoWindowContent = [
['You are here!'],
<?php
for ($x = 0; $x < count($pok_info); $x++) {
if ($x < count($pok_info)) {
$addcomma = ",";
}
else {
$addcomma = "";
}
$pok_array_info[] = "['<div class=\"info_content\"><h3>".$pok_info[$x]['name']."</h3><br><img width=\"100\" height=\"100\" src=\"".$pok_info[$x]['pic']."\"><p>".$pok_info[$x]['description']."</p>']".$addcomma;
}
$remove_comma = count($pok_array_info);
$remove_comma = $remove_comma - 1;
$pokemon_array_info[$remove_comma] = rtrim($pok_array_info[$remove_comma],',');
foreach ($pok_array_info as $value) {
echo $value;
}
?>
];
// Pok Icons
var pokeImage = [
['images/pokeball.png'],
<?php
$img_icons = array();
for ($x = 0; $x < count($pok_info); $x++) {
$img_icon = $pok_info[$x]['pic'];
$img_icon = explode(".",$img_icon);
$img_icons[] = $img_icon[0]."_small.png";
$addcomma = ",";
$pok_array_info_pic[] = "['".$img_icons[$x]."']".$addcomma;
}
$remove_comma = count($pok_array_info_pic);
$remove_comma = $remove_comma - 1;
$pok_array_info[$remove_comma] = rtrim($pok_array_info_pic[$remove_comma],',');
foreach ($pok_array_info_pic as $value) {
echo $value;
}
?>
];
// Display multiple markers on a map
var infoWindow = new google.maps.InfoWindow(), marker, i;
// Loop through our array of markers & place each one on the map
for(i = 0; i < markers.length; i++) {
var position = new google.maps.LatLng(markers[i][1], markers[i][2]);
bounds.extend(position);
marker = new google.maps.Marker({
position: position,
map: map,
title: markers[i][0],
icon: pokeImage[i][0]
});
// Allow each marker to have an info window
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infoWindow.setContent(infoWindowContent[i][0]);
infoWindow.open(map, marker);
}
})(marker, i));
map.panTo(marker.getPosition());
// Automatically center the map fitting all markers on the screen
//map.fitBounds(bounds);
//console.log('Lat:'+lats)+' Long:'+parseInt(longi));
}
// Override our map zoom level once our fitBounds function runs (Make sure it only runs once)
var boundsListener = google.maps.event.addListener((map), 'bounds_changed', function(event) {
this.setZoom(14);
google.maps.event.removeListener(boundsListener);
});
}, function(){
console.log("Could not get latitude and longitude)-:");
});
}
</script>
我設法獲取用戶的位置,只是說我怎麼能只是居中用戶的位置的地圖嗎?地圖上會有很多其他標記,但我只想將用戶的位置標記居中。你明白我的意思嗎? – MuthaFury
Okij所以ploting你必須遍歷所有出現在你的數據庫中的標記,你可以檢查並傳遞經緯度來drawmap mthod – mean
VAR的MapOptions = {縮放地圖之前:7,中心:經緯度,的mapTypeId:google.maps。 MapTypeId.ROADMAP,mapTypeControl:false};我給選項中心:經緯度,將中心爲給經緯度在地圖 – mean