I want to be able to keep a marker constantly in the center of Google Maps on my mobile application.
I have tried using bindTo
functionality of Google Maps, which binds the marker to the center of the map, but it seems a bit clumsy.
I have tried doing it using HTML and CSS also, but Google Maps does not allow it.
The same as in Uber app.
asked Jan 12, 2014 at 16:59
-
Can you please also tell me how to do this with current location displaying at the time map initializes? Thank YouAbraham Guttman– Abraham Guttman2016年06月06日 12:25:44 +00:00Commented Jun 6, 2016 at 12:25
-
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewKirk Kuykendall– Kirk Kuykendall2016年06月06日 14:38:48 +00:00Commented Jun 6, 2016 at 14:38
-
If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From ReviewMaryBeth– MaryBeth2016年06月06日 14:48:41 +00:00Commented Jun 6, 2016 at 14:48
1 Answer 1
This is a fully functional code which may satisfy your requirements:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0; padding: 0 }
#map-canvas { height: 100% }
</style>
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false">
</script>
<script type="text/javascript">
function initialize() {
var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
var mapOptions = {
center: myLatlng,
zoom: 8
};
var map = new google.maps.Map(document.getElementById("map-canvas"),
mapOptions);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: 'Hello World!'
});
google.maps.event.addListener(map, 'center_changed', function() {
// 0.1 seconds after the center of the map has changed,
// set back the marker position.
window.setTimeout(function() {
var center = map.getCenter();
marker.setPosition(center);
}, 100);
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body><div id="map-canvas"/></body></html>
answered Jan 12, 2014 at 18:38
lang-js