Google API由經緯度查高度的iPhone程式

  • warning: Declaration of views_handler_argument::init(&$view, &$options) should be compatible with views_handler::init(&$view, $options) in /home/xnvgu7/public_html/sites/all/modules/views/handlers/views_handler_argument.inc on line 745.
  • warning: Declaration of views_handler_filter::options_validate(&$form, &$form_state) should be compatible with views_handler::options_validate($form, &$form_state) in /home/xnvgu7/public_html/sites/all/modules/views/handlers/views_handler_filter.inc on line 585.
  • warning: Declaration of views_handler_filter::options_submit(&$form, &$form_state) should be compatible with views_handler::options_submit($form, &$form_state) in /home/xnvgu7/public_html/sites/all/modules/views/handlers/views_handler_filter.inc on line 585.
  • warning: Declaration of views_handler_filter_boolean_operator::value_validate(&$form, &$form_state) should be compatible with views_handler_filter::value_validate($form, &$form_state) in /home/xnvgu7/public_html/sites/all/modules/views/handlers/views_handler_filter_boolean_operator.inc on line 149.
  • warning: Declaration of views_plugin_style_default::options(&$options) should be compatible with views_object::options() in /home/xnvgu7/public_html/sites/all/modules/views/plugins/views_plugin_style_default.inc on line 25.
  • warning: Declaration of views_plugin_row::options_validate($form, &$form_state) should be compatible with views_plugin::options_validate(&$form, &$form_state) in /home/xnvgu7/public_html/sites/all/modules/views/plugins/views_plugin_row.inc on line 135.
  • warning: Declaration of views_plugin_row::options_submit($form, &$form_state) should be compatible with views_plugin::options_submit(&$form, &$form_state) in /home/xnvgu7/public_html/sites/all/modules/views/plugins/views_plugin_row.inc on line 135.

Google API由經緯度查高度的iPhone程式:2011年03月21日星期一

iPhone程式中若有了經緯度值,想找高度(離海平面標高),可用Google API,詳見Google Elevation API 一文。 下段程式碼即是:

  1. NSStringEncoding enc;
  2. NSError*error = [[[NSErroralloc] init] autorelease];
  3. NSString*altitudeGetter = [NSString stringWithContentsOfURL :[NSURLURLWithString: @"http://maps.google.com/maps/api/elevation/xml?locations=39.7391536,-104.9847034&sensor=true"] usedEncoding:&enc error:&error ];
  4. NSLog(@"the xml = %@ -- \n", altitudeGetter);

1,2行是先定兩個var給第三行用。

第三行即為使用經緯度值呼叫Google API,用stringWithContentsOfURL從http://maps.google.com/maps/api/elevation 取得高度(離海平面標高),/xml表示output為xml format,?locations=39.7391536,-104.9847034則是經、緯(latitude、longitude)度值,&sensor=true表示iPhone有GPS, usedEncoding:&enc將Encode方式回傳至enc,error:&error將error狀態回傳至error。

第四行將自Google Elevation API的xml列印出來如下:

由上xml,可以看到<eleveation>1608.6379395</elevation>顯示經、緯(latitude、longitude)度{39.7391536, -104.9847034}的高度(離海平面標高)為1608.6379395。

注意事項:

  1. 不需使用Google API金鑰。