Android学习笔记之百度地图(根据地名查询经纬度)(转载)

本文主要是介绍Android学习笔记之百度地图(根据地名查询经纬度)(转载),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 Android学习笔记之百度地图(根据地名查询经纬度)(转载) - quanquan127@126 - 学无止境

重要方法:

public int geocode(java.lang.String strAddr, java.lang.String city)

根据地址名获取地址信息
异步函数,返回结果在MKSearchListener里的onGetAddrResult方法通知
参数:
strAddr - 地址名
city - 地址所在城市
返回:

成功返回0,否则返回-1


具体实现:

package xiaosi.baiduMap;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;

 

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.MKAddrInfo;
import com.baidu.mapapi.MKDrivingRouteResult;
import com.baidu.mapapi.MKPoiResult;
import com.baidu.mapapi.MKSearch;
import com.baidu.mapapi.MKSearchListener;
import com.baidu.mapapi.MKTransitRouteResult;
import com.baidu.mapapi.MKWalkingRouteResult;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapController;
import com.baidu.mapapi.MapView;

 

public class BaiduMapActivity extends MapActivity
{
 /** Called when the activity is first created. */
 private BMapManager mapManager = null;
 private String key = "1B79478DA01F7800AEA8602517A6D89B38151105";
 private MapView mapView = null;
 private MKSearch mKSearch;
 private MapController mapController = null;
 @Override
 public void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  init();
 }

 

 private void init()
 {
  mapManager = new BMapManager(getApplication());
  mapManager.init(key, null);
  super.initMapActivity(mapManager);
  mapView = (MapView) findViewById(R.id.mapView);
  // 设置启用内置的缩放控件
  mapView.setBuiltInZoomControls(true);
  // 得到mMapView的控制权,可以用它控制和驱动平移和缩放
  mapController = mapView.getController();
  // 设置地图zoom级别
  mapController.setZoom(12);
  mKSearch = new MKSearch();
  // 注意,MKSearchListener只支持一个,以最后一次设置为准
  mKSearch.init(mapManager, new MySearchListener());
  if (mKSearch.geocode("五四广场", "青岛") == 0)
  {
   System.out.println("搜索成功");
  }
  else
  {
   System.out.println("搜索失败");
  }
 }

 

 public class MySearchListener implements MKSearchListener
 {
  public void onGetAddrResult(MKAddrInfo arg0, int arg1)
  {
   /*
    * 返回地址信息搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示结果正确,result中有相关结果信息;100表示结果正确,无相关地址信息
    */
   String Location = null;
   if (arg0 == null)
   {
    Location = "没有搜索到该地址";
   }
   else
   {
    // 获得搜索地址的经纬度
    Location = "纬度:" + arg0.geoPt.getLatitudeE6() / 1E6 + "\n"
      + "经度:" + arg0.geoPt.getLongitudeE6() / 1E6 + "\n";
    mapController.animateTo(arg0.geoPt);
   }
   AlertDialog.Builder builder = new AlertDialog.Builder(
     BaiduMapActivity.this);
   builder.setTitle("搜索结果");
   builder.setMessage(Location);
   builder.setPositiveButton("关闭",
     new android.content.DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int which)
      {
       dialog.dismiss();
      }
     });
   builder.show();
  }

 

  public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1)
  {
   /*
    * 返回驾乘路线搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示正确返回
    */
  }

 

  public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2)
  {
   /*
    * 返回poi搜索结果。 参数: arg0 - 搜索结果 arg1 - 返回结果类型: MKSearch.TYPE_POI_LIST MKSearch.TYPE_AREA_POI_LIST MKSearch.TYPE_CITY_LIST arg2 - 错误号,0表示正确返回
    */
  }

 

  public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1)
  {
   /*
    * 返回公交搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示正确返回, 当返回MKEvent.ERROR_ROUTE_ADDR时,表示起点或终点有歧义, 调用MKTransitRouteResult的getAddrResult方法获取推荐的起点或终点信息
    */
  }

 

  public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1)
  {
   /*
    * 返回步行路线搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示正确返回
    */
  }
 }

 

 @Override
 protected boolean isRouteDisplayed()
 {
  return false;
 }

 

 @Override
 protected void onDestroy()
 {
  if (mapManager != null)
  {
   mapManager.destroy();
   mapManager = null;
  }
  super.onDestroy();
 }

 

 @Override
 protected void onPause()
 {
  if (mapManager != null)
  {
   mapManager.stop();
  }
  super.onPause();
 }

 

 @Override
 protected void onResume()
 {
  if (mapManager != null)
  {
   mapManager.start();
  }
  super.onResume();
 }
}

 


这篇关于Android学习笔记之百度地图(根据地名查询经纬度)(转载)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/793498

相关文章

MySQL中between and的基本用法、范围查询示例详解

《MySQL中betweenand的基本用法、范围查询示例详解》BETWEENAND操作符在MySQL中用于选择在两个值之间的数据,包括边界值,它支持数值和日期类型,示例展示了如何使用BETWEEN... 目录一、between and语法二、使用示例2.1、betwphpeen and数值查询2.2、be

Android使用java实现网络连通性检查详解

《Android使用java实现网络连通性检查详解》这篇文章主要为大家详细介绍了Android使用java实现网络连通性检查的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录NetCheck.Java(可直接拷贝)使用示例(Activity/Fragment 内)权限要求

MyBatis-Plus使用动态表名分表查询的实现

《MyBatis-Plus使用动态表名分表查询的实现》本文主要介绍了MyBatis-Plus使用动态表名分表查询,主要是动态修改表名的几种常见场景,文中通过示例代码介绍的非常详细,对大家的学习或者工作... 目录1. 引入依赖2. myBATis-plus配置3. TenantContext 类:租户上下文

MySQL基本表查询操作汇总之单表查询+多表操作大全

《MySQL基本表查询操作汇总之单表查询+多表操作大全》本文全面介绍了MySQL单表查询与多表操作的关键技术,包括基本语法、高级查询、表别名使用、多表连接及子查询等,并提供了丰富的实例,感兴趣的朋友跟... 目录一、单表查询整合(一)通用模版展示(二)举例说明(三)注意事项(四)Mapper简单举例简单查询

2025最新版Android Studio安装及组件配置教程(SDK、JDK、Gradle)

《2025最新版AndroidStudio安装及组件配置教程(SDK、JDK、Gradle)》:本文主要介绍2025最新版AndroidStudio安装及组件配置(SDK、JDK、Gradle... 目录原生 android 简介Android Studio必备组件一、Android Studio安装二、A

MySQL 数据库进阶之SQL 数据操作与子查询操作大全

《MySQL数据库进阶之SQL数据操作与子查询操作大全》本文详细介绍了SQL中的子查询、数据添加(INSERT)、数据修改(UPDATE)和数据删除(DELETE、TRUNCATE、DROP)操作... 目录一、子查询:嵌套在查询中的查询1.1 子查询的基本语法1.2 子查询的实战示例二、数据添加:INSE

springboot+mybatis一对多查询+懒加载实例

《springboot+mybatis一对多查询+懒加载实例》文章介绍了如何在SpringBoot和MyBatis中实现一对多查询的懒加载,通过配置MyBatis的`fetchType`属性,可以全局... 目录springboot+myBATis一对多查询+懒加载parent相关代码child 相关代码懒

在DataGrip中操作MySQL完整流程步骤(从登录到数据查询)

《在DataGrip中操作MySQL完整流程步骤(从登录到数据查询)》DataGrip是JetBrains公司出品的一款现代化数据库管理工具,支持多种数据库系统,包括MySQL,:本文主要介绍在D... 目录前言一、登录 mysql 服务器1.1 打开 DataGrip 并添加数据源1.2 配置 MySQL

Go语言中如何进行数据库查询操作

《Go语言中如何进行数据库查询操作》在Go语言中,与数据库交互通常通过使用数据库驱动来实现,Go语言支持多种数据库,如MySQL、PostgreSQL、SQLite等,每种数据库都有其对应的官方或第三... 查询函数QueryRow和Query详细对比特性QueryRowQuery返回值数量1个:*sql

MyBatis Plus大数据量查询慢原因分析及解决

《MyBatisPlus大数据量查询慢原因分析及解决》大数据量查询慢常因全表扫描、分页不当、索引缺失、内存占用高及ORM开销,优化措施包括分页查询、流式读取、SQL优化、批处理、多数据源、结果集二次... 目录大数据量查询慢的常见原因优化方案高级方案配置调优监控与诊断总结大数据量查询慢的常见原因MyBAT