How to Get users' geographic location information and display it on Google maps using js + html5

Source: Internet
Author: User

How to Get users' geographic location information and display it on Google maps using js + html5

This article describes how to obtain the geographical location information of a user using js + html5 and display it on a Google Map. Share it with you for your reference. The specific implementation method is as follows:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

<! DOCTYPE html>

<Html>

<Body>

<P id = "demo"> Click the button to get your position: </p>

<Button onclick = "getLocation ()"> Try It </button>

<Div id = "mapholder"> </div>

<Script>

Var x = document. getElementById ("demo ");

Function getLocation ()

{

If (navigator. geolocation)

{

Navigator. geolocation. getCurrentPosition (showPosition, showError );

}

Else {x. innerHTML = "Geolocation is not supported by this browser .";}

}

Function showPosition (position)

{

Var latlon = position. coords. latitude + "," + position. coords. longpolling;

Var img_url = "http://maps.googleapis.com/maps/api/staticmap? Center ="

+ Latlon + "& zoom = 14 & size = 400x300 & sensor = false ";

Document. getElementById ("mapholder"). innerHTML = " ";

}

Function showError (error)

{

Switch (error. code)

{

Case error. PERMISSION_DENIED:

X. innerHTML = "User denied the request for Geolocation ."

Break;

Case error. POSITION_UNAVAILABLE:

X. innerHTML = "Location information is unavailable ."

Break;

Case error. TIMEOUT:

X. innerHTML = "The request to get user location timed out ."

Break;

Case error. UNKNOWN_ERROR:

X. innerHTML = "An unknown error occurred ."

Break;

}

}

</Script>

</Body>

</Html>

I hope this article will help you with web programming.

Related Article

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.