还请大家注意在文章进行转载的时候麻烦注明出处
《http://blog.csdn.net/shuaiyinoo》谢谢
今天是百度地图的最后一个博客的更新了,看了上面几篇博客不知道大家有没有感受或者有没有什么体会,基本上百度地图提供的查询的方法基本上都是异步调用的,通过异步调用显示在 MySearchListener implements MKSearchListener 实现了这个接口的各个方法当中
当然这次的经纬度查询和转换同样是在这里面有所体会,在进行经纬度转换的时候,只需要调用百度地图的下面两个方法,就可以成功的知道如何进行经纬度的转换了
总得来说百度地图做起来比Google地图的API做起来简单,原因可能就是应为百度地图的API是中文的,中国人开发的更加适合中国人的使用习惯等等,加油,成功是所于大家的,下面我就把这一几篇博客的核心代码贴出来,供大家参考参考
然后附上这几个博客的总体的项目,请大家笑纳......有很多不足的地方还请大家多多包含......
--->>>飞机直达源代码下载地
package com.shuaiyin.baidu;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.Toast;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.ItemizedOverlay;
import com.baidu.mapapi.MKAddrInfo;
import com.baidu.mapapi.MKBusLineResult;
import com.baidu.mapapi.MKDrivingRouteResult;
import com.baidu.mapapi.MKGeneralListener;
import com.baidu.mapapi.MKPlanNode;
import com.baidu.mapapi.MKPoiInfo;
import com.baidu.mapapi.MKPoiResult;
import com.baidu.mapapi.MKSearch;
import com.baidu.mapapi.MKSearchListener;
import com.baidu.mapapi.MKSuggestionResult;
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;
import com.baidu.mapapi.Overlay;
import com.baidu.mapapi.OverlayItem;
import com.baidu.mapapi.RouteOverlay;
import com.baidu.mapapi.TransitOverlay;
/**
* 让百度地图继承MapActivity
* @author shuaiyin
*
*/
public class BaiDu_SuYiActivity extends MapActivity {
//添加百度地图的相关控件
private MapView mapView;
//加载百度地图的引起
private BMapManager bMapManager;
//定义百度地图的KEY
private String key = "3DA88729DD94B0429A4BEE30797E04D91B0211C4";
//在百度地图上添加相应的控件
private MapController mapController;
//定义搜索对象
private MKSearch mkSearch ;
//定义一个起点
private MKPlanNode start;
//定义一个终点
private MKPlanNode end;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//首先实例化mapView
mapView = (MapView) this.findViewById(R.id.bmapView);
bMapManager = new BMapManager(BaiDu_SuYiActivity.this);
//调用百度地图加载KEY
bMapManager.init(key, new MKGeneralListener() {
@Override
public void onGetPermissionState(int arg0) {
if(arg0 == 300){
Toast.makeText(BaiDu_SuYiActivity.this, "您输入的KEY有问题,请核实", 2).show();
}
}
@Override
public void onGetNetworkState(int arg0) {
}
});
this.initMapActivity(bMapManager);
//表示可以设置缩放功能
mapView.setBuiltInZoomControls(true);
mapController = mapView.getController();
//V1.00
//在百度地图上标注一个中心点
//GeoPoint geoPoint = new GeoPoint((int)(39.915*1E6),(int)(116.404*1E6));
//给地图对象设置一个中心点
//mapController.setCenter(geoPoint);
//设置地图的缩放级别
//mapController.setZoom(12);
//显示交通地图
//mapView.setTraffic(true);
//显示卫星地图
//mapView.setSatellite(true);
//v2.00
//给地图对象设置标注物
//mapView.getOverlays().add(new MyOverlay());
//mapController.setZoom(12);
//v3.00
//给地图设置多个标注物
//显示标注的图标
//Drawable drawable = getResources().getDrawable(R.drawable.iconmarka);
//mapView.getOverlays().add(new MyOverLayItem(drawable));
//v4.00
//mapController.setZoom(14);
//在地图范围内实现对物体的搜索
//mkSearch = new MKSearch();
//mkSearch.init(bMapManager, new MySearchListener());
//这个方法表示根据中心点搜索周边的内容
//mkSearch.poiSearchNearBy("厕所", new GeoPoint((int)(39.915*1E6),(int)(116.404*1E6)), 5000);
//v5.00驾车路线的搜索
//mapController.setZoom(14);
//在地图范围内实现对物体的搜索
//mkSearch = new MKSearch();
//mkSearch.init(bMapManager, new MySearchListener());
//给定第一个坐标
//start = new MKPlanNode();
//start.pt = new GeoPoint((int)(39.915*1E6),(int)(116.404*1E6));
//给定第二个坐标
//end = new MKPlanNode();
//end.pt = new GeoPoint(40057031,116307852);
//路线的优先顺序
//mkSearch.setDrivingPolicy(MKSearch.EBUS_TIME_FIRST);
//调用路线的回调方法
//mkSearch.drivingSearch(null, start, null, end);
//6.0行走路线的搜索
//mapController.setZoom(12);
//在地图范围内实现对物体的搜索
//mkSearch = new MKSearch();
//mkSearch.init(bMapManager, new MySearchListener());
//给定第一个坐标
//start = new MKPlanNode();
//start.pt = new GeoPoint((int)(39.915*1E6),(int)(116.404*1E6));
//给定第二个坐标
//end = new MKPlanNode();
//end.pt = new GeoPoint(40057031,116307852);
//设置回调的行走路线的方法
//mkSearch.walkingSearch(null, start, null, end);
//7.0公交换乘路线的搜索
//mapController.setZoom(12);
//在地图范围内实现对物体的搜索
//mkSearch = new MKSearch();
//mkSearch.init(bMapManager, new MySearchListener());
//给定第一个坐标
//start = new MKPlanNode();
//start.pt = new GeoPoint((int)(39.915*1E6),(int)(116.404*1E6));
//给定第二个坐标
//end = new MKPlanNode();
//end.pt = new GeoPoint(40057031,116307852);
//设置回调的行走路线的方法
//mkSearch.transitSearch("北京", start, end);
//8.0公交详细路线的搜索
//mapController.setZoom(12);
//在地图范围内实现对物体的搜索
//mkSearch = new MKSearch();
//mkSearch.init(bMapManager, new MySearchListener());
//设置回调的行走路线的方法
//mkSearch.poiSearchInCity("北京", "300");
//v9.00驾车路线多条搜索
mapController.setZoom(14);
//在地图范围内实现对物体的搜索
mkSearch = new MKSearch();
mkSearch.init(bMapManager, new MySearchListener());
//给定第一个坐标
start = new MKPlanNode();
start.pt = new GeoPoint((int)(39.915*1E6),(int)(116.404*1E6));
//给定第二个坐标
end = new MKPlanNode();
end.pt = new GeoPoint(40057031,116307852);
//调用路线的回调方法
mkSearch.transitSearch("北京", start, end);
}
//搜索对象的实现
public class MySearchListener implements MKSearchListener{
//根据名称获取经纬度
@Override
public void onGetAddrResult(MKAddrInfo res, int error) {
if(error != 0){
Toast.makeText(BaiDu_SuYiActivity.this, "对不起,找不到您的结果", 2).show();
return;
}
mapView.getController().animateTo(res.geoPt);
String msg = String.valueOf(res.geoPt.getLatitudeE6()/1E6)+String.valueOf(res.geoPt.getLongitudeE6()/1E6);
Drawable drawable = getResources().getDrawable(R.drawable.iconmarka);
mapView.getOverlays().clear();
}
@Override
public void onGetBusDetailResult(MKBusLineResult result, int type) {
if(result == null||type != 0){
Toast.makeText(BaiDu_SuYiActivity.this, "对不起,找不到您的结果", 2).show();
return;
}
RouteOverlay routeOverlay = new RouteOverlay(BaiDu_SuYiActivity.this, mapView);
routeOverlay.setData(result.getBusRoute());
//清空所有的公交信息
mapView.getOverlays().clear();
mapView.getOverlays().add(routeOverlay);
mapView.invalidate();
mapView.getController().animateTo(result.getBusRoute().getStart());
}
@Override
public void onGetDrivingRouteResult(MKDrivingRouteResult result, int type) {
//对调用的搜索方法进行回调响应
if(result == null){
return;
}
RouteOverlay routeOverlay = new RouteOverlay(BaiDu_SuYiActivity.this, mapView);
//里面的参赛之所以填0是应为我们只需要返回第一条线路
routeOverlay.setData(result.getPlan(0).getRoute(0));
mapView.getOverlays().add(routeOverlay);
mapView.invalidate();
}
@Override
public void onGetPoiDetailSearchResult(int result, int type) {
}
@Override
public void onGetPoiResult(MKPoiResult result, int type, int iError) {
//对调用的搜索方法进行回调响应
//v3.00
//if(result == null){
// return;
//}
//PoiOverlay poiOverlay = new PoiOverlay(BaiDu_SuYiActivity.this, mapView);
//poiOverlay.setData(result.getAllPoi());
//mapView.getOverlays().add(poiOverlay);
//mapView.invalidate();
//v8.00
if(result == null||iError != 0){
Toast.makeText(BaiDu_SuYiActivity.this, "对不起,找不到您的结果", 2).show();
return;
}
//定义一个公交线路图描述类
MKPoiInfo mkPoiInfo = null;
//获得公交线路图的总的站点
int totalPoiNum = result.getNumPois();
for(int i = 0;i<totalPoiNum;i++){
mkPoiInfo = result.getPoi(i);
if(mkPoiInfo.ePoiType == 2){
break;
//如果说返回的时候公交的线路图就返回
}
}
mkSearch.busLineSearch("北京", mkPoiInfo.uid);
}
@Override
public void onGetRGCShareUrlResult(String arg0, int arg1) {
}
@Override
public void onGetSuggestionResult(MKSuggestionResult arg0, int arg1) {
}
@Override
public void onGetTransitRouteResult(MKTransitRouteResult result, int type) {
//公交换乘的回调方法
//v7.00
//if(result == null){
// return;
//}
//TransitOverlay transitOverlay = new TransitOverlay(BaiDu_SuYiActivity.this, mapView);
//transitOverlay.setData(result.getPlan(0));
//mapView.getOverlays().add(transitOverlay);
//刷新地图
//mapView.invalidate();
//v9.00
if(result == null){
return;
}
//获得所有的驾车线路图
int planNum = result.getNumPlan();
for(int i = 0 ;i<planNum;i++){
TransitOverlay transitOverlay = new TransitOverlay(BaiDu_SuYiActivity.this, mapView);
transitOverlay.setData(result.getPlan(i));
mapView.getOverlays().add(transitOverlay);
}
mapView.invalidate();
mapView.getController().animateTo(result.getStart().pt);
}
@Override
public void onGetWalkingRouteResult(MKWalkingRouteResult result, int type) {
//对调用的搜索方法进行回调响应
if(result == null){
return;
}
RouteOverlay routeOverlay = new RouteOverlay(BaiDu_SuYiActivity.this, mapView);
routeOverlay.setData(result.getPlan(0).getRoute(0));
mapView.getOverlays().add(routeOverlay);
//刷新地图
mapView.invalidate();
}
}
//标注一个遮盖物
public class MyOverlay extends Overlay{
//声明一个地点
private GeoPoint geoPoint = new GeoPoint((int)(39.915*1E6),(int)(116.404*1E6));
//声明一个画笔工具
private Paint paint = new Paint();
@Override
public void draw(Canvas arg0, MapView arg1, boolean arg2) {
super.draw(arg0, arg1, arg2);
Point point = mapView.getProjection().toPixels(geoPoint, null);
arg0.drawText("这里是天安门", point.x,point.y,paint);
}
}
//标注多个遮盖物
public class MyOverLayItem extends ItemizedOverlay<OverlayItem>{
private List<OverlayItem> overlayItem = new ArrayList<OverlayItem>();
//定义一组坐标
private double mLat1 = 39.90923;//经
private double mLot1 = 116.397428;//纬
private double mLat2 = 39.92923;//经
private double mLot2 = 116.377428;//纬
private double mLat3 = 39.94923;//经
private double mLot3 = 116.357428;//纬
private double mLat4 = 39.96923;//经
private double mLot4 = 116.337428;//纬
//用于在地图上标识坐标,用一个图片标注
public MyOverLayItem(Drawable drawable) {
super(drawable);
GeoPoint geoPoint1 = new GeoPoint((int)(mLat1*1E6),(int)(mLot1*1E6));
GeoPoint geoPoint2 = new GeoPoint((int)(mLat2*1E6),(int)(mLot2*1E6));
GeoPoint geoPoint3 = new GeoPoint((int)(mLat3*1E6),(int)(mLot3*1E6));
GeoPoint geoPoint4 = new GeoPoint((int)(mLat4*1E6),(int)(mLot4*1E6));
overlayItem.add(new OverlayItem(geoPoint1, "A", "这是第一个"));
overlayItem.add(new OverlayItem(geoPoint2, "B", "这是第二个"));
overlayItem.add(new OverlayItem(geoPoint3, "C", "这是第三个"));
overlayItem.add(new OverlayItem(geoPoint4, "D", "这是第四个"));
//刷新地图
populate();
}
//返回指定的List集合中每一个坐标
@Override
protected OverlayItem createItem(int arg0) {
return overlayItem.get(arg0);
}
@Override
public int size() {
return overlayItem.size();
}
//当标注物被点击的时候
@Override
public boolean onTap(int i) {
Toast.makeText(BaiDu_SuYiActivity.this, overlayItem.get(i).getSnippet(), 2).show();
return true;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if(bMapManager != null){
bMapManager.destroy();
bMapManager = null;
}
}
@Override
protected void onResume() {
super.onResume();
if(bMapManager != null){
bMapManager.start();
}
}
@Override
protected void onPause() {
super.onPause();
if(bMapManager != null){
bMapManager.stop();
}
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
}
有时你需要一个对话框,但同时对话框中的内容有更多控制和能控制其生命周期,这时你可以使用带有Dialog样式的Activity来应用你的项目中,
想使Activity有对话框那样效果可以在Androidmanifest中添加 android:style/Theme.Dialog 的主题特性
例如这样:
<activity android:name=”MyDialogActivity”
android:theme=”@android:style/Theme.Dialog”>
</activity>
如果你觉得上面不足以满足你的需求想实现 半透明的, 圆角的,不妨可以试试自定义对话框的style
首先
第一步: 准备工作要做好 一张.9图片
loading_bg.9.png
第二步 :自定义style 在你项目 values 文件夹中创建一个styles.xml 文件
接着在styles文件中添加 名为 MyDialogStyle的style
<style name="MyDialogStyle">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowFrame">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<item name="android:backgroundDimEnabled">true</item>
</style>
第三步:Dialog的布局 在layout文件夹中添加布局文件 activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="180dp"
android:layout_height="180dp"
android:layout_centerInParent="true"
android:background="@drawable/loading_bg" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="center"
android:orientation="vertical" >
<ProgressBar
android:id="@+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="正在登录"
android:textColor="#fff"
android:textSize="20sp" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
创建 DialogActivity类
public class DialogActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 这里你可以进行一些等待时的操作,我这里用8秒后显示Toast代理等待操作
new Handler().postDelayed(new Runnable(){
@Override
public void run(){
DialogActivity.this.finish();
Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_SHORT).show();
}
}, 8000);
}
}
第四步 : 在Manifest文件中对DialogActivity进行注册,并使用上面自定义MyDialogStyle样式
<activity
android:name="LoadingActivity"
android:theme="@style/MyDialogStyle" />
<activity
第五步:运行工程并查看效果图
第六步:打完收工
2楼varyli5小时前恩,还行。1楼sqyttl昨天 20:33清晰不错
在SDK中打开其他接入应用的
一直以来,在iOS的开发中,在程序中打开另外一个应用是不允许。后来有正义之士用class-dump在私有API中找到了这样的功能。那就是使用UIApplication的launchApplicationWithIdentifier:suspended:来打开。
使用的办法如下:
NSString *identifier = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleIdentifier"];
[[UIApplication sharedApplication] launchApplicationWithIdentifier:identifier suspended:NO];
毕竟是私有API不是一个好的办法,至少你永远都得不到App Store的认可。
在某些时候是其实我们可能还是需要这样的功能。作为一个SDK,其实还是有一种比较好的的。那就是使用UIApplication的openURL:的方法。
我们先来了解一下openURL和实现的方案。OpenURL其实是有很丰富的功能,除了简单的调用safari打开网站,还可有google地图搜索,Mail,拨打电话,发送短信,打开AppStore。
-(IBAction)openMaps {//打开地图
// Where is Apple on the map anyway?
NSString* addressText = @”1 Infinite Loop, Cupertino, CA 95014″;
// URL encode the spaces
addressText = [addressText stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding];
NSString* urlText = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@", addressText];
// lets throw this text on the log so we can view the url in the event we have an issue
NSLog(urlText);
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlText]];
}
-(IBAction)openEmail {//打开mail
// Fire off an email to apple support
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://devprograms@apple.com"]];
}
-(IBAction)openPhone {//拨打电话
// Call Google 411
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://8004664411"]];
}
-(IBAction)openSms {//打开短信
// Text to Google SMS
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms://466453"]];
}
-(IBAction)openBrowser {//打开浏览器
// Lanuch any iPhone developers fav site
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://itunesconnect.apple.com"]];
}
那怎样来制作从一个应用打开其他应用,这其实很简单,打开info.plist,添加一项URL types,展开URL types,再展开Item1,将Item1下的URL identifier修改为URL Scheme,展开URL Scheme,将Item1的内容修改为myapp其他程序可通过myapp://访问此自定义URL。
其实就是类似下面的样式。
这样就只要open这个应用的自定义url,系统就可以帮我们找到并打开这个程序。
NSURL *url = [NSURL URLWithString:@" myapp:"];
[[UIApplication sharedApplication] openURL:url];
作为SDK比普通应用的优势在于,每一个接入的应用都有一个AppId用于区分,我们就可以充分利用这个AppId来制作。
我们可以要求第三方开发者需要在他们Info.Plist中配置这样的字段,这样我们就可以在我们的SDK界面中打开对应AppId的应用,当然,这需要设备中真的有安装这个程序。
例如某应用分配AppId为111122223333,我们要求其再Info.plist定义URL Schemes为NDSDK111122223333,这样,我们在内部代码就可以准确识别是否有这样的程序。
更有甚者,我们可以通过canOpenURL这个方法来判断这台设备是否安装了这个应用,如果可以打开,返回YES,那应该是有安装这样的程序,不管是ipa还是Pxl的程序,应该都是没有问题的。
如果我们真的选择这样子做,那就需要在文档中说明清楚。但是需要注意的是,也许作为程序员,可能不是很喜欢看文档,也许你费尽心思写的文档他并没有看到。这时我们应该来一点强硬的手段,于是有了下面这段代码的功能。
1:检查用户是否配置了AppId
2:有没有准确配置Info的CFBundleURLSchemes字段
3:是不是可以正确打开。
// Check App ID:
// This is really a warning for the developer, this should not
// happen in a completed app
if (!kAppId) {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Setup Error"
message:@"Missing app ID. You cannot run the app until you provide this in the code."
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil,
nil];
[alertView show];
[alertView release];
} else {
// Now check that the URL scheme fb[app_id]://authorize is in the .plist and can
// be opened, doing a simple check without local app id factored in here
NSString *url = [NSString stringWithFormat:@"fb%@://authorize",kAppId];
BOOL bSchemeInPlist = NO; // find out if the sceme is in the plist file.
NSArray* aBundleURLTypes = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleURLTypes"];
if ([aBundleURLTypes isKindOfClass:[NSArray class]] &&
([aBundleURLTypes count] > 0)) {
NSDictionary* aBundleURLTypes0 = [aBundleURLTypes objectAtIndex:0];
if ([aBundleURLTypes0 isKindOfClass:[NSDictionary class]]) {
NSArray* aBundleURLSchemes = [aBundleURLTypes0 objectForKey:@"CFBundleURLSchemes"];
if ([aBundleURLSchemes isKindOfClass:[NSArray class]] &&
([aBundleURLSchemes count] > 0)) {
NSString *scheme = [aBundleURLSchemes objectAtIndex:0];
if ([scheme isKindOfClass:[NSString class]] &&
[url hasPrefix:scheme]) {
bSchemeInPlist = YES;
}
}
}
}
// Check if the authorization callback will work
BOOL bCanOpenUrl = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString: url]];
if (!bSchemeInPlist || !bCanOpenUrl) {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Setup Error"
message:@"Invalid or missing URL scheme. You cannot run the app until you set up a valid URL scheme in your .plist."
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil,
nil];
[alertView show];
[alertView release];
}
}