2010-03-17 48 views
1

我試圖找出如何使用谷歌地圖API來搜索附近的商家在iPhone應用程序。我完全不熟悉Javascript,並且已經輕鬆地瀏覽了一些Google的示例代碼。如何搜索使用谷歌地圖API?

我發現如何抓住用戶的當前位置。現在我想在該位置附近搜索「餐館」。我只是建立在樣本代碼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> 

回答

1
+0

布萊恩嗨, 是的,這就是我要找的!非常感謝!現在我試圖找出如何使用它大聲笑。我根本不熟悉JavaScript。 – Thomas 2010-03-18 15:57:05

+0

以下是其使用的一些示例:http://code.google.com/apis/ajaxsearch/documentation/#Searchers – 2010-03-18 16:32:00

+0

再次感謝。 code.google.com非常有幫助。谷歌代碼遊樂場也是有用http://code.google.com/apis/ajax/playground/#localsearch_with_markers – Thomas 2010-03-19 17:14:24