0

What I am trying to do is trying to calculate SW, NE of given locations and then delta with user's centre location. which will result to fitbounds all locations and user's location in centre.

but unfortunaly it is not working any one quick suggestion ?

public void calculateMapFitBounds(GeoLocation userLocation, List<GeoLocation > contents, Map<String, GeoLocation> latlngBounds){

        //SW
        double minLat = userLocation.getLatitude();
        double minLng = userLocation.getLongitude();

        //NE
        double maxLat = -9999;
        double maxLng = -9999;

        for(GeoLocation content: contents){

            /*
             * Populating Top left cordinate (SW)
             */
            minLat = Math.min(minLat, content.getLatitude());
            minLng = Math.min(minLng, content.getLongitude());

            /*
             * Populating Bottom right cordinate (NE)
             */
            maxLng = Math.max(maxLng, content.getLongitude()) ;
            maxLat = Math.max(maxLat, content.getLatitude());
        }

        /*
         * Calculating Delta fit bounds
         */

        double latDelta = Math.max(Math.abs(minLat - userLocation.getLatitude()), Math.abs(maxLat-userLocation.getLatitude()));

        double lngDelta = Math.max(Math.abs(maxLng - userLocation.getLongitude()), Math.abs(minLng - userLocation.getLongitude()));

        //Calculating SW
        minLat = userLocation.getLatitude()+ latDelta;
        minLng = userLocation.getLongitude()+ lngDelta;

        latlngBounds.put("swLatLng", new GeoLocation(minLat, minLng));


        //Calculating NE
        maxLat = userLocation.getLatitude()- latDelta;
        maxLng = userLocation.getLongitude()-lngDelta;

        latlngBounds.put("neLatLng", new GeoLocation(maxLat, maxLng));

    }

Later in javascript i want to use as follows

var swLatLn = new google.maps.LatLng($!swLatLng.latitude, $!swLatLng.longitude, false);
        var neLatLn = new google.maps.LatLng($neLatLng.latitude, $neLatLng.longitude, false);

        var bounds = new google.maps.LatLngBounds(swLatLn, neLatLn);
        googleMap.fitBounds(bounds);
d-man
  • 57,473
  • 85
  • 212
  • 296
  • In what sense is it "not working"? Not compiling, giving a runtime error, giving unexpected results, or...? Is it the Java part that doesn't work or the JavaScript part? (Or both?) The JavaScript looks invalid, specifically you can't have exclamation marks in variables names like you've done with `$!swLatLng`. – nnnnnn Jan 25 '12 at 07:05
  • $ indicates i am using velocity views – d-man Jan 25 '12 at 07:36

1 Answers1

0

Wrong signs (below they are corrected):

//Calculating SW
minLat = userLocation.getLatitude() - latDelta;
minLng = userLocation.getLongitude()- lngDelta;


//Calculating NE
maxLat = userLocation.getLatitude() + latDelta;
maxLng = userLocation.getLongitude()+ lngDelta;

But you definitely should tell what the problem is and whether there are error messages in the browser error console.

ps: there is an easier way for API v3 https://stackoverflow.com/a/2205577/1164491

Community
  • 1
  • 1
Cheery
  • 16,063
  • 42
  • 57
  • @Faisal khan Do you have link to the page? Or check the values of the calculated variables by yourself. Btw, look here - API 3 allows to do it automatically http://stackoverflow.com/questions/1556921/google-map-api-v3-set-bounds-and-center – Cheery Jan 25 '12 at 07:41
  • posted stackover flow link i don't see how it is doing center with specific pins with fitbounds ? – d-man Jan 25 '12 at 07:56
  • @Faisal khan read also this, it explains everything ) http://stackoverflow.com/a/2496558/1164491 – Cheery Jan 25 '12 at 08:00
  • @Faisal khan So, you set the map center where the user is, extend bounds for all locations and call fitbounds after that. – Cheery Jan 25 '12 at 08:06