Click here to Skip to main content
15,879,096 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am using google map in my website, bydefault it takes latitude and longitude to display center area during intializing the map. I have latitude, longitude and pincode variable, so i want when my latitude and longitude have values then it show on map during initializing but when they are empty display my pincode as center on the map.
JavaScript
var lat = '<%=lat%>';
var lon = '<%=lon%>';
var pincode = '<%=mappincode%>';

function first() 
         {
             if (!lat || !lon) {
                 geocoder.geocode({ 'address': pincode }, function (results, status) {
                     if (status == google.maps.GeocoderStatus.OK) {
                         lat = results[0].geometry.location.lat();
                         lon = results[0].geometry.location.lng();
                         InitializeMap();
                     }
                 });
             } else {
                        InitializeMap();
             }
         }

function InitializeMap() {
         var latlng = new google.maps.LatLng(lat, lon);
         var myOptions =
         {
             zoom: 13,
             center: latlng,
             mapTypeId: google.maps.MapTypeId.ROADMAP
         };
         var map = new google.maps.Map(document.getElementById("Gmap"), myOptions);
window.onload = first;

HTML
<div id ="Gmap" style="height: 300px; width: 600px; removed 25px; removed 25px;" ></div>
Posted
Updated 7-May-14 22:39pm
v3
Comments
Sampath Lokuge 2-May-14 2:00am    
What's your problem with above code ?
Raj Negi 2-May-14 2:12am    
when my lat & lon have values then it works perfect but when they empty nothing shows, even map is not display.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900