Android Google Maps API Web service for network positioning, computing routes, obtaining latitude and longitude, obtaining detailed addresses, etc.

Source: Internet
Author: User



---restore content starts---



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






Google Maps API Web Service



Website address:



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






is actually some interface for us to invoke, such as:



1, according to the address to obtain latitude and longitude



Http://maps.google.com/maps/api/geocode/json?address= Beijing &language=zh-cn&sensor=false



2. Calculate route data



http://maps.googleapis.com/maps/api/directions/json?origin= Beijing &destination= Shanghai &sensor=false&mode= Driving



3, according to latitude and longitude to obtain the detailed address



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



There's so much more, you can find it for yourself.






Let me introduce you. If you use these interfaces



To achieve network positioning:



Get latitude and longitude first

/ **
 * Get local
 * @param context
 * @return
 * /
public String getLocation (Context context) {
    LocationManager lm = (LocationManager) context.getSystemService (Context.LOCATION_SERVICE);
       // Return a list of the names of all known location providers, including those that have not been granted access or call activities are currently disabled
       // List <String> lp = lm.getAllProviders ();
       Criteria criteria = new Criteria ();
       criteria.setCostAllowed (false);
       // Set location service for free
       criteria.setAccuracy (Criteria.ACCURACY_COARSE); // Set horizontal position accuracy
        // getBestProvider Only the location provider that allows access to the calling activity will be returned
       String providerName = lm.getBestProvider (criteria, true);
  
       if (providerName! = null)
       {
           Location location = lm.getLastKnownLocation (providerName);
           if (location! = null) {
            // Get dimension information
            double latitude = location.getLatitude ();
            // Get longitude information
            double longitude = location.getLongitude ();
            return latitude + "," + longitude;
           }
       }
       return "";
}
 

Call the API, I wrote a tool class here

 

  

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 ();
      
    / **
     * Singleton mode
     *
     * @return
     * /
    synchronized public static MapsApiUtils getInstance () {
        return mapsApiUtils;
    }
      
    / **
     * Get response object HttpResponse according to API address and parameters
     *
     * @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;
    }
      
    / **
     * Get JSON value
     *
     * @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;
    }
      
    / **
     * Get directions of two places according to google API
     * @param origin
     * @param destination
     * @param mode Travel mode driving, walking, bicycling bicycle, transit bus
     * Is @param sensor from a device equipped with a position 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);
    }
      
    / **
     * Get geographic location based on latitude and longitude * LatLng latitude and longitude are separated by comma Latitude, Longitude
     * @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);
    }
      
    / **
     * Get latitude and longitude according to address
     * @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);
    }
}
 

 

Call the getAddress () method and pass the latitude and longitude to get the detailed address. The returned is a JSON string.

 

 
MapsApiUtils.getInstance (). GetAddress (getLocation (context));
 

 

 

Calculate route data

You can get the time and distance from the start to the end

Call the getRoutes () method, passing the start and end points

  

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

 
 

 

Get latitude and longitude based on address

  

 
MapsApiUtils.getInstance (). GetRoutes ("Beijing", "Shanghai");
  

  

Android Google Maps API network service is used for network positioning, calculating route, obtaining latitude and longitude, obtaining detailed address, etc
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.