简体   繁体   中英

To make a google map point to user defined location using Jsp and servlet

I wish to develop an google map,that takes the input from a form(city,area &Country) and wen i press the submit. ie.,1st JSP page and request is sent to Servlet and Servlet sends response to 2nd JSP page. In 2nd JSP page it should display map and points the location in google map.so how do i do it?

You just need to get lat/long from the address that you have provided in the 1st JSP by using Geocoder and then pass those lat/long to second JSP. Use below code to initialize map in script:

 var marker = null;
    var map = null;
    var markers = [];
    var markerColorPrefix;
    var infoWindow = new google.maps.InfoWindow(), marker, i;
    function initializeMap()
    {
        var map_canvas = document.getElementById('map_canvas');
        var map_options = {
            //center: myCenter,
            zoom: 15,
            mapTypeId: google.maps.MapTypeId.SATELLITE
        }
        map = new google.maps.Map(map_canvas, map_options)

    }

And use below code to update required location:

function updateMap(gps)
{
    var latitude = "${recentLocation.latitude}";
    var longitude = "${recentLocation.logitude}";
    var gpsPoint = new google.maps.LatLng(latitude, longitude);
    if(marker != null)
        marker.setMap(null);
    if(map!=null)
        map.setCenter(gpsPoint);
    marker = new google.maps.Marker({
    position:gpsPoint,
    map:map
    });
    markers.push(marker);
}

Define dive in which you want to display Google Map:

<div id="map_canvas"></div>

This will display your location on the map.

Hope this will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM