PhoneGap 手机百度地图定位插件

虽然不难。。能简单搞定的就简单搞。。能复制就复制。

插件地址:https://github.com/DoubleSpout/phonegap_baidu_sdk_location

原文博客:http://snoopyxdy.blog.163.com/blog/static/601174402014420872345/

<?xml version="1.0" encoding="UTF-8"?>

<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0" id="com.spout.phonegap.plugins.baidulocation" version="0.1.0">
    <name>BaiduLocation</name>
    <description>Baidu Location Plugin for Phonegap</description>
    <license>MIT</license>
    <keywords>baidu, location, phonegap</keywords>

    <!-- android -->
    <platform name="android">
        <js-module src="www/baidulocation.js" name="BiaduLocation">
            <clobbers target="window.baiduLocation" />
        </js-module>

        <config-file target="res/xml/config.xml" parent="/*">
            <feature name="BaiduLocation">
                <param name="android-package" value="com.spout.phonegap.plugins.baidulocation.BaiduLocation"/>
            </feature>
        </config-file> 

        <config-file target="AndroidManifest.xml" parent="/*">
            <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
            <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
            <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
            <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
            <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
            <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
            <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
            <uses-permission android:name="android.permission.INTERNET" />
            <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>
            <uses-permission android:name="android.permission.READ_LOGS"></uses-permission>
        </config-file>

        <config-file target="AndroidManifest.xml" parent="/manifest/application">
            <service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote"></service>
        </config-file>

        <source-file src="src/android/BaiduLocation.java" target-dir="src/com/spout/phonegap/plugins/baidulocation" />   
        <source-file src="src/android/locSDK_4.0.jar" target-dir="libs" framework="true"/>      
        <source-file src="src/android/liblocSDK4.so" target-dir="libs/armeabi" framework="true"/>   
    </platform>         
</plugin>

将上述地址配置到指定文件即可.

需要留意版本,本人就还是在用android 4.1 cordova 用的是2.8版本,所以稍微麻烦了点

莫鄙视…

补充一个2.8版本的百度地图定位

package com.spout.phonegap.plugins.baidulocation;

import java.util.HashMap;
import java.util.Map;

import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;

public class BaiduLocation extends Plugin {

	public static final String PLUGIN_NAME = "BaiduLocation";
	NotificationManager notificationManager;
	Notification note;
	PendingIntent contentIntent;

	public CallbackContext callbackContext;
	private static final String STOP_ACTION = "stop";
	private static final String GET_ACTION = "getCurrentPosition";
	public LocationClient locationClient = null;
	public JSONObject jsonObj = new JSONObject();
	public boolean result = false;

	public BDLocationListener myListener;

	private static final Map<Integer, String> ERROR_MESSAGE_MAP = new HashMap<Integer, String>();

	private static final String DEFAULT_ERROR_MESSAGE = "服务端定位失败";

	static {
		ERROR_MESSAGE_MAP.put(61, "GPS定位结果");
		ERROR_MESSAGE_MAP.put(62, "扫描整合定位依据失败。此时定位结果无效");
		ERROR_MESSAGE_MAP.put(63, "网络异常,没有成功向服务器发起请求。此时定位结果无效");
		ERROR_MESSAGE_MAP.put(65, "定位缓存的结果");
		ERROR_MESSAGE_MAP.put(66, "离线定位结果。通过requestOfflineLocaiton调用时对应的返回结果");
		ERROR_MESSAGE_MAP.put(67, "离线定位失败。通过requestOfflineLocaiton调用时对应的返回结果");
		ERROR_MESSAGE_MAP.put(68, "网络连接失败时,查找本地离线定位时对应的返回结果。");
		ERROR_MESSAGE_MAP.put(161, "表示网络定位结果");
	};

	public String getErrorMessage(int locationType) {
		String result = ERROR_MESSAGE_MAP.get(locationType);
		if (result == null) {
			result = DEFAULT_ERROR_MESSAGE;
		}
		return result;
	}
	private void logMsg(String s) {
		System.out.println(s);
	}

	public CallbackContext getCallbackContext() {
		return callbackContext;
	}

	public void setCallbackContext(CallbackContext callbackContext) {
		this.callbackContext = callbackContext;
	}
	@Override
	public boolean execute(String action, JSONArray args,
			final CallbackContext callbackContext) throws JSONException {
		Log.d(PLUGIN_NAME, "Plugin execute called with action: " + action);

		setCallbackContext(callbackContext);
		if (GET_ACTION.equals(action)) {
			cordova.getActivity().runOnUiThread(new Runnable() {
				@Override
				public void run() {

					Log.d(PLUGIN_NAME, "run: ");
					locationClient = new LocationClient(cordova.getActivity());
					locationClient.setAK("BfkPvjDGHC0ATZhIr6wxnHh9");// 设置百度的ak
					myListener = new MyLocationListener();
					locationClient.registerLocationListener(myListener);
					LocationClientOption option = new LocationClientOption();
					option.setOpenGps(true);
					option.setCoorType("bd09ll");// 返回的定位结果是百度经纬度,默认值gcj02
					option.setProdName("BaiduLoc");
					option.disableCache(true);// 禁止启用缓存定位
					locationClient.setLocOption(option);

					locationClient.start();
					locationClient.requestLocation();

				}

			});
		} else if (STOP_ACTION.equals(action)) {
			locationClient.stop();
		}

		while (result == false) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		return true;
	}
	@Override
	public void onDestroy() {
		if (locationClient != null && locationClient.isStarted()) {
			locationClient.stop();
			locationClient = null;
		}
		super.onDestroy();
	}

	public class MyLocationListener implements BDLocationListener {
		@Override
		public void onReceiveLocation(BDLocation location) {
			if (location == null)
				return;
			try {

				Log.d(PLUGIN_NAME, "MyLocationListener");
				JSONObject coords = new JSONObject();
				coords.put("latitude", location.getLatitude());
				coords.put("longitude", location.getLongitude());
				coords.put("radius", location.getRadius());

				jsonObj.put("coords", coords);

				int locationType = location.getLocType();

				jsonObj.put("locationType", locationType);
				jsonObj.put("code", locationType);
				jsonObj.put("message", getErrorMessage(locationType));

				switch (location.getLocType()) {

				case BDLocation.TypeGpsLocation:
					coords.put("speed", location.getSpeed());
					coords.put("altitude", location.getAltitude());
					jsonObj.put("SatelliteNumber",
							location.getSatelliteNumber());
					break;

				case BDLocation.TypeNetWorkLocation:
					jsonObj.put("addr", location.getAddrStr());
					break;
				}

				Log.d("BaiduLocationPlugin", "run: " + jsonObj.toString());
				callbackContext.success(jsonObj);
				result = true;
			} catch (JSONException e) {
				callbackContext.error(e.getMessage());
				result = true;
			}

		}

		@Override
		public void onReceivePoi(BDLocation arg0) {
			// TODO Auto-generated method stub

		}
	}

	@SuppressWarnings("unused")
	@Override
	public PluginResult execute(String arg0, JSONArray arg1, String arg2) {

		Log.d("BaiduLocationPlugin", "run: xxxx" );
		return new PluginResult(true ? PluginResult.Status.OK
				: PluginResult.Status.ERROR);
		// TODO Auto-generated method stub
	}

}

 

 

 

DISCUZ 特殊积分解决方案

最近discuz有个小功能需求,需要实现送某某积分的操作,但是是对外的。不可进行其它操作。下面用B积分进行表示

只是需要在积分日志列表上进行显示,但是又不可对该积分进行操作。考虑到不影响discuz的系统.使用扩展积分8类型,但是该积分是关闭状态,无法进行其它操作。

送B积分时,使用类型参数8进行标志。如:

							updatemembercount ( array (
									// 用户id
							), array (
									8 => $score  //分值
							), true, '', 1, '', '标题','内容描述' );

然后前台显示时,在插件钩子处,挂上类型8的显示,只在特殊地方进行显示。别的地方调用时,也不会出现什么影响

如:

class plugin_xxxxx_home extends plugin_gd_jifen {
	function spacecp_credit_extra() {
			$_G ['setting'] ['extcredits'] [8] = array (
					'img' => '',
					'title' => 'B积分',
					'unit' => '',
					'ratio' => 0,
					'showinthread' => '0',
					'allowexchangein' => '',
					'allowexchangeout' => '',
					'hiddeninheader' => true 
			);
	}
}

前台显示赠送的B积分提示

原理:http://witmax.cn/discuz-credits-analysis.html

但是具体要提示的,需要补充下面的代码:

$_G ['setting'] ['creditnames'] .= ',8|B|';

页面通过判断html里是否有该标志进行对应的提示

soap 超时处理

class SoapClientX extends SoapClient {
	private $timeout;
	function __construct($wdsl, $options = array()) {
		// 考虑到不和原函数冲突.
		if (isset ( $options ['timeout'] )) {
			$this->timeout = intval ( $options ['timeout'] );
		} else {
			$this->timeout = 10;
		}
		parent::__construct ( $wdsl, $options );
	}
	public function __doRequest($request, $location, $action, $version, $one_way = false) {
		ini_set ( 'default_socket_timeout', $this->timeout );
		try {
			// 当超时返回空时,表示非正常报文返回,出现"Error Fetching http headers"异常
			$response = parent::__doRequest ( $request, $location, $action, $version, $one_way );
		} catch ( Exception $e ) {
			if (stristr ( $e->getMessage (), "Error Fetching http headers" )) {
				throw new SoapFault ( 'timeout', "请求超时" );
			}
		}
		if (! $response) {
			throw new SoapFault ( 'timeout', "请求超时" );
		}
		return ($response);
	}
}

 

看完 新概念二册

前几天刚看完第二册。

先看中文,翻译回英文,再去看课文的方式对内容的印象相当深,不过也花了不少时间。不过值了~

现在听朋友球的建议,巩固。从第一册到第二册重新过,然后按 先翻译再看课文的方式学习,加深印象,巩固基础。再去看第三册。

里面的老师说了句很实在的话:

英语这门语言,并不一定越理解越用得好,还是需要经常锻炼来熟悉它。

继续继续~