Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4051088
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:09:04+00:00 2026-05-20T14:09:04+00:00

I have a MySQL database that contains a large number of polygons, one for

  • 0

I have a MySQL database that contains a large number of polygons, one for each district on a map. They are all quite complicated and it’s unrealistic to simply load them all at once. I’ve looked into using MarkerManager for Google Maps API v3 and this seems to be working as intended. However I’m not sure how to best go about querying my database for polygons that are in the viewport.

I’m thinking that it would be best to store the center of each polygon in a separate column as a simple lat,lng pair so that a query could be run on that – and then return the full polygon.

What is the best way to go about this?

SELECT * FROM polygons WHERE [the center of each polygon is within the bounds of the viewport]

Is this even possible? Am I going about this the wrong way?

And additionally, is there a simple script I can run on my data to return the center of each polygon?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T14:09:04+00:00Added an answer on May 20, 2026 at 2:09 pm

    The format of the query will depend on the database schema, of course. If you’d like to fetch polygon data for polygons with center points inside the current viewport, you could try this:

    select *
    from polygons
    where ((lat_northeast - lat_southwest) / 2) > $viewport_lat_southwest
    and ((lat_northeast - lat_southwest) / 2) < $viewport_lat_northeast
    and ((lng_northeast - lng_southwest) / 2) > $viewport_lng_southwest
    and ((lng_northeast - lng_southwest) / 2) < $viewport_lng_northeast
    

    The query considers the center point to be the center of the polygon’s rectangular bounding box, which could be somewhat different from the “weighted” center for peculiarly shaped polygons. The coordinates of the bounding box, as returned by calling getNorthEast() and getSouthWest() on a LatLngBounds object, are represented by the pseudo-variables “$viewport_lat_southwest”, etc.; “lat_northeast” etc. represent columns in the database table.

    The query can be fairly easily adapted to select only polygons having all or any of their bounding box within the viewport by comparing only the “near” coordinate (for all) or the “far” coordinate (for any) of the polygon to the viewport coordinate.

    If you have separate records for the individual points in each polygon, you could select them like this:

    select distinct polygon_id
    from points
    where lat > $viewport_lat_southwest
    and lat < $viewport_lat_northeast
    and lng > $viewport_lng_southwest
    and lng < $viewport_lng_northeast
    

    That would select polygons with any points in the viewport. To find polygons with all points in the viewport, you could try this:

    select polygon_id
    from points
    where min(lat) > $viewport_lat_southwest
    and max(lat) < $viewport_lat_northeast
    and min(lng) > $viewport_lng_southwest
    and max(lng) < $viewport_lng_northeast
    group by polygon_id
    

    That query also demonstrates a strategy for calculating the bounding box from a set of points:

    select
    min(lat) as latSW,
    min(lng) as lngSW,
    max(lat) as latNE,
    max(lng) as lngNE
    from points
    group by polygon_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a 'price' variable that contains some integer number from a MySQL database.
I have a MYSQL database that contains data on my companies customers as they
I have a MySQL database that contains all the words in the standard English
The code below works great. I have a MySQL database that contains book titles
I'm trying to normalize a mysql database.... I currently have a table that contains
I have a MySql database that stores a timestamp for each record I insert.
I have read-only access to a remote MySQL database, which contains a very large
I have read-only access to a remote MySQL database, which contains a very large
I ran into an interesting problem. I have a MySQL database that contains some
I have a large database (~2700 entries) of vocabulary. Each row contains an English

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.