简体   繁体   English

从最近上传的KML文件初始化Google Maps API

[英]Initialising Google Maps API from recently uploaded KML file

so I am making a simple website with the initial feature simply being thus: 因此,我正在制作一个具有初始功能的简单网站,因此:

The user uploads a KML file to the website. 用户将KML文件上传到网站。 The Google Maps API is initialised using this KML file, so that a map is shown with their recent movements. 使用此KML文件初始化Google Maps API,以便显示地图及其最新动态。

Now this sounds very simple but I'm having some trouble with the basics. 现在,这听起来很简单,但是我在基础方面遇到了一些麻烦。 This is more or less my first foray into web development so I'm trying to learn HTML, CSS, AJAX, PHP, Google Maps API V3, and of course the KML file format. 这或多或少是我第一次涉足网络开发,因此我正在尝试学习HTML,CSS,AJAX,PHP,Google Maps API V3,当然还有KML文件格式。

To initialise the Google Maps API using a KML file, one must first set up some basic information such as this: 要使用KML文件初始化Google Maps API,必须首先设置一些基本信息,例如:

function initialize() {
  var myLatlng = new google.maps.LatLng(40.65, -73.95);
  var myOptions = {
    zoom: 12,
    center: myLatlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };

The important bit to look at here being where to center the map. 这里要注意的重要一点是地图的中心。

As well as this, one must specify which KML file to use: 除此之外,还必须指定要使用的KML文件:

var nyLayer = new google.maps.KmlLayer(
      'http://www.searcharoo.net/SearchKml/newyork.kml',
      {  suppressInfoWindows: true,
         map: map});

These are of course just examples, not the data I will be using. 这些当然只是示例,并非我将要使用的数据。

Now my problem is such. 现在我的问题就是这样。 These values seem to be predetermined and static. 这些值似乎是预定的并且是静态的。 To initialise the Google Maps API these values need to be known beforehand. 要初始化Google Maps API,必须预先知道这些值。 But in my case, these values will not be known UNTIL the user uploads the KML file. 但是在我的情况下,直到用户上传KML文件后,这些值才能被知道。

Thus far I have a simple PHP script to handle file uploads. 到目前为止,我有一个简单的PHP脚本来处理文件上传。 I do not know how to pass control over to the JavaScript containing the setup for Google Maps, or how to write this code such that the values which are only known once the file has been uploaded are used to initilialise the map. 我不知道如何将控制权传递给包含Google Maps设置的JavaScript,或者如何编写此代码,以便仅在文件上传后才知道的值用于初始化地图。

In summary, what I mean to achieve is this: 总而言之,我要实现的目标是:

User browses to KML file. 用户浏览到KML文件。 User presses "Upload" button. 用户按下“上传”按钮。 File is uploaded and then immediately used to initialise a map showing their movements. 文件被上传,然后立即用于初始化显示其运动的地图。

I may have written too much but I figure it's best to write too much than not enough. 我可能写了太多,但我认为最好写太多而不是写太多。 Thanks in advance for your advice. 预先感谢您的建议。

Given the code snippets you have posted, the KmlLayer should center and zoom to show the contents of the kml file. 给定您发布的代码段,KmlLayer应该居中并缩放以显示kml文件的内容。 If it is not doing that, please provide enough information to reproduce the problem (the kml file and an html/js file that displays it). 如果没有这样做,请提供足够的信息来重现该问题(显示该问题的kml文件和html / js文件)。

EDIT: You wrote: 编辑:您写道:

I need my php file upload script to save the name of the KML file, and then somehow I need to extract information regarding where the map should be centred, 我需要使用php文件上传脚本来保存KML文件的名称,然后以某种方式提取关于地图应居中位置的信息,

You don't need to do that unless you don't like the default behavior of center and zoom the map to display all the content of the kml 除非您不喜欢center的默认行为,否则不需要这样做,并缩放地图以显示kml的所有内容

and then call the Google Maps API (which I believe is JavaScript) with the information I've just extracted. 然后使用我刚刚提取的信息调用Google Maps API(我相信是JavaScript)。 This all needs to be done with one press of the Upload button. 所有这些都需要通过按一下“上传”按钮来完成。

Yes. 是。

EDIT2: 编辑2:

Okay, but how? 好的,但是如何? I don't know what to do. 我不知道该怎么办。 This is my first attempt at web development. 这是我第一次进行Web开发。 I'm not asking for someone to write the code for me, but pointers on how to do this 我不是在要求别人为我编写代码,而是有关如何执行此操作的指针

Step 1: This example from the documentation shows how to display a kml file, replace the reference to 'http://gmaps-samples.googlecode.com/svn/trunk/ggeoxml/cta.kml' with the reference to the kml file you want to display. 步骤1: 该文档中的示例显示了如何显示kml文件,并将对“ http://gmaps-samples.googlecode.com/svn/trunk/ggeoxml/cta.kml”的引用替换为对kml文件的引用您要显示。

Step 2: change your php to generate the html and javascript required to load and display it when the button is clicked. 第2步:更改您的php,以生成在单击按钮时加载并显示所需的html和javascript。 How that works will depend on where on your page you want to display it and whether you want it to be a hidden div that is displayed when the button is clicked, a new window, or something else. 其工作方式取决于您要在页面上显示它的位置,以及是否希望它是单击按钮,新窗口或其他时显示的隐藏div。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM