extends:http://blog.csdn.net/h7870181/article/details/12505883

Google Maps API 网络服务

官网地址 :

https://developers.google.com/maps/documentation/webservices/?hl=zh-cn

其实就是一些接口,供我们调用,如:

1、根据地址获取经纬度

http://maps.google.com/maps/api/geocode/json?address=北京&language=zh-CN&sensor=false

2、计算路线数据

http://maps.googleapis.com/maps/api/directions/json?origin=北京&destination=上海&sensor=false&mode=driving

3、根据经纬度获取详细地址

http://maps.google.com/maps/api/geocode/json?latlng="latlng"&language=zh-CN&sensor=false

等等还有很多,大家可以自己去找找

给大家介绍一下如果利用这些接口

实现网络定位:

首先获取经纬度

/**
* 获取本地
* @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,我这里写了一个工具类

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 url
* @return
*/
private Object getValues(Map<String, Object> params, String url) {
String token = "";
HttpResponse response = post(params, url);
if (response != null) {
try {
token = EntityUtils.toString(response.getEntity());
response.removeHeaders("operator");
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return token;
} /**
* 根据google API 获取两地的路线
* @param origin 起点
* @param destination 终点
* @param mode 出行方式 driving驾车, walking步行, bicycling自行车, transit公交车
* @param sensor 是否来自装有位置传感器的设备 true Or false
* @return
*/
public Object getRoutes(String origin, String destination) {
String url = "http://maps.googleapis.com/maps/api/directions/json?origin="+ origin +"&destination="+ destination +"&sensor=false&mode=driving&region=zh";
return getValues(null, url);
} /**
* 根据经纬度 获取地理位置
* LatLng 经纬度以逗号隔开 纬度,经度
* @return
*/
public Object getAddress(String latlng) {
String url = "http://maps.google.com/maps/api/geocode/json?latlng="+latlng+"&language=zh-CN&sensor=false";
return getValues(null, url);
} /**
* 根据地址获取经纬度
* @return
*/
public Object getLatlng(String str) {
String url = "http://maps.google.com/maps/api/geocode/json?address="+ str+"&language=zh-CN&sensor=false";
return getValues(null, url);
}
}

调用getAddress()方法 传递经纬度来获取详细地址 返回的是JSON字符串,大家解析一下就可以

可以得到起点到终点的时间和路程

调用getRoutes() 方法,传起点和终点

MapsApiUtils.getInstance().getLatLng("39.90403,116.407526");  

根据地址获取经纬度

 

Android Google Maps API 网络服务用于网络定位、计算路线、获取经纬度、获取详细地址等的更多相关文章

  1. Google Maps API V3 之 路线服务

    Google官方教程: Google 地图 API V3 使用入门 Google 地图 API V3 针对移动设备进行开发 Google 地图 API V3 之事件 Google 地图 API V3 ...

  2. Google Maps API V3 之 图层

    Google官方教程: Google 地图 API V3 使用入门 Google 地图 API V3 针对移动设备进行开发 Google 地图 API V3 之事件 Google 地图 API V3 ...

  3. Google Maps API Web Services

    原文:Google Maps API Web Services 摘自:https://developers.google.com/maps/documentation/webservices/ Goo ...

  4. Google Maps API V3 之绘图库 信息窗口

    Google官方教程: Google 地图 API V3 使用入门 Google 地图 API V3 针对移动设备进行开发 Google 地图 API V3 之事件 Google 地图 API V3 ...

  5. Google Maps API 将开始收费

    Google Maps API 将开始收费 一.总结 一句话总结:国外的话openstreetmap或许不错 国内的话就高德吧 二.Google Maps API 将开始收费 曾经免费的 Google ...

  6. Google Maps API v2密钥申请

    1. 进入到Google APIs Console页面 https://code.google.com/apis/console/ 点击左边导航栏的Seivices进入 在All services 的 ...

  7. Android Google Maps 开始

    由于工作需要,最近对Android的各大地图进行了试用. 其中有Google地图,百度地图,高德地图,还有开源的OSM. 在使用Google地图的时候,官网流程写的非常清楚,但是其中也遇到一些问题.这 ...

  8. google maps api申请的问题

    现在已经改由统一的GOOGLE API控制台进行所有GOOGLE API的管理了. 方法是使用Google帐号登入 https://code.google.com/apis/console. 然后在所 ...

  9. Google maps API开发(一)(转)

    一.加载Google maps API <script type="text/javascript" src="http://ditu.google.com/map ...

随机推荐

  1. 内存管理 初始化(四)mem_init bootmem 迁移至伙伴系统

    mm_init中执行mem_init,将原通过bootmem分配器管理的低端内存 及  通过meminfo得知的高端内存释放到伙伴系统中,最后bootmem位图本身占用的低端内存物理页也被释放进伙伴系 ...

  2. level1 - unit 1 - 句子结构

    preface 学习英语做为一种爱好,就是希望有一天能够和老外流畅沟通,目前来看,日常沟通还是没有问题的和我的外教. 知识日积月累,现在就把以前学习的(从初中到现在)知识总结下.每天一更,更到单元总结 ...

  3. oracle18c linux x86-64 install 杂记

    132 yum install libstdc++-devel 133 yum install compat-libstdc++-33 135 yum install compat-libcap1 1 ...

  4. 关于android 内存的笔记

    原文 https://developer.android.com/training/articles/memory.html 1.慎重使用Service,最好的办法是使用IntentService,一 ...

  5. 通过MyEclipse部署web应用程序开发环境

    1.下载并安装MyEclipse10 2.配置java开发的jar包 3.配置tomcat服务器 4.部署tomcat服务器 点击Bronse可以查看部署后的文件夹目录 5.启动tomcat服务器 6 ...

  6. 关于Android打版本号的小白文

    尽管常常和android打交道.但事实上我对android不是非常了解. 这里记录一下ant编译androidproject的过程,然后顺便记录一下android的一些基本概念.不求渡人,但求渡己.这 ...

  7. RedHat6.5-Linux安装telnet服务

    1 下载以下三个包 telnet-0.17-47.el6.x86_64.rpm(telnet客户端) telnet-server-0.17-47.el6.x86_64.rpm(telnet服务端) x ...

  8. SpringMVC由浅入深day01_1springmvc框架介绍

    springmvc 第一天 springmvc的基础知识 课程安排: 第一天:springmvc的基础知识 什么是springmvc? springmvc框架原理(掌握) 前端控制器.处理器映射器.处 ...

  9. python的httplib、urllib和urllib2的区别及用

    慢慢的把它们总结一下,总结就是最好的学习方法 宗述 首先来看一下他们的区别 urllib和urllib2 urllib 和urllib2都是接受URL请求的相关模块,但是urllib2可以接受一个Re ...

  10. 文件完整性hash验证demo(python脚本)

    一个简单的文件完整性hash验证脚本 #!/usr/bin/env python # -*- coding: utf- -*- import os import hashlib import json ...