汽车导航

在LinearLayout界面中

<LinearLayout 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"
    android:orientation="vertical" >


    <Button 
        android:id="@+id/gps"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="开启GPS定位"
        android:gravity="center"
        />
    <Button 
        android:id="@+id/baidu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="开启百度定位"
        android:gravity="center"
        />
    <Button 
        android:id="@+id/amap"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="开启高德定位"
        android:gravity="center"
        />
    <TextView 
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

</LinearLayout>


在MainActivity中

package com.example.daohangdemo;

import java.util.Date;
import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.LocationManagerProxy;
import com.amap.api.location.LocationProviderProxy;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;
import com.example.daohangdemo.R;

public class MainActivity extends Activity implements OnClickListener{

private TextView mTextView;
private Button gpsBtn, baiduBtn, amapBtn;
private LocationManager gpsManager;
private LocationClient baduduManager;
private LocationManagerProxy aMapManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.text);
gpsBtn = (Button) findViewById(R.id.gps);
baiduBtn = (Button) findViewById(R.id.baidu);
amapBtn = (Button) findViewById(R.id.amap);
gpsBtn.setOnClickListener(this);
baiduBtn.setOnClickListener(this);
amapBtn.setOnClickListener(this);
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.gps:
if (gpsBtn.getText().toString().equals("开启GPS定位")) {
startGps();
gpsBtn.setText("停止GPS定位");
} else {
stopGps();
gpsBtn.setText("开启GPS定位");
}
break;
case R.id.baidu:
if (baiduBtn.getText().toString().equals("开启百度定位")) {
startBaidu();
baiduBtn.setText("停止百度定位");
} else {
stopBaidu();
baiduBtn.setText("开启百度定位");
}
break;
case R.id.amap:
if (amapBtn.getText().toString().equals("开启高德定位")) {
startAmap();
amapBtn.setText("停止高德定位");
} else {
stopAmap();
amapBtn.setText("开启高德定位");
}
break;
default:
break;
}
}

private void startAmap() {
aMapManager = LocationManagerProxy.getInstance(this);
aMapManager.requestLocationUpdates(LocationProviderProxy.AMapNetwork, 2000, 10, mAMapLocationListener);
}

private void stopAmap() {
if (aMapManager != null) {
aMapManager.removeUpdates(mAMapLocationListener);
aMapManager.destory();
}
aMapManager = null;
}

private void startBaidu() {
if (baduduManager == null) {
baduduManager = new LocationClient(this);
LocationClientOption option = new LocationClientOption();
option.setLocationMode(LocationMode.Hight_Accuracy); 
option.setCoorType("gcj02"); 
option.setScanSpan(1000);
option.setIsNeedAddress(true);
baduduManager.setLocOption(option);
baduduManager.registerLocationListener(mBdLocationListener);
}
baduduManager.start();
}

private void stopBaidu() {
baduduManager.stop();
}

private void startGps() {
gpsManager = (LocationManager) getSystemService(LOCATION_SERVICE);
String provider = gpsManager.getProvider(LocationManager.GPS_PROVIDER).getName();
gpsManager.requestLocationUpdates(provider, 3000, 10, gpsListener);
}

private void stopGps() {
gpsManager.removeUpdates(gpsListener);
}

private LocationListener gpsListener = new LocationListener() {

@Override
public void onLocationChanged(Location location) {
Log.e("Location", "onLocationChanged");
double latitude = location.getLatitude();
double longitude = location.getLongitude();
float speed = location.getSpeed();
long time = location.getTime();
String s = "latitude--->" + latitude
+  "  longitude--->" + longitude
+  "  speed--->" + speed 
+  "  time--->" + new Date(time).toLocaleString();
mTextView.setText("GPS定位\n" + s);
}
@Override
public void onProviderDisabled(String provider) {
Log.e("Location", "onProviderDisabled");
}
@Override
public void onProviderEnabled(String provider) {
Log.e("Location", "onProviderEnabled");
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.e("Location", "onStatusChanged");
}
};

private BDLocationListener mBdLocationListener = new BDLocationListener() {
@Override
public void onReceiveLocation(BDLocation location) {
//Receive Location 
StringBuffer sb = new StringBuffer(256);
sb.append("time : ");
sb.append(location.getTime());
sb.append("\nerror code : ");
sb.append(location.getLocType());
sb.append("\nlatitude : ");
sb.append(location.getLatitude());
sb.append("\nlontitude : ");
sb.append(location.getLongitude());
sb.append("\nradius : ");
sb.append(location.getRadius());
if (location.getLocType() == BDLocation.TypeGpsLocation){
sb.append("\nspeed : ");
sb.append(location.getSpeed());
sb.append("\nsatellite : ");
sb.append(location.getSatelliteNumber());
sb.append("\ndirection : ");
sb.append("\naddr : ");
sb.append(location.getAddrStr());
sb.append(location.getDirection());
} else if (location.getLocType() == BDLocation.TypeNetWorkLocation){
sb.append("\naddr : ");
sb.append(location.getAddrStr());
sb.append("\noperationers : ");
sb.append(location.getOperators());
}
mTextView.setText("百度定位\n" + sb.toString());
}
};

private AMapLocationListener mAMapLocationListener = new AMapLocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onLocationChanged(AMapLocation location) {
if (location != null) {
Double geoLat = location.getLatitude();
Double geoLng = location.getLongitude();
String cityCode = "";
String desc = "";
Bundle locBundle = location.getExtras();
if (locBundle != null) {
cityCode = locBundle.getString("citycode");
desc = locBundle.getString("desc");
}
String str = ("定位成功:(" + geoLng + "," + geoLat + ")"
+ "\n精    度    :" + location.getAccuracy() + "米"
+ "\n定位方式:" + location.getProvider() + "\n定位时间:"
+ new Date(location.getTime()).toLocaleString() + "\n城市编码:"
+ cityCode + "\n位置描述:" + desc + "\n省:"
+ location.getProvince() + "\n市:" + location.getCity()
+ "\n区(县):" + location.getDistrict() + "\n区域编码:" + location
.getAdCode());
mTextView.setText("高德定位\n" + str);
}
}
};
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值