How can I display country borders, US state borders, and city borders using Google Maps?

我的未来我决定 提交于 2019-12-29 07:12:27

问题


I am using Google Maps v3 and I need to add border lines for different areas to my map. For example, in Google Maps all the US State borders are shown automatically. I need to do the same thing for different countries. For example, show all the city boundaries in England. How i can do this with Google Maps API v3?

Regards, Yossi


回答1:


You can use the KML support that is provided in Google Maps, learn more by reviewing the KML Introduction, and then create v3 KmlLayer. The Lat-Lng coordinates that make up the country, state, and city boundaries is not provided out-of-the-box in Google Maps, but some of supporting data is available:

  1. KML of the World Countries as created by Valery Hronusov
  2. KML of the World Capitals as created by Filipumme
  3. KML of the US States is available for download from Google
  4. Some additional city data is available, but that is often city-by-city and requires online searching to find

Much of this data is also available publicly for dynamic retrieval as Fusion Tables, but in that case, you will be using shared public data. I use KML for this type of thing, because I want to have internal copies of the data, maintain full control over the data, and have the ability to make my own changes or updates.




回答2:


Google has started highlighting search areas in pick color.

It's not available in the API.

(It may be available in the future. Features of Google Maps do migrate into the API, but Google don't make announcements in advance)

You would need to find the city boundaries and draw the line yourself, through overlays.




回答3:


I found it difficult to find border/boundary data on Internet - even for well-known boundaries like Canadian provinces. Try using Google Maps Engine. Draw your borders on the map as closed polygons. Next click folder and do "export to KML". The format is long, lat, altitude (with multiple points). This can easily be converted to other formats.

For example, I used a simple perl script to convert from KML to the format I wanted.

while (<>) {
chop;
if (/<name>/) {
    s/<name>//g;
    s/<\/name>//g;
    s/^\s+//; # strip whitespace
    s/\s+$//; # strip whitespace
    print "<state colour=\"red\" name=\"$_\">\n";
}
if (/<coordinates/) {
    s/<coordinates>//;
    s/<\/coordinates>//;
    s/,0.0\s*/,/g;
    @array = split(",");
    for ($i = 0; $i < @array; $i = $i + 2) {
        if ($array[$i+1] && $array[$i]) {
                $array[$i+1] =~ s/^\s+//; # strip whitespace
                $array[$i+1] =~ s/\s+$//; # strip whitespace
                $array[$i] =~ s/^\s+//; # strip whitespace
                $array[$i] =~ s/\s+$//; # strip whitespace
                print  "<point lat=\"" . $array[$i+1] . "\" lng=\"" . $array[$i] . "\" \/>\n";

        }
    }
        print "</state>\n";
}


来源:https://stackoverflow.com/questions/10397255/how-can-i-display-country-borders-us-state-borders-and-city-borders-using-goog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!