百度地图

一 效果图

       代码中没有错误 但是无法运行。错误如下:


二  代码

1.MainActivity.java
package bzu.cn.wyd.example.baidumaptest;

import java.util.List;

import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.model.LatLng;


import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

import android.app.Activity;
import android.content.Context;

import android.widget.Toast;

public class MainActivity extends Activity
{
	private static final Location Listener = null;

	private BaiduMap baidumap;

	private MapView mapView;
	
	private LocationManager locationManager;
	private String provider;
	
	private boolean isFirstLocate =true;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		SDKInitializer.initialize(getApplicationContext());
		setContentView(R.layout.activity_main);
		mapView = (MapView) findViewById(R.id.map_view);
		baidumap=mapView.getMap();
		locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
		List<String> providerList=locationManager.getProviders(true);
		if(providerList.contains(LocationManager.NETWORK_PROVIDER)){
			provider=LocationManager.GPS_PROVIDER;			
		}else if(providerList.contains(LocationManager.NETWORK_PROVIDER)){
			provider=LocationManager.NETWORK_PROVIDER;
			
		}else{
			Toast.makeText(this, "No location provide to use", Toast.LENGTH_SHORT).show();
			return;
		}
		Location location=locationManager.getLastKnownLocation(provider);
		if(location!=null){
			navigateTo(location);			
		}
		locationManager.requestLocationUpdates(provider, 5000, 1, locationListener);		
	}
	private void navigateTo(Location location)
	{
		// TODO Auto-generated method stub
		if(isFirstLocate){
			LatLng ll=new LatLng(location.getAltitude(), location.getLongitude());
			MapStatusUpdate update=MapStatusUpdateFactory.newLatLng(ll);
			baidumap.animateMapStatus(update);
			update=MapStatusUpdateFactory.zoomTo(16f);
			baidumap.animateMapStatus(update);
			isFirstLocate=false;
		}	
	}
	LocationListener locationListener=new LocationListener()
	{
		
		@Override
		public void onStatusChanged(String provider, int status, Bundle extras)
		{
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void onProviderEnabled(String provider)
		{
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void onProviderDisabled(String provider)
		{
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void onLocationChanged(Location location)
		{
			// TODO Auto-generated method stub
			if(location!=null){
				navigateTo(location);
			}
		}
	};
	protected void onDestory(){
		super.onDestroy();
		mapView.onDestroy();
		if(locationManager!=null){
			locationManager.removeUpdates(locationListener);			
		}
	}
	protected void onPause(){
		super.onPause();
		mapView.onPause();
	}	
	protected void onResume(){
		super.onResume();
		mapView.onResume();
	}
}
2.activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


    <com.baidu.mapapi.map.MapView
        android:id="@+id/map_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true" />


</LinearLayout>
3.AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="bzu.cn.wyd.example.baidumaptest"
    android:versionCode="1"
    android:versionName="1.0" >


    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />


    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.USE_CREDENTIALS" />
    <uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
    <uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.BROADCAST_STICKY" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />


    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <meta-data
            android:name="com.baidu.lbsapi.API_KEY"
            android:value="LTLdkcQPlXMZr3m6bujDB47v"/>
        <activity
            android:name="bzu.cn.wyd.example.baidumaptest.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />


                <meta-data
                    android:name="com.baidu.lbsapi.API_KEY"
                    android:value="LTLdkcQPlXMZr3mbujDb47v" />


                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>


</manifest>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值