1) get the debug keystore from command line
keytool -list -alias androiddebugkey -keystore "C:\Documents and Settings\Administrator\Local Settings\Application Data\
Android\debug.keystore" -storepass android -keypass android
keytool -list -alias androiddebugkey -keystore "C:\Documents and Settings\Administrator\Local Settings\Application Data\
Android\debug.keystore" -storepass android -keypass android
2) Go to http://code.google.com/intl/zh-TW/android/maps-api-signup.html ,type your MD5 fingerprint, so that you can get the map api key as follow.
3) in AndroidManifest.xml, add
<uses-library android:name="com.google.android.maps"/>
and
<uses-permission android:name="android.permission.INTERNET" />
<uses-library android:name="com.google.android.maps"/>
and
<uses-permission android:name="android.permission.INTERNET" />
Below class is usefull to show direction path between two routes in android
public class MapActivity extends FragmentActivity {
private static LatLng target_position =null;
private static LatLng souce_position = null;
String locationValue=null,addressValue=null;
Double latitudeValue=0.0,longitudeValue=0.0;
GoogleMap googleMap;
GPSTracker gpstracker=null;
final String TAG = "PathGoogleMapActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_path_google_map);
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
googleMap = fm.getMap();
gpstracker=new GPSTracker(this);
locationValue=getIntent().getExtras().getString("location");
addressValue=convertLatLong(locationValue);
String loc[]=addressValue.split("\\|");
String s1=loc[0].toString();
String s2=loc[1].toString();
target_position= new LatLng(Double.parseDouble(s1),Double.parseDouble(s2));
souce_position=new LatLng(gpstracker.getLatitude(), gpstracker.getLongitude());
MarkerOptions options = new MarkerOptions();
options.position(souce_position);
//options.position(BROOKLYN_BRIDGE);
options.position(target_position);
googleMap.addMarker(options);
String url = getMapsApiDirectionsUrl();
ReadTask downloadTask = new ReadTask();
downloadTask.execute(url);
/* googleMap.setBuildingsEnabled(false);
googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
googleMap.setTrafficEnabled(false);*/
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(target_position,4));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(5), 2000, null);
addMarkers();
}
private String getMapsApiDirectionsUrl() {
String waypoints = "waypoints=optimize:true|"
+ target_position.latitude + "," + target_position.longitude
+ "|" + "|"+ souce_position.latitude + ","
+ souce_position.longitude;
String sensor = "sensor=false";
String params = waypoints + "&" + sensor;
String output = "json";
String url = "https://maps.googleapis.com/maps/api/directions/"
+ output + "?" + params;
return url;
}
private void addMarkers() {
if (googleMap != null) {
googleMap.addMarker(new MarkerOptions().position(target_position)
.title("source"));
googleMap.addMarker(new MarkerOptions().position(souce_position)
.title("Destination"));
}
}
private class ReadTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... url) {
String data = "";
try {
HttpConnection http = new HttpConnection();
data = http.readUrl(url[0]);
} catch (Exception e) {
Log.d("Background Task", e.toString());
}
return data;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
new ParserTask().execute(result);
}
}
private class ParserTask extends
AsyncTask<String, Integer, List<List<HashMap<String, String>>>> {
@Override
protected List<List<HashMap<String, String>>> doInBackground(
String... jsonData) {
JSONObject jObject;
List<List<HashMap<String, String>>> routes = null;
try {
jObject = new JSONObject(jsonData[0]);
PathJSONParser parser = new PathJSONParser();
routes = parser.parse(jObject);
} catch (Exception e) {
e.printStackTrace();
}
return routes;
}
@Override
protected void onPostExecute(List<List<HashMap<String, String>>> routes) {
ArrayList<LatLng> points = null;
PolylineOptions polyLineOptions = null;
// traversing through routes
for (int i = 0; i < routes.size(); i++) {
points = new ArrayList<LatLng>();
polyLineOptions = new PolylineOptions();
List<HashMap<String, String>> path = routes.get(i);
for (int j = 0; j < path.size(); j++) {
HashMap<String, String> point = path.get(j);
double lat = Double.parseDouble(point.get("lat"));
double lng = Double.parseDouble(point.get("lng"));
LatLng position = new LatLng(lat, lng);
points.add(position);
}
polyLineOptions.addAll(points);
polyLineOptions.width(2);
polyLineOptions.color(Color.BLUE);
}
googleMap.addPolyline(polyLineOptions);
}
}
public String convertLatLong(String cityName)
{
/* String location=cityName;
String inputLine = "";
String result = "", lat="",longi="";
location=location.replaceAll(" ", "%20");
String myUrl="http://maps.google.com/maps/geo?q="+location+"&output=csv";
try{
URL url=new URL(myUrl);
URLConnection urlConnection=url.openConnection();
BufferedReader in = new BufferedReader(new
InputStreamReader(urlConnection.getInputStream()));
while ((inputLine = in.readLine()) != null) {
result=inputLine;
}
lat = result.substring(6, result.lastIndexOf(","));
longi = result.substring(result.lastIndexOf(",") + 1);
}
catch(Exception e){
e.printStackTrace();
}
*/
Address locationa=null;
Geocoder coder=null;
String location=cityName;
double locationa1=0,locationa2 = 0;
coder = new Geocoder(this,
Locale.getDefault());
List<Address> address;
try {
address = coder.getFromLocationName(location,5);
if (address == null) {
// Log.d(TAG, "############Address not correct #########");
System.out.println("Adress not correct");
}
locationa1 = address.get(0).getLatitude();
locationa2 = address.get(0).getLongitude();
}
catch(Exception e)
{
}
return locationa1+"|"+locationa2;
}
}
private static LatLng target_position =null;
private static LatLng souce_position = null;
String locationValue=null,addressValue=null;
Double latitudeValue=0.0,longitudeValue=0.0;
GoogleMap googleMap;
GPSTracker gpstracker=null;
final String TAG = "PathGoogleMapActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_path_google_map);
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
googleMap = fm.getMap();
gpstracker=new GPSTracker(this);
locationValue=getIntent().getExtras().getString("location");
addressValue=convertLatLong(locationValue);
String loc[]=addressValue.split("\\|");
String s1=loc[0].toString();
String s2=loc[1].toString();
target_position= new LatLng(Double.parseDouble(s1),Double.parseDouble(s2));
souce_position=new LatLng(gpstracker.getLatitude(), gpstracker.getLongitude());
MarkerOptions options = new MarkerOptions();
options.position(souce_position);
//options.position(BROOKLYN_BRIDGE);
options.position(target_position);
googleMap.addMarker(options);
String url = getMapsApiDirectionsUrl();
ReadTask downloadTask = new ReadTask();
downloadTask.execute(url);
/* googleMap.setBuildingsEnabled(false);
googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
googleMap.setTrafficEnabled(false);*/
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(target_position,4));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(5), 2000, null);
addMarkers();
}
private String getMapsApiDirectionsUrl() {
String waypoints = "waypoints=optimize:true|"
+ target_position.latitude + "," + target_position.longitude
+ "|" + "|"+ souce_position.latitude + ","
+ souce_position.longitude;
String sensor = "sensor=false";
String params = waypoints + "&" + sensor;
String output = "json";
String url = "https://maps.googleapis.com/maps/api/directions/"
+ output + "?" + params;
return url;
}
private void addMarkers() {
if (googleMap != null) {
googleMap.addMarker(new MarkerOptions().position(target_position)
.title("source"));
googleMap.addMarker(new MarkerOptions().position(souce_position)
.title("Destination"));
}
}
private class ReadTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... url) {
String data = "";
try {
HttpConnection http = new HttpConnection();
data = http.readUrl(url[0]);
} catch (Exception e) {
Log.d("Background Task", e.toString());
}
return data;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
new ParserTask().execute(result);
}
}
private class ParserTask extends
AsyncTask<String, Integer, List<List<HashMap<String, String>>>> {
@Override
protected List<List<HashMap<String, String>>> doInBackground(
String... jsonData) {
JSONObject jObject;
List<List<HashMap<String, String>>> routes = null;
try {
jObject = new JSONObject(jsonData[0]);
PathJSONParser parser = new PathJSONParser();
routes = parser.parse(jObject);
} catch (Exception e) {
e.printStackTrace();
}
return routes;
}
@Override
protected void onPostExecute(List<List<HashMap<String, String>>> routes) {
ArrayList<LatLng> points = null;
PolylineOptions polyLineOptions = null;
// traversing through routes
for (int i = 0; i < routes.size(); i++) {
points = new ArrayList<LatLng>();
polyLineOptions = new PolylineOptions();
List<HashMap<String, String>> path = routes.get(i);
for (int j = 0; j < path.size(); j++) {
HashMap<String, String> point = path.get(j);
double lat = Double.parseDouble(point.get("lat"));
double lng = Double.parseDouble(point.get("lng"));
LatLng position = new LatLng(lat, lng);
points.add(position);
}
polyLineOptions.addAll(points);
polyLineOptions.width(2);
polyLineOptions.color(Color.BLUE);
}
googleMap.addPolyline(polyLineOptions);
}
}
public String convertLatLong(String cityName)
{
/* String location=cityName;
String inputLine = "";
String result = "", lat="",longi="";
location=location.replaceAll(" ", "%20");
String myUrl="http://maps.google.com/maps/geo?q="+location+"&output=csv";
try{
URL url=new URL(myUrl);
URLConnection urlConnection=url.openConnection();
BufferedReader in = new BufferedReader(new
InputStreamReader(urlConnection.getInputStream()));
while ((inputLine = in.readLine()) != null) {
result=inputLine;
}
lat = result.substring(6, result.lastIndexOf(","));
longi = result.substring(result.lastIndexOf(",") + 1);
}
catch(Exception e){
e.printStackTrace();
}
*/
Address locationa=null;
Geocoder coder=null;
String location=cityName;
double locationa1=0,locationa2 = 0;
coder = new Geocoder(this,
Locale.getDefault());
List<Address> address;
try {
address = coder.getFromLocationName(location,5);
if (address == null) {
// Log.d(TAG, "############Address not correct #########");
System.out.println("Adress not correct");
}
locationa1 = address.get(0).getLatitude();
locationa2 = address.get(0).getLongitude();
}
catch(Exception e)
{
}
return locationa1+"|"+locationa2;
}
}
No comments:
Post a Comment