实现网络定位:
首先获取经纬度
[java]
/**
* 獲取本地
* @param context
* @return
*/
public String getLocation(Context context){
LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
// 返回所有已知的位置提供者的名称列表,包括未获准访问或调用活动目前已停用的。
//List<String> lp = lm.getAllProviders();
Criteria criteria = new Criteria();
criteria.setCostAllowed(false);
//设置位置服务免费
criteria.setAccuracy(Criteria.ACCURACY_COARSE); //设置水平位置精度
//getBestProvider 只有允许访问调用活动的位置供应商将被返回
String providerName = lm.getBestProvider(criteria, true);
if (providerName != null)
{
Location location = lm.getLastKnownLocation(providerName);
if(location!=null){
//获取维度信息
double latitude = location.getLatitude();
//获取经度信息
double longitude = location.getLongitude();
return latitude+","+longitude;
}
}
return "";
}
调用API,我这里写了一个工具类
[java]
package com.techrare.utils;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
public class MapsApiUtils {
private static MapsApiUtils mapsApiUtils = new MapsApiUtils();
/**
* 单例模式
*
* @return
*/
synchronized public static MapsApiUtils getInstance() {
return mapsApiUtils;
}
/**
* 根据API地址和参数获取响应对象HttpResponse
*
* @param params
* @param url
* @return
*/
private HttpResponse post(Map<String, Object> params, String url) {
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("charset", HTTP.UTF_8);
httpPost.setHeader("Content-Type",
"application/x-www-form-urlencoded; charset=utf-8");
HttpResponse response = null;
if (params != null && params.size() > 0) {
List<NameValuePair> nameValuepairs = new ArrayList<NameValuePair>();
for (String key : params.keySet()) {
nameValuepairs.add(new BasicNameValuePair(key, (String) params
.get(key)));
}
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuepairs,
HTTP.UTF_8));
response = client.execute(httpPost);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (RuntimeException e) {
e.printStackTrace();
}
} else {
try {
response = client.execute(httpPost);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return response;
}
/**
* 得到JSON值
*
* @param params
* @param
补充:移动开发 , Android ,