Loading a different google map everytime, after clicking different address - dictionary

In my website I have two panel, left side is for adrress and right side is for google map.
I want to load a google map once I click a particular address without page refresh.
Please help me.

You should implement this via Javascript, the algorithm will be something like this:
Show a map based on a default location (e.g. your city and adequate zoom level to this)
a form with a textbox to search and address
An event on click on a "search button" to geocode and get a lat,lon for that address
Re-set via Javascript the center of the map based on the geocode service response
add a marker on location returned from geocoding service.
hope this helps
PD How are you querying the map with the panels right now ?

Geocode the address here is the link on how to go about it
https://developers.google.com/maps/documentation/javascript/geocoding#GeocodingRequests
then do this
map.setMap(latLng)
where map is your global map object
and latLng is co-ordinates obtained from geocode library

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
var geocoder;
var map;
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(-34.397, 150.644);
var mapOptions = {zoom: 8,center: latlng
}
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
}
function codeAddress() {
var address = document.getElementById('address').value;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
}
else
{
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<input id="address" type="textbox" value="New Delhi, India">
<input type="button" value="Geocode" onclick="codeAddress()">

Related

Geocode a location and match it up to an existing overlay with Google maps API

I've got a great map with the circle overlay but now I want to add the functionality to geocode an address and show it on the same map to see if the address falls within the cicle.
<!DOCTYPE html>
<html>
<head>
<script
src="http://maps.googleapis.com/maps/api/js">
</script>
<script>
var amsterdam=new google.maps.LatLng(52.395715,4.888916);
function initialize()
{
var mapProp = {
center:amsterdam,
zoom:7,
mapTypeId:google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("googleMap"),mapProp);
var myCity = new google.maps.Circle({
center:amsterdam,
radius:20000,
strokeColor:"#0000FF",
strokeOpacity:0.8,
strokeWeight:2,
fillColor:"#0000FF",
fillOpacity:0.4
});
myCity.setMap(map);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="googleMap" style="width:500px;height:380px;"></div>
</body>
</html>
The above code works great. I just can't get the extra geocoded part done.
Use the google maps api geocode service like this:
var geocoder = new google.maps.Geocoder();
function geocode(address) {
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
});
}
geocode("Wilhelminastraat 109HS")
This will find the latitude and longitude of your address and mark it on the map.
http://jsfiddle.net/Lawd9ety/1/

Google map API geolocation after first display of the map

This code takes a big list of (400) markers and adds it to the map, at the end, it shows the whole map including all the markers.
What I have tried to achieve is: when geolocation is available, center the map on location, zoom to level 16 and refresh the map to show it, otherwise, let the whole big map show... I have read and tried many different things, but the geolocation must happen before the map is created. I want to make it happen after. I show you my code here and the temporary link to the working site: http://studioteknik.co/brasseursillimites.com/detaillants/
function initialize()
{
var map = new google.maps.Map(document.getElementById('map-canvas'));
var bounds = new google.maps.LatLngBounds();
var infowindow = new google.maps.InfoWindow();
for (var i in locations) {
var p = locations[i];
var latlng = new google.maps.LatLng(p[1], p[2]);
bounds.extend(latlng);
var marker = new google.maps.Marker({
position: latlng,
map: map,
title: p[0]
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(this.title);
infowindow.open(map, this);
});
}
map.fitBounds(bounds);
}
google.maps.event.addDomListener(window, 'load', initialize);
Here is a simple example of geolocation. Just add the geolocation code anywhere after the map object is created. If the user doesn't allow geolocation, the map will be shown at the default location / zoom level.
function initialize() {
var mapOptions = {
zoom: 10,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: new google.maps.LatLng(0,0)
};
var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
// Geolocation code
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
map.panTo(new google.maps.LatLng(position.coords.latitude, position.coords.longitude));
map.setZoom(16);
});
}
}
initialize();
JSFiddle demo

Geocode not appearing

