Google maps API watchPosition - google-maps-api-3

Im building a web app in order to show a marker in the users current location using geolocation and GPS. Here is my code
var marker;
$(window).load(function() {
myOptions = { zoom: 20, mapTypeId: google.maps.MapTypeId.ROADMAP };
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var watchId = navigator.geolocation.watchPosition(centerMap);
});
function centerMap(location)
{
var myLatlng = new google.maps.LatLng(location.coords.latitude,location.coords.longitude);
map.setCenter(myLatlng);
map.setZoom(15);
$("#lat").text("Latitude : " + location.coords.latitude);
$("#lon").text("Longitude : " + location.coords.longitude);
//show current location on map
marker = new google.maps.Marker({
position: myLatlng,
map: map,
zIndex:1
});
navigator.geolocation.clearWatch(watchId);
}
This code works but it keep adding markers as the user changes location.
How i can show only one marker in the current users location?

Instead of generating a new marker each time centerMap is called simply update its position with marker.setPosition(myLatlng).

Related

Is there a way to get the coordinates of a marker in Google Maps?

I'm trying to get my hands dirty on Google Maps API v3. But now I'm stuck with figuring out how to get the coordinates of a certain marker?
<script>
var homeLatlng = new google.maps.LatLng(16.61096000671, 120.31346130371);
var homeMarker = new google.maps.Marker({
position: homeLatlng,
map: map
});
var myOptions = {
center: new google.maps.LatLng(16.61096000671, 120.31346130371),
zoom: 10,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map =
new google.maps.Map(document.getElementById("map_canvas"), myOptions);
//I know that I'm the one who set the coordinates for this,
// but how do I fetch it back?
$('#plot').click(function(){
homeMarker.setMap(map);
});
</script>
homeMarker.getPosition(); //returns a google.maps.LatLng object

Auto refresh Google Map

I'm looking for a method to my Google map auto refresh for each "n" seconds, now i'm refreshing all page, but i must to refresh only the map,
follow my JS code:
var DEFAULT_ZOOM = 14;
function initialize() {
var map;
var latlng = new google.maps.LatLng(-19.0, -59.0);
var myOptions = {
zoom: parseInt(getCookie("zoom_gm")),
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
initMarkers(map);
var zm = DEFAULT_ZOOM;
google.maps.event.addListener(map, 'zoom_changed',
function(){
zm = map.getZoom();
setCookie("zoom_gm", zm);
}
);
}
You will have to retrieve the new data using AJAX. Once you received the data, you have to walk the array/object you've received and add the new Markers. Depending on what you receive(maybe the response contains coordinates you've already set) you need to remove existing markers by using markerObj.setMap(null) .
That's all I can suggest so far, you're very stingy with informations.

Show my location using google maps

I'm trying to do this.When a user comes to sites,i need to show him on google map and also will pass some latitude and longitude.Now my map has to show the path of user and the lat and long which i pass.
Using my below code,i pass my lat and long.Now,i need to show the user position.
How to show both this?
function initialize() {
var myOptions = {
zoom: 17,
center: new google.maps.LatLng(23.333,34.534),
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
var image = 'image.png';
var myLatLng = new google.maps.LatLng(23.333,34.534);
var beachMarker = new google.maps.Marker({
position: myLatLng,
map: map,
icon: image
});
}
[This is how i tried]:http://dpaste.com/695949/
it did not work.
If you want to use HTML5 geo location then in your initialize function after you add the beachMarker put:
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(e) {
var marker = new google.maps.Marker({
map: map,
position: new google.maps.LatLng(e.coords.latitude, e.coords.longitude);
});
}, function() {
// Handle error here
});
}
To display directions on google map you need to have source and destination latlong. in your above code you have destination latlong but user latlong are missing. you need to first get user latlong then you have to use google direction renderer. for more reference read http://code.google.com/apis/maps/documentation/javascript/directions.html
user(source) latlong can be found in many ways -
using geocoder gem based on ip, address, etc.
using html5 geolocation read http://mobile.tutsplus.com/tutorials/mobile-web-apps/html5-geolocation/

making a custom marker using google map api v3

I am using google map API V3. i want to use a custom marker instead of that red marker. Earlier the code was
var myOptions = {
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
// display map
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
if(display_marker) {
// create a map marker
var marker = new google.maps.Marker({
map: map,
position: latlng
});
}
but for the custom marker, i changed the code to
var myOptions = {
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
// display map
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
// create a map marker
var image = 'imgs/pin.png';
var marker = new google.maps.Marker({
map: map,
position: latlng,
icon: image
});
but yet this is not working.
The image path is likely incorrect but,
You could also eliminate var image and just define the icon under var marker depending on your situation.
var marker = new google.maps.Marker({
map: map,
position: latlng,
icon: 'imgs/pin.png',
});
Your code looks okay.
Perhaps the image path is off.
Try putting the pin.png in the same folder as the map and then changing the
var image = 'imgs/pin.png';
to
var image = 'pin.png';
If it works, then you need to revise the path, probably to something like
var image = '/imgs/pin.png';
or
var image = '../imgs/pin.png';

Google Maps Javascript API V3 - Want to add a marker and return the LatLng of the marker

I have a question relevant to Google Maps API. I am learning how to use the Google Maps API. I can just show the map in the div tag, but what I want is to be able to click on the map and show the marker and return the LatLng of the clicked point.
function initialize(v_lat,v_long,v_place) {
var latlng = new google.maps.LatLng(-34.397, 150.644);
var myOptions = {
zoom: 15,
center: latlng,
mapTypeId: google.maps.MapTypeId.SATELLITE
};
var map = new google.maps.Map( document.getElementById("map_canvas") , myOptions );
}
here is the click event example
where you click on map marker place on that point and click on marker which will return the latlng of that location.
try this
var marker;
google.maps.event.addListener(map, 'click', function() {
if(marker==null){
marker = new google.maps.Marker({
position: myLatlng,
map: map,
title:"Hello World!"
});
google.maps.event.addListener(marker, 'click', function() {
alert("latlng" + marker.getPosition());
});
}
});
here is the link for google map v3 you can find all tutorials related to the map
http://code.google.com/apis/maps/documentation/javascript/tutorial.html
http://code.google.com/apis/maps/documentation/javascript/events.html

Resources