javascript - 如何使用 Google map API 进行搜索?

标签 javascript html iphone xcode google-maps

我正在尝试弄清楚如何使用 Google Maps API 在 iPhone 应用程序中搜索附近的商家。我对 Javascript 完全陌生,并且已经轻松地浏览了一些 Google 的示例代码。

我找到了如何获取用户的当前位置。现在我想搜索该位置附近的“餐馆”。我只是基于 here 中的示例代码进行构建。无论如何,我都会将其更改发布在下面,以防万一。

<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Google Maps JavaScript API v3 Example: Map Geolocation</title>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
<script type="text/javascript" src="http://code.google.com/apis/gears/gears_init.js"></script>
<script type="text/javascript">

var currentLocation;
var detroit = new google.maps.LatLng(42.328784, -83.040877);
var browserSupportFlag =  new Boolean();
var map;
var infowindow = new google.maps.InfoWindow();

function initialize() 
{
    var myOptions = 
    {
        zoom: 6,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    map.enableGoogleBar();

    // Try W3C Geolocation method (Preferred)
    if(navigator.geolocation) 
    {
        browserSupportFlag = true;
        navigator.geolocation.getCurrentPosition(function(position) 
        {
        // TRP - Save current location in a variable (currentLocation)
        currentLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
        // TRP - Center the map around current location
        map.setCenter(currentLocation);
    }, 

        function() 
        {
            handleNoGeolocation(browserSupportFlag);
        });
    }

    else 
    {
        // Browser doesn't support Geolocation
        browserSupportFlag = false;
        handleNoGeolocation(browserSupportFlag);
    }
}

function handleNoGeolocation(errorFlag) 
{
    if (errorFlag == true) 
    {
        // TRP - Default location is Detroit, MI
        currentLocation = detroit;
        contentString = "Error: The Geolocation service failed.";
    } 

    else 
    {
        // TRP - This should never run. It's embedded in a UIWebView, running on iPhone
        contentString = "Error: Your browser doesn't support geolocation.";
    }

    // TRP - Set the map to the default location and display the error message
    map.setCenter(currentLocation);
    infowindow.setContent(contentString);
    infowindow.setPosition(currentLocation);
    infowindow.open(map);
}

</script>
</head>
<body style="margin:0px; padding:0px;" onload="initialize()">
  <div id="map_canvas" style="width:100%; height:100%"></div>
</body>

</html>

最佳答案

关于javascript - 如何使用 Google map API 进行搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2465716/

相关文章:

javascript - Jquery本地存储。它在哪里?

iphone - 如何在后台线程上定期从服务器轮询数据?

javascript - Knockout.js - 数组/对象文字和访问音频路径的属性

javascript - 加载失败,因为未找到受支持的源。播放 HTML5 音频元素时

javascript - Alt 键触发 keyup() 函数

javascript - 在 div 中找到第一个有边距的元素? (目的 : remove margin)

iphone - 在 MKMapView 中移动 MKCircle

iphone - 非法配置 - 静态 TableView 仅在嵌入 UITableViewController 时有效

javascript - 无法在 jquery 的 div 中显示 ul li 的计数

javascript - window.innerWidth window.outerWidth 和有什么区别?