Below is the javascript for my Geocode. My map will not appear, and I cannot seem to find my error. Please advise.
I'm trying to show the "true" value of the actual location. API was generated appropriately on Google's developers website.
<script>
var geocoder;
var map;
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.Latlng(-34.397, 150.644);
var mapOptions = {
zoom: 8,
Center: latlng
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
}
function codeAddress() {
var address = document.get.getElementById('address').value;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
</script>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=drawing&sensor=true"></script>
You have at least 3 javascript errors which will cause issues:
javascript is case sensitive:
Center is not the same as center
Latlng is not the same as LatLng
there is a missing comma after "Center: latlng" in the mapOptions
this is incorrect (extra ".get"):
var address = document.get.getElementById('address').value;
should be:
var address = document.getElementById('address').value;
And as Jimmie Johansson observed, there is no call to initialize (or for that matter codeAddress)
working example
Do you run initialize() anywhere? Without that your map won't show up.

Google Maps Pass value of dragend to variable

Ok I spent 2 days working on this time for some help :) Everything works fine with the code now I just need to make the marker draggable and update Lat and Lng values. At present the code captures lat and lng via current position as well as via entering a location on the web page. The value of _Lat and _lng are then submitted the a database. I need the user to be able to drag the marker to fine tune the location and update _lat and _lng prior to the database submit. I have made the marker draggable but I can't figure out how to add the dragend code so that it dynamically updates the value of lat and _lng prior to the database submit. I don't need to update anything on the web page just I need to update the _lat and _lng values that get posted to the database. Here is the existing code:
Thanks
var gmap;
var gmarker;
var geocoder;
function initialize() {
// Get here center of map - by default - San Francisco center
var center = new google.maps.LatLng(37.47, -122.25);
var mapOptions = {
center: center,
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
geocoder = new google.maps.Geocoder()
gmap = new google.maps.Map(document.getElementById("map_canvas"),
mapOptions);
// Set inner position of map to form parameters
$('input[name="_lat"]').val(37.47);
$('input[name="_lng"]').val(-122.25);
gmarker = new google.maps.Marker({
map: gmap,
position: center,
icon: '/static/internal/images/mapicon.png',
draggable: true
});
google.maps.event.addListener(gmap, 'click', function(event) {
gmarker.setPosition(event.latLng);
$('input[name="_lat"]').val(event.latLng.lat());
$('input[name="_lng"]').val(event.latLng.lng());
});
navigator.geolocation.getCurrentPosition(function(position){
var _lng = position.coords.longitude;
var _lat = position.coords.latitude;
$('input[name="_lng"]').val(_lng);
$('input[name="_lat"]').val(_lat);
var new_position = new google.maps.LatLng(_lat, _lng);
gmap.setCenter(new_position);
gmarker.setPosition(new_position);
})
}
function showAddress(address) {
if (geocoder) {
geocoder.geocode(
{'address': address},
function(results, status) {
if (status != google.maps.GeocoderStatus.OK) {
alert(address + " not found");
} else {
gmap.setCenter(results[0].geometry.location);
gmarker.setPosition(results[0].geometry.location);
$('input[name="_lat"]').val(results[0].geometry.location.lat());
$('input[name="_lng"]').val(results[0].geometry.location.lng());
}
}
);
}
}
listen to the position_changed-event of the marker:
google.maps.event.addListener(gmarker, 'position_changed', function() {
$('input[name="_lat"]').val(this.getPosition().lat());
$('input[name="_lng"]').val(this.getPosition().lng());
});
Your click event is on map it should be on marker
Change
google.maps.event.addListener(gmap, 'click', function(event) {
To
google.maps.event.addListener(gmarker, 'click', function(event) {

API v3: Issue with GeoCoding service in Firefox 6.0 / IE 9.0

I have created a code to add multiple markers and infoWindows using the GeoCoding service. I have copied the method from API v3 Docs. My script pulls address information from a ASP.Net web service and writes them onto a hidden div element.
function codeAddress(address)
{
//Get the location information from address
geocoder.geocode( { 'address': address}, function(results, status)
{
if (status == google.maps.GeocoderStatus.OK)
{
//Add a google marker onto the given address
var marker = new google.maps.Marker({
map: map,
animation: google.maps.Animation.DROP,
position: results[0].geometry.location
});
//Prepare a infoWindow to display complete address
var faddress = "<h4>Office Address:</h4><span>" + address + "</span>";
var infowindow = new google.maps.InfoWindow({content: faddress});
//Opening information window on mouseover event of marker
google.maps.event.addListener(marker, 'mouseover', function(){
infowindow.open(map, marker);
});
//closing the info window when mouse is moved out of marker
google.maps.event.addListener(marker, 'mouseout', function(){
infowindow.close();
});
}
});
}
Next piece of code reads the address from hidden div elements and adds the marker along with InfoWindows. This code works perfectly with Internet Explorer 7.0/8.0.
//Populate the result onto div element
$.each(arr,function(index, item){
$(".result").append("<div class='block'>" + item + "</div>");
});
//Loop through the div element and add marker to map
$(".block").each(function(){
codeAddress($(this).text());
})
But the same code is not working when i open it on Firefox 6.0/IE 9.0.
GeoCoding service returns ZERO_RESULTS for the same call. When I call the method 5 times with same address it is able to add markers.
Any idea if geoCoding service has issues with new browsers ?
Thanks in Advance...
Sudhir
this is the code im using to generate maps, you can take it and adjust it to your needs:
var address = document.getElementsByTagName('address')[0].innerHTML;
var title = document.getElementById('contact').getElementsByTagName('h2')[0].innerHTML;
var geocoder;
var map;
geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'address': address },
function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var myOptions = {
zoom: 15,
center: results[0].geometry.location,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location,
title: title
});
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});

Resources