Map of locations where students can locate visitors (1)

Source: Internet
Author: User
Tags geoip database

Nowadays, many blogger prefer to embed a small Google map in their blog to mark their location. Such a simple function can increase the color of your blog and attract more visitors. By studying the previous chapter, I believe that readers can make Google maps by themselves. This chapter will not describe this too much, but will introduce how to locate a visitor on a map and record the visitor's regional source.
Understand Microsoft's IP address locating Interface

I. This section describes how to use Microsoft's open interfaces to obtain visitor locations. This interface opened by Microsoft is very simple. It is similar to the loadgeoinfo () interface generated using the geoip database of maxmind described in the previous chapter. For more information, see http://msdn2.microsoft.com/en-us/library/aa907670.aspx.

This interface is described in detail below.

Enter "http://maps.live.com/wifiipservice/locate.ashx" in the browser to display the following information in normal conditions.

Setautolocateviewport (30.55397429, 114.289461, 10, false, "% 1 has determined your location by using your computer's IP address .");

The data may be slightly different, but the interfaces to be implemented are consistent, that is, setautolocateviewport (). The function prototype is as follows:

Setautolocateviewport (latitude, longpolling, zoomlevel, unknown, message );

The following describes the meanings of each parameter.

-Latitude: the latitude of the current IP address;

-The second parameter, longpolling, indicates the longitude of the current IP address;

-Zoomlevel, the third parameter, is the current scaling level suitable for Microsoft Virtual Earth display (useless for Google Maps APIs );

-4th parameter unknown-this interface does not have relevant information and may be used to enable the virtual 3D function of the Virtual Earth map (useless to Google Maps APIs );

-5th parameter messages-messages should be displayed (useless to Google Maps APIs ).

The preceding section describes how to display the geographic information of visitor IP addresses. Of course, there are still situations where the visitor's IP address cannot be normally obtained, which are divided into two types.

The first type is shown as follows:

Showmessage ("Virtual Earth cannot determine your current location. Try again later .");

The second is similar, as shown below:

Autolocateundetectable ("% 1 cannot determine your current location. Try again later .");

You can use this free interface after learning this information.

2. Use the IP address locating interface of Microsoft to obtain the Visitor Location

This section uses the interface described in the previous section to locate the visitor. The steps include creating basic Google Maps, completing interfaces, and calling interfaces.

1. Create a basic Google Map

The previous section describes how to create a map. Unlike the previous article, in order to ensure that a map can be perfectly embedded into a blog page, CSS of the webpage needs to be set to meet the requirements. The Code is as follows:

<Style type = "text/CSS">

Body, HTML, # map {

Margin: 0px;

Width: 100%;

Height: 100%;

}

</Style>

The purpose of this Code is to make the body part of the HTML page and the map container (that is, the layer where # map, ID is map) 100% fill the whole page without any blank space.

2. Implementation Interface

First, implement the setautolocateviewport () interface. This is the most important interface in this example and is used for actual locating. Here, you only need to obtain the longitude and latitude information and mark the geographical location of the visitor on the Google map. The Code is as follows:

// The following code implements the setautolocateviewport () Interface

// Set the map center to the Current Visitor Location and add a landmark

Function setautolocateviewport (latitude, longpolling, zoomlevel, unknown, message)

{

// Visitor Location

VaR point = new glatlng (latitude, longpolling );

// Create a custom gicon

VaR myicon = new gicon ();

Myicon. Image = "http://labs.google.com/ridefinder/images/mm_20_green.png ";

Myicon. Shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png ";

Myicon. iconsize = new gsize (12, 20 );

Myicon. shadowsize = new gsize (22, 20 );

Myicon. iconanchor = new gpoint (6, 20 );

Myicon. infographic wanchor = new gpoint (5, 1 );

// Mark the visitor's location

VaR marker = new gmarker (point, // create a custom gmarker

{Icon: myicon,

Title: "Green marked as your current location "});

Map. addoverlay (Marker );

// Move to the Current Visitor's location

Map. Panto (point );

}

In addition, to avoid undefined object errors from browser browsing, you must provide corresponding interfaces for showmessage () and autolocateundetectable () which cannot correctly obtain data. For the sake of simplicity, only the window. Alert () method is used to indicate that information cannot be obtained. If you are interested, you can try other friendly methods to provide the above information.

The showmessage () code is as follows:

// Showmessage () Interface

Function showmessage ()

{

Alert ("You cannot provide your location for the moment. Please try again later! ");

}

The autolocateundetectable () code is as follows:

// Showmessage () Interface

Function autolocateundetectable ()

{

Alert ("your location cannot be detected for the moment. Please try again later! ");

}

In this way, the interface is implemented, and you only need to call it later.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.