├── .classpath
├── .gitattributes
├── .gitignore
├── .project
├── .settings
└── org.eclipse.jdt.core.prefs
├── AndroidManifest.xml
├── README.md
├── ic_launcher-web.png
├── libs
└── android-support-v4.jar
├── proguard-project.txt
├── project.properties
├── res
├── drawable-hdpi
│ ├── car.png
│ └── ic_launcher.png
├── drawable-mdpi
│ └── ic_launcher.png
├── drawable-xhdpi
│ └── ic_launcher.png
├── drawable-xxhdpi
│ └── ic_launcher.png
├── layout
│ ├── activity_main.xml
│ ├── main_welcome.xml
│ ├── name_list.xml
│ ├── search_frnd.xml
│ ├── signin.xml
│ └── welcome.xml
├── menu
│ └── main.xml
├── values-v11
│ └── styles.xml
├── values-v14
│ └── styles.xml
├── values-w820dp
│ └── dimens.xml
└── values
│ ├── dimens.xml
│ ├── strings.xml
│ └── styles.xml
└── src
└── com
└── example
└── friendlocation
├── Frnd_Direction.java
├── JSONParser.java
├── LocationListener.java
├── MainActivity.java
├── Main_Welcome.java
├── MoveToFriend.java
├── Search_Frnd.java
├── Signin.java
├── UpdateLocationService.java
└── Welcome.java
/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Auto detect text files and perform LF normalization
2 | * text=auto
3 |
4 | # Custom for Visual Studio
5 | *.cs diff=csharp
6 |
7 | # Standard to msysgit
8 | *.doc diff=astextplain
9 | *.DOC diff=astextplain
10 | *.docx diff=astextplain
11 | *.DOCX diff=astextplain
12 | *.dot diff=astextplain
13 | *.DOT diff=astextplain
14 | *.pdf diff=astextplain
15 | *.PDF diff=astextplain
16 | *.rtf diff=astextplain
17 | *.RTF diff=astextplain
18 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Built application files
2 | *.apk
3 | *.ap_
4 |
5 | # Files for the Dalvik VM
6 | *.dex
7 |
8 | # Java class files
9 | *.class
10 |
11 | # Generated files
12 | bin/
13 | gen/
14 |
15 | # Gradle files
16 | .gradle/
17 | build/
18 |
19 | # Local configuration file (sdk path, etc)
20 | local.properties
21 |
22 | # Proguard folder generated by Eclipse
23 | proguard/
24 |
25 | # Log Files
26 | *.log
27 |
28 | # =========================
29 | # Operating System Files
30 | # =========================
31 |
32 | # OSX
33 | # =========================
34 |
35 | .DS_Store
36 | .AppleDouble
37 | .LSOverride
38 |
39 | # Thumbnails
40 | ._*
41 |
42 | # Files that might appear on external disk
43 | .Spotlight-V100
44 | .Trashes
45 |
46 | # Directories potentially created on remote AFP share
47 | .AppleDB
48 | .AppleDesktop
49 | Network Trash Folder
50 | Temporary Items
51 | .apdisk
52 |
53 | # Windows
54 | # =========================
55 |
56 | # Windows image file caches
57 | Thumbs.db
58 | ehthumbs.db
59 |
60 | # Folder config file
61 | Desktop.ini
62 |
63 | # Recycle Bin used on file shares
64 | $RECYCLE.BIN/
65 |
66 | # Windows Installer files
67 | *.cab
68 | *.msi
69 | *.msm
70 | *.msp
71 |
72 | # Windows shortcuts
73 | *.lnk
74 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | FriendLocation
4 |
5 |
6 |
7 |
8 |
9 | com.android.ide.eclipse.adt.ResourceManagerBuilder
10 |
11 |
12 |
13 |
14 | com.android.ide.eclipse.adt.PreCompilerBuilder
15 |
16 |
17 |
18 |
19 | org.eclipse.jdt.core.javabuilder
20 |
21 |
22 |
23 |
24 | com.android.ide.eclipse.adt.ApkBuilder
25 |
26 |
27 |
28 |
29 |
30 | com.android.ide.eclipse.adt.AndroidNature
31 | org.eclipse.jdt.core.javanature
32 |
33 |
34 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
3 | org.eclipse.jdt.core.compiler.compliance=1.6
4 | org.eclipse.jdt.core.compiler.source=1.6
5 |
--------------------------------------------------------------------------------
/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
10 |
11 |
14 |
15 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
32 |
33 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
52 | "
55 |
56 | "
59 |
60 | "
63 |
64 | "
67 |
68 | "
71 |
72 | "
75 |
76 |
79 |
82 |
83 |
84 |
85 |
86 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Android-Google-Map-2
2 | Android Google Map 2 Project to find friend location with live movement. Login, Registration with Friends search options.
3 |
4 | 
5 |
6 | Tutorial available on http://www.coders-hub.com/2015/02/advance-android-google-map-2-tutorial-with-example.html
7 |
--------------------------------------------------------------------------------
/ic_launcher-web.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jmimohsin/Android-Google-Map-2/e2c1defd6b3144cd423d58d2834ba12b9d101509/ic_launcher-web.png
--------------------------------------------------------------------------------
/libs/android-support-v4.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jmimohsin/Android-Google-Map-2/e2c1defd6b3144cd423d58d2834ba12b9d101509/libs/android-support-v4.jar
--------------------------------------------------------------------------------
/proguard-project.txt:
--------------------------------------------------------------------------------
1 | # To enable ProGuard in your project, edit project.properties
2 | # to define the proguard.config property as described in that file.
3 | #
4 | # Add project specific ProGuard rules here.
5 | # By default, the flags in this file are appended to flags specified
6 | # in ${sdk.dir}/tools/proguard/proguard-android.txt
7 | # You can edit the include path and order by changing the ProGuard
8 | # include property in project.properties.
9 | #
10 | # For more details, see
11 | # http://developer.android.com/guide/developing/tools/proguard.html
12 |
13 | # Add any project specific keep options here:
14 |
15 | # If your project uses WebView with JS, uncomment the following
16 | # and specify the fully qualified class name to the JavaScript interface
17 | # class:
18 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
19 | # public *;
20 | #}
21 |
--------------------------------------------------------------------------------
/project.properties:
--------------------------------------------------------------------------------
1 | # This file is automatically generated by Android Tools.
2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3 | #
4 | # This file must be checked in Version Control Systems.
5 | #
6 | # To customize properties used by the Ant build system edit
7 | # "ant.properties", and override values to adapt the script to your
8 | # project structure.
9 | #
10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
12 |
13 | # Project target.
14 | target=android-19
15 | android.library.reference.1=../appcompat_v7
16 | android.library.reference.2=../google-play-services_lib
17 |
--------------------------------------------------------------------------------
/res/drawable-hdpi/car.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jmimohsin/Android-Google-Map-2/e2c1defd6b3144cd423d58d2834ba12b9d101509/res/drawable-hdpi/car.png
--------------------------------------------------------------------------------
/res/drawable-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jmimohsin/Android-Google-Map-2/e2c1defd6b3144cd423d58d2834ba12b9d101509/res/drawable-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/drawable-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jmimohsin/Android-Google-Map-2/e2c1defd6b3144cd423d58d2834ba12b9d101509/res/drawable-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/drawable-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jmimohsin/Android-Google-Map-2/e2c1defd6b3144cd423d58d2834ba12b9d101509/res/drawable-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/drawable-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jmimohsin/Android-Google-Map-2/e2c1defd6b3144cd423d58d2834ba12b9d101509/res/drawable-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
5 |
6 |
13 |
14 |
25 |
26 |
27 |
28 |
36 |
37 |
48 |
49 |
56 |
57 |
65 |
66 |
--------------------------------------------------------------------------------
/res/layout/main_welcome.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
14 |
15 |
24 |
25 |
34 |
35 |
44 |
45 |
--------------------------------------------------------------------------------
/res/layout/name_list.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
13 |
14 |
21 |
22 |
29 |
30 |
37 |
38 |
45 |
46 |
47 |
--------------------------------------------------------------------------------
/res/layout/search_frnd.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
11 |
12 |
21 |
22 |
23 |
24 |
30 |
31 |
32 |
33 |
37 |
38 |
39 |
40 |
--------------------------------------------------------------------------------
/res/layout/signin.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
12 |
13 |
19 |
20 |
28 |
29 |
30 |
31 |
37 |
38 |
46 |
47 |
48 |
54 |
55 |
63 |
64 |
70 |
71 |
79 |
80 |
85 |
86 |
--------------------------------------------------------------------------------
/res/layout/welcome.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
14 |
15 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/res/menu/main.xml:
--------------------------------------------------------------------------------
1 |
13 |
--------------------------------------------------------------------------------
/res/values-v11/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/res/values-v14/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
8 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
8 | 64dp
9 |
10 |
11 |
--------------------------------------------------------------------------------
/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 16dp
5 | 16dp
6 |
7 |
8 |
--------------------------------------------------------------------------------
/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | FriendLocation
5 | Hello world!
6 | Settings
7 |
8 |
9 |
--------------------------------------------------------------------------------
/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
14 |
15 |
16 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/src/com/example/friendlocation/Frnd_Direction.java:
--------------------------------------------------------------------------------
1 | package com.example.friendlocation;
2 |
3 | import java.io.InputStream;
4 | import java.util.ArrayList;
5 | import java.util.List;
6 |
7 | import javax.xml.parsers.DocumentBuilder;
8 | import javax.xml.parsers.DocumentBuilderFactory;
9 |
10 | import org.apache.http.HttpResponse;
11 | import org.apache.http.NameValuePair;
12 | import org.apache.http.client.HttpClient;
13 | import org.apache.http.client.methods.HttpPost;
14 | import org.apache.http.impl.client.DefaultHttpClient;
15 | import org.apache.http.message.BasicNameValuePair;
16 | import org.apache.http.protocol.BasicHttpContext;
17 | import org.apache.http.protocol.HttpContext;
18 | import org.json.JSONException;
19 | import org.json.JSONObject;
20 | import org.w3c.dom.Document;
21 | import org.w3c.dom.Node;
22 | import org.w3c.dom.NodeList;
23 |
24 | import com.google.android.gms.maps.CameraUpdateFactory;
25 | import com.google.android.gms.maps.GoogleMap;
26 | import com.google.android.gms.maps.MapFragment;
27 | import com.google.android.gms.maps.model.BitmapDescriptorFactory;
28 | import com.google.android.gms.maps.model.CameraPosition;
29 | import com.google.android.gms.maps.model.LatLng;
30 | import com.google.android.gms.maps.model.Marker;
31 | import com.google.android.gms.maps.model.MarkerOptions;
32 | import com.google.android.gms.maps.model.Polyline;
33 | import com.google.android.gms.maps.model.PolylineOptions;
34 |
35 | import android.annotation.TargetApi;
36 | import android.content.Context;
37 | import android.content.Intent;
38 | import android.graphics.Color;
39 | import android.location.Criteria;
40 | import android.location.Location;
41 | import android.location.LocationListener;
42 | import android.location.LocationManager;
43 | import android.os.AsyncTask;
44 | import android.os.Build;
45 | import android.os.Bundle;
46 | import android.os.Handler;
47 | import android.support.v7.app.ActionBarActivity;
48 | import android.util.DisplayMetrics;
49 | import android.view.Menu;
50 | import android.view.MenuItem;
51 | import android.widget.Toast;
52 |
53 | @TargetApi(Build.VERSION_CODES.HONEYCOMB)
54 | public class Frnd_Direction extends ActionBarActivity {
55 |
56 | GoogleMap map;
57 | String name,id,frnd_name,frnd_lat,frnd_longi,frnd_id;
58 | LatLng my_latlong,frnd_latlong;
59 |
60 | JSONParser jsonParser = new JSONParser();
61 | private static String url = "http://znsoftech.com/googlemap/new/location.php";
62 | private static String frnd_url = "http://znsoftech.com/googlemap/new/get_frnd_update_loc.php";
63 |
64 | PolylineOptions rectLine;
65 | ArrayList directionPoint;
66 | Document doc;
67 | MarkerOptions mo,frnd_mo;
68 |
69 | private int zoom = -1;
70 | private int animateSpeed = -1;
71 | private boolean isAnimated = false;
72 | private double animateDistance = -1;
73 | private double animateCamera = -1;
74 | private int step = -1;
75 | private Polyline animateLine = null;
76 | private double totalAnimateDistance = 0;
77 | private boolean cameraLock = false;
78 | private OnAnimateListener mAnimateListener = null;
79 | private boolean flatMarker = false;
80 | private boolean isCameraTilt = false;
81 | private boolean isCameraZoom = false;
82 | private ArrayList animatePositionList = null;
83 | private boolean drawMarker = false;
84 | private boolean drawLine = false;
85 |
86 |
87 | private LatLng animateMarkerPosition = null;
88 | private LatLng beginPosition = null;
89 | private LatLng endPosition = null;
90 | private Marker animateMarker = null;
91 |
92 | public final static int SPEED_VERY_FAST = 1;
93 | public final static int SPEED_FAST = 2;
94 | public final static int SPEED_NORMAL = 3;
95 | public final static int SPEED_SLOW = 4;
96 | public final static int SPEED_VERY_SLOW = 5;
97 |
98 | @TargetApi(Build.VERSION_CODES.HONEYCOMB)
99 | @Override
100 | protected void onCreate(Bundle savedInstanceState) {
101 | super.onCreate(savedInstanceState);
102 | setContentView(R.layout.welcome);
103 |
104 | Intent i=getIntent();
105 | name=i.getStringExtra("name");
106 | id=i.getStringExtra("id");
107 | frnd_lat=i.getStringExtra("frnd_lat");
108 | frnd_longi=i.getStringExtra("frnd_longi");
109 | frnd_name=i.getStringExtra("frnd_name");
110 | frnd_id=i.getStringExtra("frnd_id");
111 |
112 | rectLine = new PolylineOptions().width(3).color(Color.RED);
113 |
114 | LocationManager lm=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
115 | Criteria c=new Criteria();
116 | String provider=lm.getBestProvider(c, false);
117 | Location location=lm.getLastKnownLocation(provider);
118 |
119 | map=((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
120 |
121 | if(map==null && location==null)
122 | Toast.makeText(getApplicationContext(),"Sorry! unable to create Map.", Toast.LENGTH_SHORT).show();
123 | else
124 | {
125 | map.setMyLocationEnabled(true);
126 | map.getUiSettings().setZoomControlsEnabled(true);
127 | map.getUiSettings().setMyLocationButtonEnabled(true);
128 |
129 | my_latlong = new LatLng(location.getLatitude(), location.getLongitude());
130 | mo=new MarkerOptions().position(my_latlong).title(name+", you are here!").snippet(""+my_latlong);
131 |
132 | frnd_latlong = new LatLng(Double.parseDouble(frnd_lat), Double.parseDouble(frnd_longi));
133 | frnd_mo=new MarkerOptions().position(frnd_latlong).title(frnd_name+" is here!").snippet(""+frnd_latlong)
134 | .icon(BitmapDescriptorFactory.defaultMarker(
135 | BitmapDescriptorFactory.HUE_GREEN));
136 |
137 | map.clear();
138 | map.addMarker(mo);
139 | map.addMarker(frnd_mo);
140 |
141 | map.moveCamera(CameraUpdateFactory.newLatLng(frnd_latlong));
142 | map.animateCamera(CameraUpdateFactory.zoomTo(15));
143 |
144 | lm.requestLocationUpdates(provider, 1000, 10, new LocationListener() {
145 |
146 | @Override
147 | public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
148 | // TODO Auto-generated method stub
149 |
150 | }
151 |
152 | @Override
153 | public void onProviderEnabled(String arg0) {
154 | // TODO Auto-generated method stub
155 |
156 | }
157 |
158 | @Override
159 | public void onProviderDisabled(String arg0) {
160 | // TODO Auto-generated method stub
161 |
162 | }
163 |
164 | @Override
165 | public void onLocationChanged(Location location) {
166 | // TODO Auto-generated method stub
167 | Toast.makeText(getApplicationContext(), "Location changed!", Toast.LENGTH_SHORT).show();
168 | new Request_Update().execute(location);
169 | }
170 | });
171 |
172 | }
173 | }
174 |
175 | public class Request_Update extends AsyncTask
176 | {
177 | @Override
178 | protected void onPreExecute()
179 | {
180 | Toast.makeText(getApplicationContext(), "onPreExecute()!", Toast.LENGTH_SHORT).show();
181 | }
182 | @Override
183 | protected Location doInBackground(Location... location) {
184 | // TODO Auto-generated method stub
185 |
186 | //update my location on server...............
187 | List params = new ArrayList();
188 | params.add(new BasicNameValuePair("id", id));
189 | params.add(new BasicNameValuePair("lat", location[0].getLatitude()+""));
190 | params.add(new BasicNameValuePair("longi", location[0].getLongitude()+""));
191 | jsonParser.makeHttpRequest(url,"POST", params);
192 |
193 | //Get friend updated location..........
194 | List frnd_params = new ArrayList();
195 | frnd_params.add(new BasicNameValuePair("frnd_id", frnd_id));
196 | JSONObject frnd_json=jsonParser.makeHttpRequest(frnd_url,"POST", frnd_params);
197 |
198 | try {
199 | int success = frnd_json.getInt("success");
200 | if (success == 1)
201 | {
202 | frnd_lat=frnd_json.getString("lat");
203 | frnd_longi=frnd_json.getString("longi");
204 | }
205 | } catch (JSONException e) {
206 | }
207 | /////update path................................
208 |
209 | String url = "http://maps.googleapis.com/maps/api/directions/xml?"
210 | + "origin=" + location[0].getLatitude() + "," + location[0].getLongitude()
211 | + "&destination=" + frnd_lat + "," + frnd_longi
212 | + "&sensor=false&units=metric&mode=driving";
213 |
214 | try {
215 | HttpClient httpClient = new DefaultHttpClient();
216 | HttpContext localContext = new BasicHttpContext();
217 | HttpPost httpPost = new HttpPost(url);
218 | HttpResponse response = httpClient.execute(httpPost, localContext);
219 | InputStream in = response.getEntity().getContent();
220 | DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
221 | doc = builder.parse(in);
222 | } catch (Exception e) {
223 | }
224 |
225 | return location[0];
226 | }
227 |
228 | @Override
229 | protected void onPostExecute(Location location)
230 | {
231 | //.....................................
232 | my_latlong = new LatLng(location.getLatitude(), location.getLongitude());
233 | mo=new MarkerOptions().position(my_latlong).title(name+", you are here!").snippet(""+my_latlong);
234 |
235 | frnd_latlong = new LatLng(Double.parseDouble(frnd_lat), Double.parseDouble(frnd_longi));
236 | frnd_mo=new MarkerOptions().position(frnd_latlong).title(frnd_name+" is here!").snippet(""+frnd_latlong)
237 | .icon(BitmapDescriptorFactory.defaultMarker(
238 | BitmapDescriptorFactory.HUE_GREEN));
239 |
240 | map.clear();
241 | if(doc!=null)
242 | {
243 | directionPoint=getDirection(doc);
244 | for(int ii = 0 ; ii < directionPoint.size() ; ii++) {
245 | rectLine.add(directionPoint.get(ii));
246 | }
247 |
248 | map.addPolyline(rectLine);
249 | }
250 |
251 | map.addMarker(mo);
252 | map.addMarker(frnd_mo);
253 | }
254 |
255 | }
256 |
257 | public ArrayList getDirection(Document doc) {
258 | NodeList nl1, nl2, nl3;
259 | ArrayList listGeopoints = new ArrayList();
260 | nl1 = doc.getElementsByTagName("step");
261 | if (nl1.getLength() > 0) {
262 | for (int i = 0; i < nl1.getLength(); i++) {
263 | Node node1 = nl1.item(i);
264 | nl2 = node1.getChildNodes();
265 |
266 | Node locationNode = nl2.item(getNodeIndex(nl2, "start_location"));
267 | nl3 = locationNode.getChildNodes();
268 | Node latNode = nl3.item(getNodeIndex(nl3, "lat"));
269 | double lat = Double.parseDouble(latNode.getTextContent());
270 | Node lngNode = nl3.item(getNodeIndex(nl3, "lng"));
271 | double lng = Double.parseDouble(lngNode.getTextContent());
272 | listGeopoints.add(new LatLng(lat, lng));
273 |
274 | locationNode = nl2.item(getNodeIndex(nl2, "polyline"));
275 | nl3 = locationNode.getChildNodes();
276 | latNode = nl3.item(getNodeIndex(nl3, "points"));
277 | ArrayList arr = decodePoly(latNode.getTextContent());
278 | for(int j = 0 ; j < arr.size() ; j++) {
279 | listGeopoints.add(new LatLng(arr.get(j).latitude
280 | , arr.get(j).longitude));
281 | }
282 |
283 | locationNode = nl2.item(getNodeIndex(nl2, "end_location"));
284 | nl3 = locationNode.getChildNodes();
285 | latNode = nl3.item(getNodeIndex(nl3, "lat"));
286 | lat = Double.parseDouble(latNode.getTextContent());
287 | lngNode = nl3.item(getNodeIndex(nl3, "lng"));
288 | lng = Double.parseDouble(lngNode.getTextContent());
289 | listGeopoints.add(new LatLng(lat, lng));
290 | }
291 | }
292 |
293 | return listGeopoints;
294 | }
295 |
296 | private int getNodeIndex(NodeList nl, String nodename) {
297 | for(int i = 0 ; i < nl.getLength() ; i++) {
298 | if(nl.item(i).getNodeName().equals(nodename))
299 | return i;
300 | }
301 | return -1;
302 | }
303 |
304 | private ArrayList decodePoly(String encoded) {
305 | ArrayList poly = new ArrayList();
306 | int index = 0, len = encoded.length();
307 | int lat = 0, lng = 0;
308 | while (index < len) {
309 | int b, shift = 0, result = 0;
310 | do {
311 | b = encoded.charAt(index++) - 63;
312 | result |= (b & 0x1f) << shift;
313 | shift += 5;
314 | } while (b >= 0x20);
315 | int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
316 | lat += dlat;
317 | shift = 0;
318 | result = 0;
319 | do {
320 | b = encoded.charAt(index++) - 63;
321 | result |= (b & 0x1f) << shift;
322 | shift += 5;
323 | } while (b >= 0x20);
324 | int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
325 | lng += dlng;
326 |
327 | LatLng position = new LatLng((double)lat / 1E5, (double)lng / 1E5);
328 | poly.add(position);
329 | }
330 | return poly;
331 | }
332 |
333 | @Override
334 | public boolean onCreateOptionsMenu(Menu menu)
335 | {
336 | menu.add(0,0,0, "Distance");
337 | menu.add(0,1,1, "Direction");
338 | return super.onCreateOptionsMenu(menu);
339 | }
340 |
341 | public boolean onOptionsItemSelected(MenuItem item)
342 | {
343 | switch(item.getItemId())
344 | {
345 | case 0:
346 | Location l1=new Location("One");
347 | l1.setLatitude(my_latlong.latitude);
348 | l1.setLongitude(my_latlong.longitude);
349 |
350 | Location l2=new Location("Two");
351 | l2.setLatitude(frnd_latlong.latitude);
352 | l2.setLongitude(frnd_latlong.longitude);
353 |
354 | float distance=l1.distanceTo(l2);
355 | String dist=distance+" M";
356 |
357 | if(distance>1000.0f)
358 | {
359 | distance=distance/1000.0f;
360 | dist=distance+" KM";
361 | }
362 |
363 | Toast.makeText(getApplicationContext(), dist, Toast.LENGTH_SHORT).show();
364 |
365 | break;
366 | case 1:
367 | animateDirection( directionPoint, SPEED_VERY_FAST
368 | , true, false, true, true, new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.car)), false, true, new PolylineOptions().width(3).color(Color.GREEN));
369 | break;
370 | }
371 | return true;
372 | }
373 |
374 | public void animateDirection(ArrayList direction, int speed
375 | , boolean cameraLock1, boolean isCameraTilt1, boolean isCameraZoom1
376 | , boolean drawMarker1, MarkerOptions mo, boolean flatMarker1
377 | , boolean drawLine1, PolylineOptions po) {
378 | if(direction.size() > 1) {
379 |
380 | isAnimated = true;
381 | animatePositionList = direction;
382 | animateSpeed = speed;
383 | drawMarker = drawMarker1;
384 | drawLine = drawLine1;
385 | flatMarker = flatMarker1;
386 | isCameraTilt = isCameraTilt1;
387 | isCameraZoom = isCameraZoom1;
388 | step = 0;
389 | cameraLock = cameraLock1;
390 |
391 | setCameraUpdateSpeed(speed);
392 |
393 | beginPosition = direction.get(step);
394 | endPosition = direction.get(step + 1);
395 | animateMarkerPosition = beginPosition;
396 |
397 | if(mAnimateListener != null)
398 | mAnimateListener.onProgress(step, direction.size());
399 |
400 | if(cameraLock) {
401 | float bearing = getBearing(beginPosition, endPosition);
402 | CameraPosition.Builder cameraBuilder = new CameraPosition.Builder()
403 | .target(animateMarkerPosition).bearing(bearing);
404 |
405 | if(isCameraTilt)
406 | cameraBuilder.tilt(90);
407 | else
408 | cameraBuilder.tilt(map.getCameraPosition().tilt);
409 |
410 | if(isCameraZoom)
411 | cameraBuilder.zoom(zoom);
412 | else
413 | cameraBuilder.zoom(map.getCameraPosition().zoom);
414 |
415 | CameraPosition cameraPosition = cameraBuilder.build();
416 | map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
417 | }
418 |
419 | if(drawMarker) {
420 | if(mo != null)
421 | animateMarker = map.addMarker(mo.position(beginPosition));
422 | else
423 | animateMarker = map.addMarker(new MarkerOptions().position(beginPosition));
424 |
425 | if(flatMarker) {
426 | animateMarker.setFlat(true);
427 |
428 | float rotation = getBearing(animateMarkerPosition, endPosition) + 180;
429 | animateMarker.setRotation(rotation);
430 | }
431 | }
432 |
433 |
434 | if(drawLine) {
435 | if(po != null)
436 | animateLine = map.addPolyline(po.add(beginPosition)
437 | .add(beginPosition).add(endPosition)
438 | .width(dpToPx((int)po.getWidth())));
439 | else
440 | animateLine = map.addPolyline(new PolylineOptions()
441 | .width(dpToPx(5)));
442 | }
443 |
444 | new Handler().postDelayed(r, speed);
445 | if(mAnimateListener != null)
446 | mAnimateListener.onStart();
447 | }
448 | }
449 |
450 | private LatLng getNewPosition(LatLng begin, LatLng end) {
451 | double lat = Math.abs(begin.latitude - end.latitude);
452 | double lng = Math.abs(begin.longitude - end.longitude);
453 |
454 | double dis = Math.sqrt(Math.pow(lat, 2) + Math.pow(lng, 2));
455 | if(dis >= animateDistance) {
456 | double angle = -1;
457 |
458 | if(begin.latitude <= end.latitude && begin.longitude <= end.longitude)
459 | angle = Math.toDegrees(Math.atan(lng / lat));
460 | else if(begin.latitude > end.latitude && begin.longitude <= end.longitude)
461 | angle = (90 - Math.toDegrees(Math.atan(lng / lat))) + 90;
462 | else if(begin.latitude > end.latitude && begin.longitude > end.longitude)
463 | angle = Math.toDegrees(Math.atan(lng / lat)) + 180;
464 | else if(begin.latitude <= end.latitude && begin.longitude > end.longitude)
465 | angle = (90 - Math.toDegrees(Math.atan(lng / lat))) + 270;
466 |
467 | double x = Math.cos(Math.toRadians(angle)) * animateDistance;
468 | double y = Math.sin(Math.toRadians(angle)) * animateDistance;
469 | totalAnimateDistance += animateDistance;
470 | double finalLat = begin.latitude + x;
471 | double finalLng = begin.longitude + y;
472 |
473 | return new LatLng(finalLat, finalLng);
474 | } else {
475 | return end;
476 | }
477 | }
478 |
479 | private Runnable r = new Runnable() {
480 | public void run() {
481 |
482 | animateMarkerPosition = getNewPosition(animateMarkerPosition, endPosition);
483 |
484 | if(drawMarker)
485 | animateMarker.setPosition(animateMarkerPosition);
486 |
487 |
488 | if(drawLine) {
489 | List points = animateLine.getPoints();
490 | points.add(animateMarkerPosition);
491 | animateLine.setPoints(points);
492 | }
493 |
494 | if((animateMarkerPosition.latitude == endPosition.latitude
495 | && animateMarkerPosition.longitude == endPosition.longitude)) {
496 | if(step == animatePositionList.size() - 2) {
497 | isAnimated = false;
498 | totalAnimateDistance = 0;
499 | if(mAnimateListener != null)
500 | mAnimateListener.onFinish();
501 | } else {
502 | step++;
503 | beginPosition = animatePositionList.get(step);
504 | endPosition = animatePositionList.get(step + 1);
505 | animateMarkerPosition = beginPosition;
506 |
507 | if(flatMarker && step + 3 < animatePositionList.size() - 1) {
508 | float rotation = getBearing(animateMarkerPosition, animatePositionList.get(step + 3)) + 180;
509 | animateMarker.setRotation(rotation);
510 | }
511 |
512 | if(mAnimateListener != null)
513 | mAnimateListener.onProgress(step, animatePositionList.size());
514 | }
515 | }
516 |
517 | if(cameraLock && (totalAnimateDistance > animateCamera || !isAnimated)) {
518 | totalAnimateDistance = 0;
519 | float bearing = getBearing(beginPosition, endPosition);
520 | CameraPosition.Builder cameraBuilder = new CameraPosition.Builder()
521 | .target(animateMarkerPosition).bearing(bearing);
522 |
523 | if(isCameraTilt)
524 | cameraBuilder.tilt(90);
525 | else
526 | cameraBuilder.tilt(map.getCameraPosition().tilt);
527 |
528 | if(isCameraZoom)
529 | cameraBuilder.zoom(zoom);
530 | else
531 | cameraBuilder.zoom(map.getCameraPosition().zoom);
532 |
533 | CameraPosition cameraPosition = cameraBuilder.build();
534 | map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
535 |
536 | }
537 |
538 | if(isAnimated) {
539 | new Handler().postDelayed(r, animateSpeed);
540 | }
541 | }
542 | };
543 |
544 | public interface OnAnimateListener {
545 | public void onFinish();
546 | public void onStart();
547 | public void onProgress(int progress, int total);
548 | }
549 |
550 | private int dpToPx(int dp) {
551 | DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
552 | int px = Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
553 | return px;
554 | }
555 |
556 | private float getBearing(LatLng begin, LatLng end) {
557 | double lat = Math.abs(begin.latitude - end.latitude);
558 | double lng = Math.abs(begin.longitude - end.longitude);
559 | if(begin.latitude < end.latitude && begin.longitude < end.longitude)
560 | return (float)(Math.toDegrees(Math.atan(lng / lat)));
561 | else if(begin.latitude >= end.latitude && begin.longitude < end.longitude)
562 | return (float)((90 - Math.toDegrees(Math.atan(lng / lat))) + 90);
563 | else if(begin.latitude >= end.latitude && begin.longitude >= end.longitude)
564 | return (float)(Math.toDegrees(Math.atan(lng / lat)) + 180);
565 | else if(begin.latitude < end.latitude && begin.longitude >= end.longitude)
566 | return (float)((90 - Math.toDegrees(Math.atan(lng / lat))) + 270);
567 | return -1;
568 | }
569 |
570 | public void setCameraUpdateSpeed(int speed) {
571 | if(speed == SPEED_VERY_SLOW) {
572 | animateDistance = 0.000005;
573 | animateSpeed = 20;
574 | animateCamera = 0.0004;
575 | zoom = 19;
576 | } else if(speed == SPEED_SLOW) {
577 | animateDistance = 0.00001;
578 | animateSpeed = 20;
579 | animateCamera = 0.0008;
580 | zoom = 18;
581 | } else if(speed == SPEED_NORMAL) {
582 | animateDistance = 0.00005;
583 | animateSpeed = 20;
584 | animateCamera = 0.002;
585 | zoom = 16;
586 | } else if(speed == SPEED_FAST) {
587 | animateDistance = 0.0001;
588 | animateSpeed = 20;
589 | animateCamera = 0.004;
590 | zoom = 15;
591 | } else if(speed == SPEED_VERY_FAST) {
592 | /*animateDistance = 0.0005;
593 | animateSpeed = 20;
594 | animateCamera = 0.004;
595 | zoom = 13;
596 | */
597 | animateDistance = 0.001;
598 | animateSpeed = 100;
599 | animateCamera = 0.04;
600 | zoom = 15;
601 | } else {
602 | animateDistance = 0.00005;
603 | animateSpeed = 20;
604 | animateCamera = 0.002;
605 | zoom = 16;
606 | }
607 | }
608 |
609 | }
--------------------------------------------------------------------------------
/src/com/example/friendlocation/JSONParser.java:
--------------------------------------------------------------------------------
1 | package com.example.friendlocation;
2 |
3 | import java.io.BufferedReader;
4 | import java.io.IOException;
5 | import java.io.InputStream;
6 | import java.io.InputStreamReader;
7 | import java.io.UnsupportedEncodingException;
8 | import java.util.List;
9 |
10 | import org.apache.http.HttpEntity;
11 | import org.apache.http.HttpResponse;
12 | import org.apache.http.NameValuePair;
13 | import org.apache.http.client.ClientProtocolException;
14 | import org.apache.http.client.entity.UrlEncodedFormEntity;
15 | import org.apache.http.client.methods.HttpGet;
16 | import org.apache.http.client.methods.HttpPost;
17 | import org.apache.http.client.utils.URLEncodedUtils;
18 | import org.apache.http.impl.client.DefaultHttpClient;
19 | import org.json.JSONException;
20 | import org.json.JSONObject;
21 |
22 | import android.util.Log;
23 |
24 | public class JSONParser {
25 |
26 | static InputStream is = null;
27 | static JSONObject jObj = null;
28 | static String json = "";
29 |
30 | public JSONParser()
31 | {
32 |
33 | }
34 |
35 | public JSONObject makeHttpRequest(String url, String method,List params) {
36 |
37 | try {
38 |
39 | if(method == "POST")
40 | {
41 | DefaultHttpClient httpClient = new DefaultHttpClient();
42 | HttpPost httpPost = new HttpPost(url);
43 | httpPost.setEntity(new UrlEncodedFormEntity(params));
44 |
45 | HttpResponse httpResponse = httpClient.execute(httpPost);
46 | HttpEntity httpEntity = httpResponse.getEntity();
47 | is = httpEntity.getContent();
48 |
49 | }
50 | else if(method == "GET")
51 | {
52 | DefaultHttpClient httpClient = new DefaultHttpClient();
53 | String paramString = URLEncodedUtils.format(params, "utf-8");
54 | url += "?" + paramString;
55 | HttpGet httpGet = new HttpGet(url);
56 |
57 | HttpResponse httpResponse = httpClient.execute(httpGet);
58 | HttpEntity httpEntity = httpResponse.getEntity();
59 | is = httpEntity.getContent();
60 | }
61 |
62 |
63 | } catch (UnsupportedEncodingException e) {
64 | e.printStackTrace();
65 | } catch (ClientProtocolException e) {
66 | e.printStackTrace();
67 | } catch (IOException e) {
68 | e.printStackTrace();
69 | }
70 |
71 | try {
72 | BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
73 | StringBuilder sb = new StringBuilder();
74 | String line = null;
75 | while ((line = reader.readLine()) != null)
76 | {
77 | sb.append(line + "\n");
78 | }
79 | is.close();
80 | json = sb.toString();
81 | }
82 | catch (Exception e) {
83 | Log.e("Buffer Error", "Error converting result " + e.toString());
84 | }
85 | try {
86 | jObj = new JSONObject(json);
87 | } catch (JSONException e) {
88 | Log.e("JSON Parser", "Error parsing data " + e.toString());
89 | }
90 | //return json string
91 | return jObj;
92 | }
93 | }
94 |
--------------------------------------------------------------------------------
/src/com/example/friendlocation/LocationListener.java:
--------------------------------------------------------------------------------
1 | package com.example.friendlocation;
2 |
3 | import android.content.BroadcastReceiver;
4 | import android.content.Context;
5 | import android.content.Intent;
6 | import android.net.ConnectivityManager;
7 | import android.net.NetworkInfo;
8 | import android.widget.Toast;
9 |
10 | public class LocationListener extends BroadcastReceiver {
11 |
12 | @Override
13 | public void onReceive(Context context, Intent intent) {
14 | // TODO Auto-generated method stub
15 | if(intent==null || intent.getAction()==null)
16 | return;
17 |
18 | if(isOnline(context))
19 | {
20 | intent=new Intent(context, UpdateLocationService.class);
21 | intent.putExtra("Started", "YES");
22 | context.startService(intent);
23 | Toast.makeText(context, "online & Started", Toast.LENGTH_SHORT).show();
24 | }
25 |
26 | }
27 |
28 | private boolean isOnline(Context mContext) {
29 | ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
30 | NetworkInfo netInfo = cm.getActiveNetworkInfo();
31 | if (netInfo != null && netInfo.isConnectedOrConnecting())
32 | {
33 | return true;
34 | }
35 | return false;
36 | }
37 |
38 | }
39 |
--------------------------------------------------------------------------------
/src/com/example/friendlocation/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.example.friendlocation;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | import org.apache.http.NameValuePair;
7 | import org.apache.http.message.BasicNameValuePair;
8 | import org.json.JSONException;
9 | import org.json.JSONObject;
10 |
11 | import android.support.v7.app.ActionBarActivity;
12 | import android.app.Activity;
13 | import android.app.ProgressDialog;
14 | import android.content.Context;
15 | import android.content.Intent;
16 | import android.content.SharedPreferences;
17 | import android.net.ConnectivityManager;
18 | import android.net.NetworkInfo;
19 | import android.os.AsyncTask;
20 | import android.os.Bundle;
21 | import android.os.StrictMode;
22 | import android.util.Log;
23 | import android.view.Menu;
24 | import android.view.View;
25 | import android.widget.Button;
26 | import android.widget.EditText;
27 | import android.widget.Toast;
28 |
29 | public class MainActivity extends ActionBarActivity {
30 |
31 | Button login,signin;
32 | private EditText mobile_number,password;
33 | private ProgressDialog pDialog;
34 | int flag=0;
35 | SharedPreferences sp;
36 | JSONParser jsonParser = new JSONParser();
37 | private static String url = "http://znsoftech.com/googlemap/new/login.php";
38 | private static final String TAG_SUCCESS = "success";
39 |
40 |
41 | @Override
42 | protected void onCreate(Bundle savedInstanceState) {
43 | StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
44 | .detectDiskReads().detectDiskWrites().detectNetwork()
45 | .penaltyLog().build());
46 | super.onCreate(savedInstanceState);
47 | setContentView(R.layout.activity_main);
48 |
49 | sp=getSharedPreferences("location", Activity.MODE_PRIVATE);
50 |
51 | //Go To Signin.java
52 | signin=(Button)findViewById(R.id.signin);
53 | signin.setOnClickListener(new View.OnClickListener()
54 | {
55 | @Override
56 | public void onClick(View view) {
57 | Intent i = new Intent(getApplicationContext(), Signin.class);
58 | startActivity(i);
59 |
60 | }
61 | });
62 | // Close Signin.java
63 |
64 | //Get all data and log in
65 | login=(Button)findViewById(R.id.login);
66 | mobile_number=(EditText)findViewById(R.id.mobile_number);
67 | password=(EditText)findViewById(R.id.password);
68 |
69 | login.setOnClickListener(new View.OnClickListener()
70 | {
71 | @Override
72 | public void onClick(View view) {
73 |
74 | //Check all fields
75 | if(mobile_number.length()<10)
76 | {
77 | Toast.makeText(MainActivity.this,"Enter correct mobile number!", Toast.LENGTH_LONG).show();
78 | return;
79 | }
80 | if(password.length()<4)
81 | {
82 | Toast.makeText(MainActivity.this,"Enter correct password!", Toast.LENGTH_LONG).show();
83 | return;
84 | }
85 | //check connectivity
86 | if(!isOnline(MainActivity.this))
87 | {
88 | Toast.makeText(MainActivity.this,"No network connection!", Toast.LENGTH_LONG).show();
89 | return;
90 | }
91 |
92 | //from login.java
93 | new loginAccess().execute();
94 | }
95 |
96 | //code to check online details
97 | private boolean isOnline(Context mContext) {
98 | ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
99 | NetworkInfo netInfo = cm.getActiveNetworkInfo();
100 | if (netInfo != null && netInfo.isConnectedOrConnecting())
101 | {
102 | return true;
103 | }
104 | return false;
105 | }
106 | //Close code that check online details
107 | });
108 | //Close log in
109 | }
110 |
111 |
112 | class loginAccess extends AsyncTask {
113 |
114 | protected void onPreExecute() {
115 | super.onPreExecute();
116 | pDialog = new ProgressDialog(MainActivity.this);
117 | pDialog.setMessage("Login...");
118 | pDialog.setIndeterminate(false);
119 | //pDialog.setCancelable(true);
120 | pDialog.show();
121 | }
122 | @Override
123 | protected String doInBackground(String... arg0) {
124 | List params = new ArrayList();
125 | String number=mobile_number.getText().toString();
126 | String pwd=password.getText().toString();
127 | params.add(new BasicNameValuePair("mobile_number", number));
128 | params.add(new BasicNameValuePair("password", pwd));
129 | JSONObject json = jsonParser.makeHttpRequest(url,"POST", params);
130 | Log.d("Create Response", json.toString());
131 |
132 | try {
133 | int success = json.getInt(TAG_SUCCESS);
134 | if (success == 1)
135 | {
136 | flag=0;
137 | int id=json.getInt("id");
138 | String name=json.getString("name");
139 |
140 | Intent i = new Intent(getApplicationContext(),Main_Welcome.class);
141 | i.putExtra("name",name);
142 | i.putExtra("mobile_number",number);
143 | i.putExtra("id", id+"");
144 | sp.edit().putString("id", id+"").commit();
145 | startActivity(i);
146 | finish();
147 | }
148 | else
149 | {
150 | // failed to login
151 | flag=1;
152 | }
153 | } catch (JSONException e) {
154 | e.printStackTrace();
155 | }
156 | return null;
157 | }
158 | protected void onPostExecute(String file_url) {
159 | pDialog.dismiss();
160 | if(flag==1)
161 | Toast.makeText(MainActivity.this,"Enter Correct informations!", Toast.LENGTH_LONG).show();
162 |
163 | }
164 |
165 | }
166 | @Override
167 | public boolean onCreateOptionsMenu(Menu menu) {
168 | // Inflate the menu; this adds items to the action bar if it is present.
169 | getMenuInflater().inflate(R.menu.main, menu);
170 | return true;
171 | }
172 |
173 |
174 | }
175 |
176 |
--------------------------------------------------------------------------------
/src/com/example/friendlocation/Main_Welcome.java:
--------------------------------------------------------------------------------
1 | package com.example.friendlocation;
2 |
3 | import java.io.IOException;
4 | import java.util.ArrayList;
5 | import java.util.List;
6 | import java.util.Locale;
7 |
8 | import org.apache.http.NameValuePair;
9 | import org.apache.http.message.BasicNameValuePair;
10 | import org.json.JSONException;
11 | import org.json.JSONObject;
12 |
13 | import android.app.AlertDialog;
14 | import android.app.AlertDialog.Builder;
15 | import android.app.Dialog;
16 | import android.content.Context;
17 | import android.content.DialogInterface;
18 | import android.content.Intent;
19 | import android.location.Address;
20 | import android.location.Criteria;
21 | import android.location.Geocoder;
22 | import android.location.Location;
23 | import android.location.LocationListener;
24 | import android.location.LocationManager;
25 | import android.os.Bundle;
26 | import android.support.v7.app.ActionBarActivity;
27 | import android.view.View;
28 | import android.widget.TextView;
29 | import android.widget.Toast;
30 |
31 | public class Main_Welcome extends ActionBarActivity{
32 |
33 | TextView tv;
34 | LocationManager lm;
35 | String provider, mylocation,name,mobile_number,id;
36 | Criteria c;
37 | Geocoder geo;
38 | List addresses;
39 | Intent i;
40 | JSONParser jsonParser = new JSONParser();
41 | private static String url = "http://znsoftech.com/googlemap/new/location.php";
42 |
43 | @Override
44 | protected void onCreate(Bundle savedInstanceState) {
45 | super.onCreate(savedInstanceState);
46 | setContentView(R.layout.main_welcome);
47 | tv=(TextView)findViewById(R.id.textView1);
48 | Intent i=getIntent();
49 | name=i.getStringExtra("name");
50 | id=i.getStringExtra("id");
51 | mobile_number=i.getStringExtra("mobile_number");
52 |
53 | tv.setText("Welcome "+name);
54 |
55 | geo=new Geocoder(getApplicationContext(), Locale.getDefault());
56 |
57 | lm=(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
58 | c=new Criteria();
59 | provider=lm.getBestProvider(c, false);
60 |
61 | lm.requestSingleUpdate(provider, new LocationListener() {
62 |
63 | @Override
64 | public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
65 | // TODO Auto-generated method stub
66 |
67 | }
68 |
69 | @Override
70 | public void onProviderEnabled(String arg0) {
71 | // TODO Auto-generated method stub
72 |
73 | }
74 |
75 | @Override
76 | public void onProviderDisabled(String arg0) {
77 | // TODO Auto-generated method stub
78 |
79 | }
80 |
81 | @Override
82 | public void onLocationChanged(Location location) {
83 | // TODO Auto-generated method stub
84 | List params = new ArrayList();
85 | params.add(new BasicNameValuePair("id", id));
86 | params.add(new BasicNameValuePair("lat", location.getLatitude()+""));
87 | params.add(new BasicNameValuePair("longi", location.getLongitude()+""));
88 | JSONObject json=jsonParser.makeHttpRequest(url,"POST", params);
89 |
90 | try {
91 | int success = json.getInt("success");
92 | if (success == 1)
93 | Toast.makeText(getApplicationContext(), "Updated in welcome page!", Toast.LENGTH_SHORT).show();
94 | else
95 | Toast.makeText(getApplicationContext(), "Error in welcome page!", Toast.LENGTH_SHORT).show();
96 |
97 | } catch (JSONException e) {
98 | // TODO Auto-generated catch block
99 | //e.printStackTrace();
100 | }
101 | }
102 | }, null);
103 | /*
104 | lm.requestLocationUpdates(provider, 0, 0, new LocationListener() {
105 |
106 | @Override
107 | public void onStatusChanged(String provider, int status, Bundle extras) {
108 | // TODO Auto-generated method stub
109 |
110 | }
111 |
112 | @Override
113 | public void onProviderEnabled(String provider) {
114 | // TODO Auto-generated method stub
115 |
116 | }
117 |
118 | @Override
119 | public void onProviderDisabled(String provider) {
120 | // TODO Auto-generated method stub
121 |
122 | }
123 |
124 | @Override
125 | public void onLocationChanged(Location location) {
126 | // TODO Auto-generated method stub
127 | List params = new ArrayList();
128 | params.add(new BasicNameValuePair("id", id));
129 | params.add(new BasicNameValuePair("lat", location.getLatitude()+""));
130 | params.add(new BasicNameValuePair("longi", location.getLongitude()+""));
131 | JSONObject json=jsonParser.makeHttpRequest(url,"POST", params);
132 |
133 | try {
134 | int success = json.getInt("success");
135 | if (success == 1)
136 | Toast.makeText(getApplicationContext(), "Updated in welcome page!", Toast.LENGTH_SHORT).show();
137 | else
138 | Toast.makeText(getApplicationContext(), "Error in welcome page!", Toast.LENGTH_SHORT).show();
139 |
140 | } catch (JSONException e) {
141 | // TODO Auto-generated catch block
142 | //e.printStackTrace();
143 | }
144 |
145 | }
146 | });
147 | */
148 |
149 | }
150 |
151 | public void action(View v)
152 | {
153 | switch(v.getId())
154 | {
155 | case R.id.button1:
156 | i=new Intent(this, Welcome.class);
157 | i.putExtra("name",name);
158 | i.putExtra("mobile_number",mobile_number);
159 | i.putExtra("id", id);
160 | startActivity(i);
161 | break;
162 | case R.id.button2:
163 | Location l=lm.getLastKnownLocation(provider);
164 | if(l!=null)
165 | {
166 | mylocation="Lattitude: "+l.getLatitude()+" Longitude: "+l.getLongitude();
167 |
168 | if(Geocoder.isPresent())
169 | {
170 | try {
171 | Toast.makeText(getApplicationContext(), "In Try!", Toast.LENGTH_SHORT).show();
172 | addresses = geo.getFromLocation(l.getLatitude(), l.getLongitude(), 1);
173 | if (addresses != null && addresses.size() > 0)
174 | {
175 | Address address = addresses.get(0);
176 | String addressText = String.format(
177 | "%s, %s, %s",
178 | // If there's a street address, add it
179 | address.getMaxAddressLineIndex() > 0 ?
180 | address.getAddressLine(0) : "",
181 | // Locality is usually a city
182 | address.getLocality(),
183 | // The country of the address
184 | address.getCountryName());
185 | mylocation="Lattitude: "+l.getLatitude()+" Longitude: "+l.getLongitude()+"\nAddress: "+addressText;
186 | }
187 | } catch (IOException e) {
188 | // TODO Auto-generated catch block
189 | //e.printStackTrace();
190 | Toast.makeText(getApplicationContext(), "In Exception!", Toast.LENGTH_SHORT).show();
191 | }
192 | }
193 | }
194 | else
195 | mylocation="No provider enabled!";
196 | onCreateDialog(10);
197 | break;
198 |
199 | case R.id.button3:
200 | i=new Intent(this, Search_Frnd.class);
201 | i.putExtra("name",name);
202 | i.putExtra("mobile_number",mobile_number);
203 | i.putExtra("id", id);
204 | startActivity(i);
205 | break;
206 | }
207 | }
208 |
209 | @SuppressWarnings("deprecation")
210 | protected Dialog onCreateDialog(int id)
211 | {
212 | switch(id)
213 | {
214 | case 10:
215 | Builder builder=new AlertDialog.Builder(this);
216 | builder.setMessage(mylocation);
217 | builder.setTitle("My Location");
218 | //button
219 | builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
220 |
221 | @Override
222 | public void onClick(DialogInterface dialog, int which)
223 | {
224 |
225 |
226 | }
227 | });
228 |
229 | AlertDialog dialog=builder.create();
230 | dialog.show();
231 |
232 | }
233 | return super.onCreateDialog(id);
234 | }
235 |
236 | }
237 |
--------------------------------------------------------------------------------
/src/com/example/friendlocation/MoveToFriend.java:
--------------------------------------------------------------------------------
1 | package com.example.friendlocation;
2 |
3 | import java.io.InputStream;
4 | import java.util.ArrayList;
5 |
6 | import javax.xml.parsers.DocumentBuilder;
7 | import javax.xml.parsers.DocumentBuilderFactory;
8 |
9 | import org.apache.http.HttpResponse;
10 | import org.apache.http.client.HttpClient;
11 | import org.apache.http.client.methods.HttpPost;
12 | import org.apache.http.impl.client.DefaultHttpClient;
13 | import org.apache.http.protocol.BasicHttpContext;
14 | import org.apache.http.protocol.HttpContext;
15 | import org.w3c.dom.Document;
16 | import org.w3c.dom.Node;
17 | import org.w3c.dom.NodeList;
18 |
19 | import com.google.android.gms.maps.CameraUpdateFactory;
20 | import com.google.android.gms.maps.GoogleMap;
21 | import com.google.android.gms.maps.MapFragment;
22 | import com.google.android.gms.maps.GoogleMap.OnMyLocationChangeListener;
23 | import com.google.android.gms.maps.model.BitmapDescriptorFactory;
24 | import com.google.android.gms.maps.model.CameraPosition;
25 | import com.google.android.gms.maps.model.LatLng;
26 | import com.google.android.gms.maps.model.Marker;
27 | import com.google.android.gms.maps.model.MarkerOptions;
28 | import com.google.android.gms.maps.model.Polyline;
29 | import com.google.android.gms.maps.model.PolylineOptions;
30 |
31 | import android.annotation.SuppressLint;
32 | import android.annotation.TargetApi;
33 | import android.content.Intent;
34 | import android.graphics.Color;
35 | import android.hardware.SensorManager;
36 | import android.location.Location;
37 | import android.os.AsyncTask;
38 | import android.os.Build;
39 | import android.os.Bundle;
40 | import android.speech.tts.TextToSpeech;
41 | import android.support.v7.app.ActionBarActivity;
42 | import android.view.Menu;
43 | import android.view.MenuItem;
44 | import android.view.View;
45 | import android.widget.TextView;
46 | import android.widget.Toast;
47 |
48 | @TargetApi(Build.VERSION_CODES.HONEYCOMB)
49 | public class MoveToFriend extends ActionBarActivity // implements TextToSpeech.OnInitListener
50 | {
51 |
52 | GoogleMap map;
53 | String name,id,frnd_name,frnd_lat,frnd_longi,frnd_id;
54 | LatLng my_latlong,frnd_latlong;
55 | TextView tv;
56 | //private TextToSpeech tts;
57 | ArrayList directionPoint;
58 | int size_of_latlong,latlong_index=0;
59 | ArrayList polylines;
60 | String direction="walking";
61 | Document doc;
62 | SensorManager sensor_manager;
63 | //private int result=0;
64 | MarkerOptions mo,frnd_mo;
65 | Marker my_marker,frnd_marker;
66 | boolean first_time_flag=false, update_flag=false;
67 |
68 | @SuppressLint("NewApi") @Override
69 | protected void onCreate(Bundle savedInstanceState) {
70 | super.onCreate(savedInstanceState);
71 | setContentView(R.layout.welcome);
72 |
73 | Intent i=getIntent();
74 | name=i.getStringExtra("name");
75 | id=i.getStringExtra("id");
76 | frnd_lat=i.getStringExtra("frnd_lat");
77 | frnd_longi=i.getStringExtra("frnd_longi");
78 | frnd_name=i.getStringExtra("frnd_name");
79 | frnd_id=i.getStringExtra("frnd_id");
80 |
81 | polylines=new ArrayList();
82 |
83 | //tts = new TextToSpeech(this, this);
84 |
85 | tv=(TextView)findViewById(R.id.textView1);
86 | tv.setVisibility(View.VISIBLE);
87 | tv.setText("Wait...");
88 | frnd_latlong=new LatLng(Double.parseDouble(frnd_lat), Double.parseDouble(frnd_longi));
89 |
90 | map=((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
91 |
92 | if(map==null)
93 | Toast.makeText(getApplicationContext(),"Sorry! unable to create Map.", Toast.LENGTH_SHORT).show();
94 | else
95 | {
96 | map.setMyLocationEnabled(true);
97 | map.getUiSettings().setZoomControlsEnabled(true);
98 | map.getUiSettings().setMyLocationButtonEnabled(true);
99 | //map.getUiSettings().setCompassEnabled(true);
100 |
101 | map.setOnMyLocationChangeListener(new OnMyLocationChangeListener() {
102 |
103 | @Override
104 | public void onMyLocationChange(Location location) {
105 | // TODO Auto-generated method stub
106 | if(update_flag==true)
107 | {
108 | my_latlong = new LatLng(location.getLatitude(), location.getLongitude());
109 |
110 | if(latlong_index==size_of_latlong)
111 | {
112 | //speakOut("you are very close to your friend.");
113 | return;
114 | }
115 |
116 | float bearing = getBearing(my_latlong, directionPoint.get(latlong_index));
117 |
118 | mo=new MarkerOptions().position(my_latlong).title(name+", you are here!").snippet(""+my_latlong)
119 | .icon(BitmapDescriptorFactory.fromResource(R.drawable.car)).flat(true).rotation(bearing+180);
120 | my_marker.remove();
121 | my_marker=map.addMarker(mo);
122 | //map.moveCamera(CameraUpdateFactory.newLatLng(my_latlong));
123 |
124 | CameraPosition.Builder cameraBuilder = new CameraPosition.Builder()
125 | .target(my_latlong).bearing(bearing);
126 |
127 | cameraBuilder.tilt(map.getCameraPosition().tilt);
128 | cameraBuilder.zoom(map.getCameraPosition().zoom);
129 | CameraPosition cameraPosition = cameraBuilder.build();
130 |
131 | map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
132 |
133 | if(getDistanceinDouble(directionPoint.get(latlong_index),my_latlong)<15.0f)
134 | {
135 | polylines.get(latlong_index).remove();
136 | latlong_index++;
137 | }
138 |
139 | if(getDistanceinDouble(directionPoint.get(latlong_index),my_latlong)>200.0f)
140 | {
141 | first_time_flag=false;
142 | update_flag=false;
143 | return;
144 | }
145 |
146 | if(latlong_index= end.latitude && begin.longitude < end.longitude)
184 | return (float)((90 - Math.toDegrees(Math.atan(lng / lat))) + 90);
185 | else if(begin.latitude >= end.latitude && begin.longitude >= end.longitude)
186 | return (float)(Math.toDegrees(Math.atan(lng / lat)) + 180);
187 | else if(begin.latitude < end.latitude && begin.longitude >= end.longitude)
188 | return (float)((90 - Math.toDegrees(Math.atan(lng / lat))) + 270);
189 | return -1;
190 | }
191 |
192 | private String getDistance(LatLng my_latlong,LatLng frnd_latlong){
193 | Location l1=new Location("One");
194 | l1.setLatitude(my_latlong.latitude);
195 | l1.setLongitude(my_latlong.longitude);
196 |
197 | Location l2=new Location("Two");
198 | l2.setLatitude(frnd_latlong.latitude);
199 | l2.setLongitude(frnd_latlong.longitude);
200 |
201 | float distance=l1.distanceTo(l2);
202 | String dist=distance+" M";
203 |
204 | if(distance>1000.0f)
205 | {
206 | distance=distance/1000.0f;
207 | dist=distance+" KM";
208 | }
209 | return dist;
210 | }
211 |
212 | private float getDistanceinDouble(LatLng my_latlong,LatLng frnd_latlong){
213 | Location l1=new Location("One");
214 | l1.setLatitude(my_latlong.latitude);
215 | l1.setLongitude(my_latlong.longitude);
216 |
217 | Location l2=new Location("Two");
218 | l2.setLatitude(frnd_latlong.latitude);
219 | l2.setLongitude(frnd_latlong.longitude);
220 |
221 | float distance=l1.distanceTo(l2);
222 |
223 | return distance;
224 | }
225 |
226 | private String getDirection(LatLng my_latlong,LatLng frnd_latlong) {
227 | // TODO Auto-generated method stub
228 | double my_lat=my_latlong.latitude;
229 | double my_long=my_latlong.longitude;
230 |
231 | double frnd_lat=frnd_latlong.latitude;
232 | double frnd_long=frnd_latlong.longitude;
233 |
234 | double radians=getAtan2((frnd_long-my_long),(frnd_lat-my_lat));
235 | double compassReading = radians * (180 / Math.PI);
236 |
237 | String[] coordNames = {"North", "North-East", "East", "South-East", "South", "South-West", "West", "North-West", "North"};
238 | int coordIndex = (int) Math.round(compassReading / 45);
239 |
240 | if (coordIndex < 0) {
241 | coordIndex = coordIndex + 8;
242 | };
243 |
244 | return coordNames[coordIndex]; // returns the coordinate value
245 | }
246 |
247 | private double getAtan2(double longi,double lat) {
248 | return Math.atan2(longi, lat);
249 | }
250 | });
251 | }
252 | }
253 | /*
254 | @Override
255 | public void onDestroy() {
256 | // Don't forget to shutdown!
257 | if (tts != null) {
258 | tts.stop();
259 | tts.shutdown();
260 | }
261 | super.onDestroy();
262 | }
263 |
264 | @Override
265 | public void onInit(int status) {
266 | // TODO Auto-generated method stub
267 | if (status == TextToSpeech.SUCCESS) {
268 | result = tts.setLanguage(Locale.US);
269 | if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
270 | Toast.makeText(this, "Missing TTS data", Toast.LENGTH_LONG).show();
271 | }
272 | }
273 | }
274 |
275 | private void speakOut(String text) {
276 | if(result!=tts.setLanguage(Locale.US))
277 | {
278 | // Toast.makeText(getApplicationContext(), "Enter right Words...... ", Toast.LENGTH_LONG).show();
279 | }else{
280 | tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
281 | }
282 | }
283 | */
284 | @Override
285 | public boolean onCreateOptionsMenu(Menu menu)
286 | {
287 | menu.add(0,0,0, "Normal Mode");
288 | menu.add(0,1,1, "Hybrid Mode");
289 | menu.add(0,2,2, "Terrain Mode");
290 | menu.add(0,3,3, "Satellite Mode");
291 | menu.add(0,4,4, "With Traffic");
292 | menu.add(0,5,5, "Without traffic");
293 | menu.add(0,6,6, "Walking Direction");
294 | menu.add(0,7,7, "Driving Direction");
295 |
296 | return super.onCreateOptionsMenu(menu);
297 | }
298 |
299 | public boolean onOptionsItemSelected(MenuItem item)
300 | {
301 | switch(item.getItemId())
302 | {
303 | case 0:
304 | map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
305 | break;
306 | case 1:
307 | map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
308 | break;
309 | case 2:
310 | map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
311 | break;
312 | case 3:
313 | map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
314 | break;
315 | case 4:
316 | map.setTrafficEnabled(true);
317 | break;
318 | case 5:
319 | map.setTrafficEnabled(false);
320 | break;
321 | case 6:
322 | direction="walking";
323 | first_time_flag=false;
324 | update_flag=false;
325 | break;
326 | case 7:
327 | direction="driving";
328 | first_time_flag=false;
329 | update_flag=false;
330 | break;
331 | }
332 | return true;
333 | }
334 |
335 | public class Request_Update extends AsyncTask
336 | {
337 | @Override
338 | protected void onPreExecute()
339 | {
340 | //Toast.makeText(getApplicationContext(), "onPreExecute()!", Toast.LENGTH_SHORT).show();
341 | }
342 | @Override
343 | protected Location doInBackground(Location... location) {
344 | // TODO Auto-generated method stub
345 |
346 | String url = "http://maps.googleapis.com/maps/api/directions/xml?"
347 | + "origin=" + location[0].getLatitude() + "," + location[0].getLongitude()
348 | + "&destination=" + frnd_lat + "," + frnd_longi
349 | + "&sensor=false&units=metric&mode="+direction;
350 |
351 | try {
352 | HttpClient httpClient = new DefaultHttpClient();
353 | HttpContext localContext = new BasicHttpContext();
354 | HttpPost httpPost = new HttpPost(url);
355 | HttpResponse response = httpClient.execute(httpPost, localContext);
356 | InputStream in = response.getEntity().getContent();
357 | DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
358 | doc = builder.parse(in);
359 | } catch (Exception e) {
360 | }
361 |
362 | return location[0];
363 | }
364 |
365 | @Override
366 | protected void onPostExecute(Location location)
367 | {
368 | if(doc!=null)
369 | {
370 | directionPoint=getDirection(doc);
371 | int ii = 0;
372 | size_of_latlong=directionPoint.size();
373 | for( ; ii getDirection(Document doc) {
400 | NodeList nl1, nl2, nl3;
401 | ArrayList listGeopoints = new ArrayList();
402 | nl1 = doc.getElementsByTagName("step");
403 | if (nl1.getLength() > 0) {
404 | for (int i = 0; i < nl1.getLength(); i++) {
405 | Node node1 = nl1.item(i);
406 | nl2 = node1.getChildNodes();
407 |
408 | Node locationNode = nl2.item(getNodeIndex(nl2, "start_location"));
409 | nl3 = locationNode.getChildNodes();
410 | Node latNode = nl3.item(getNodeIndex(nl3, "lat"));
411 | double lat = Double.parseDouble(latNode.getTextContent());
412 | Node lngNode = nl3.item(getNodeIndex(nl3, "lng"));
413 | double lng = Double.parseDouble(lngNode.getTextContent());
414 | listGeopoints.add(new LatLng(lat, lng));
415 |
416 | locationNode = nl2.item(getNodeIndex(nl2, "polyline"));
417 | nl3 = locationNode.getChildNodes();
418 | latNode = nl3.item(getNodeIndex(nl3, "points"));
419 | ArrayList arr = decodePoly(latNode.getTextContent());
420 | for(int j = 0 ; j < arr.size() ; j++) {
421 | listGeopoints.add(new LatLng(arr.get(j).latitude, arr.get(j).longitude));
422 | }
423 |
424 | locationNode = nl2.item(getNodeIndex(nl2, "end_location"));
425 | nl3 = locationNode.getChildNodes();
426 | latNode = nl3.item(getNodeIndex(nl3, "lat"));
427 | lat = Double.parseDouble(latNode.getTextContent());
428 | lngNode = nl3.item(getNodeIndex(nl3, "lng"));
429 | lng = Double.parseDouble(lngNode.getTextContent());
430 | listGeopoints.add(new LatLng(lat, lng));
431 | }
432 | }
433 |
434 | return listGeopoints;
435 | }
436 |
437 | private int getNodeIndex(NodeList nl, String nodename) {
438 | for(int i = 0 ; i < nl.getLength() ; i++) {
439 | if(nl.item(i).getNodeName().equals(nodename))
440 | return i;
441 | }
442 | return -1;
443 | }
444 |
445 | private ArrayList decodePoly(String encoded) {
446 | ArrayList poly = new ArrayList();
447 | int index = 0, len = encoded.length();
448 | int lat = 0, lng = 0;
449 | while (index < len) {
450 | int b, shift = 0, result = 0;
451 | do {
452 | b = encoded.charAt(index++) - 63;
453 | result |= (b & 0x1f) << shift;
454 | shift += 5;
455 | } while (b >= 0x20);
456 | int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
457 | lat += dlat;
458 | shift = 0;
459 | result = 0;
460 | do {
461 | b = encoded.charAt(index++) - 63;
462 | result |= (b & 0x1f) << shift;
463 | shift += 5;
464 | } while (b >= 0x20);
465 | int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
466 | lng += dlng;
467 |
468 | LatLng position = new LatLng((double)lat / 1E5, (double)lng / 1E5);
469 | poly.add(position);
470 | }
471 | return poly;
472 | }
473 | }
--------------------------------------------------------------------------------
/src/com/example/friendlocation/Search_Frnd.java:
--------------------------------------------------------------------------------
1 | package com.example.friendlocation;
2 |
3 | import java.io.IOException;
4 | import java.util.ArrayList;
5 | import java.util.HashMap;
6 | import java.util.List;
7 | import java.util.Locale;
8 |
9 | import org.apache.http.NameValuePair;
10 | import org.apache.http.message.BasicNameValuePair;
11 | import org.json.JSONArray;
12 | import org.json.JSONException;
13 | import org.json.JSONObject;
14 |
15 | import android.app.AlertDialog;
16 | import android.app.Dialog;
17 | import android.app.ProgressDialog;
18 | import android.app.AlertDialog.Builder;
19 | import android.content.Context;
20 | import android.content.DialogInterface;
21 | import android.content.Intent;
22 | import android.location.Address;
23 | import android.location.Geocoder;
24 | import android.net.ConnectivityManager;
25 | import android.net.NetworkInfo;
26 | import android.os.AsyncTask;
27 | import android.os.Bundle;
28 | import android.os.StrictMode;
29 | import android.support.v7.app.ActionBarActivity;
30 | import android.util.Log;
31 | import android.view.View;
32 | import android.widget.AdapterView;
33 | import android.widget.AdapterView.OnItemClickListener;
34 | import android.widget.Button;
35 | import android.widget.EditText;
36 | import android.widget.ListAdapter;
37 | import android.widget.ListView;
38 | import android.widget.SimpleAdapter;
39 | import android.widget.TextView;
40 | import android.widget.Toast;
41 |
42 | public class Search_Frnd extends ActionBarActivity implements OnItemClickListener {
43 |
44 | Button bt;
45 | EditText tv;
46 | ListView lv;
47 | private ProgressDialog pDialog;
48 | String frnd_name,frnd_location, frnd_lat, frnd_longi,my_name,my_id,frnd_id;
49 | int flag=0;
50 | Geocoder geo;
51 | List addresses;
52 | JSONParser jsonParser = new JSONParser();
53 | private static String url = "http://znsoftech.com/googlemap/new/search_frnd.php";
54 | private static final String TAG_SUCCESS = "success";
55 | private static final String TAG_ID = "id";
56 | private static final String TAG_NAME = "name";
57 | private static final String TAG_LAT = "lat";
58 | private static final String TAG_LONGI = "longi";
59 | private static final String TAG_DATE_TIME = "date_time";
60 | private static final String TAG_FRND_LIST = "frnd_list";
61 | ArrayList> all_list;
62 |
63 |
64 | @Override
65 | protected void onCreate(Bundle savedInstanceState) {
66 | StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
67 | .detectDiskReads().detectDiskWrites().detectNetwork()
68 | .penaltyLog().build());
69 | super.onCreate(savedInstanceState);
70 | setContentView(R.layout.search_frnd);
71 |
72 | Intent i=getIntent();
73 | my_name=i.getStringExtra("name");
74 | my_id= i.getStringExtra("id");
75 |
76 | tv=(EditText)findViewById(R.id.editText1);
77 | bt=(Button)findViewById(R.id.button1);
78 | lv=(ListView)findViewById(R.id.listView1);
79 | all_list=new ArrayList>();
80 | geo=new Geocoder(getApplicationContext(), Locale.getDefault());
81 |
82 | bt.setOnClickListener(new View.OnClickListener()
83 | {
84 | @Override
85 | public void onClick(View view) {
86 |
87 | if(tv.length()<2)
88 | {
89 | Toast.makeText(Search_Frnd.this,"Enter correct name!", Toast.LENGTH_LONG).show();
90 | return;
91 | }
92 | //check connectivity
93 | if(!isOnline(Search_Frnd.this))
94 | {
95 | Toast.makeText(Search_Frnd.this,"No network connection!", Toast.LENGTH_LONG).show();
96 | return;
97 | }
98 | all_list.clear();
99 | lv.setAdapter(null);
100 | new search_frnd_query().execute();
101 | }
102 |
103 | //code to check online details
104 | private boolean isOnline(Context mContext) {
105 | ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
106 | NetworkInfo netInfo = cm.getActiveNetworkInfo();
107 | if (netInfo != null && netInfo.isConnectedOrConnecting())
108 | {
109 | return true;
110 | }
111 | return false;
112 | }
113 | //Close code that check online details
114 | });
115 |
116 | }
117 |
118 | class search_frnd_query extends AsyncTask {
119 |
120 | protected void onPreExecute() {
121 | super.onPreExecute();
122 | pDialog = new ProgressDialog(Search_Frnd.this);
123 | pDialog.setMessage("Searching Friends...");
124 | pDialog.setIndeterminate(false);
125 | //pDialog.setCancelable(true);
126 | pDialog.show();
127 | }
128 | @Override
129 | protected String doInBackground(String... arg0) {
130 | List params = new ArrayList();
131 | String frnd_name=tv.getText().toString();
132 | params.add(new BasicNameValuePair("frnd_name", frnd_name));
133 |
134 | JSONObject json = jsonParser.makeHttpRequest(url,"POST", params);
135 | Log.d("Create Response", json.toString());
136 |
137 | try {
138 | int success = json.getInt(TAG_SUCCESS);
139 | if (success == 1)
140 | {
141 | flag=0;
142 | JSONArray frnd_list=json.getJSONArray(TAG_FRND_LIST);
143 | for(int i=0; i map=new HashMap();
152 |
153 | map.put(TAG_ID, id);
154 | map.put(TAG_NAME, name);
155 | map.put(TAG_LAT, lat);
156 | map.put(TAG_LONGI, longi);
157 | map.put(TAG_DATE_TIME, date_time);
158 |
159 | all_list.add(map);
160 |
161 | }
162 | }
163 | else
164 | {
165 | // failed to get friend list
166 | flag=1;
167 | }
168 | } catch (JSONException e) {
169 | e.printStackTrace();
170 | }
171 | return null;
172 | }
173 | protected void onPostExecute(String file_url) {
174 | pDialog.dismiss();
175 | if(flag==1)
176 | Toast.makeText(Search_Frnd.this,"Enter Correct informations!", Toast.LENGTH_LONG).show();
177 | else
178 | {
179 | runOnUiThread(new Runnable() {
180 |
181 | @Override
182 | public void run() {
183 | // TODO Auto-generated method stub
184 | ListAdapter adapter=new SimpleAdapter(Search_Frnd.this, all_list, R.layout.name_list, new String[]{TAG_NAME, TAG_ID, TAG_LAT, TAG_LONGI, TAG_DATE_TIME}, new int[]{R.id.name, R.id.id, R.id.lat, R.id.longi, R.id.date_time});
185 | lv.setAdapter(adapter);
186 | lv.setOnItemClickListener(Search_Frnd.this);
187 | }
188 | });
189 | }
190 | }
191 |
192 | }
193 |
194 | @Override
195 | public void onItemClick(AdapterView> parent, View view, int position, long id) {
196 | // TODO Auto-generated method stub
197 | String name=((TextView)view.findViewById(R.id.name)).getText().toString();
198 | String lat=((TextView)view.findViewById(R.id.lat)).getText().toString();
199 | String date_time=((TextView)view.findViewById(R.id.date_time)).getText().toString();
200 | String longi=((TextView)view.findViewById(R.id.longi)).getText().toString();
201 | String frndid=((TextView)view.findViewById(R.id.id)).getText().toString();
202 | frnd_lat=lat;
203 | frnd_id=frndid;
204 | frnd_longi=longi;
205 | frnd_location="Name: "+name+"\nDate Time: "+date_time;
206 | frnd_name=name;
207 | if(lat!=null && lat.length()>4)
208 | {
209 |
210 | if(Geocoder.isPresent())
211 | {
212 | try {
213 | addresses = geo.getFromLocation(Double.parseDouble(lat), Double.parseDouble(longi), 1);
214 | if (addresses != null && addresses.size() > 0)
215 | {
216 | Address address = addresses.get(0);
217 | String addressText = String.format(
218 | "%s, %s, %s",
219 | // If there's a street address, add it
220 | address.getMaxAddressLineIndex() > 0 ?
221 | address.getAddressLine(0) : "",
222 | // Locality is usually a city
223 | address.getLocality(),
224 | // The country of the address
225 | address.getCountryName());
226 | frnd_location="Name: "+name+"\nLattitude: "+lat+"\nLongitude: "+longi+"\nAddress: "+addressText+"\nLast Login: "+date_time;
227 | }
228 | } catch (IOException e) {
229 | // TODO Auto-generated catch block
230 | //e.printStackTrace();
231 | }
232 | }
233 |
234 | }
235 |
236 | onCreateDialog(10);
237 |
238 | }
239 |
240 | @SuppressWarnings("deprecation")
241 | protected Dialog onCreateDialog(int id)
242 | {
243 | switch(id)
244 | {
245 | case 10:
246 | Builder builder=new AlertDialog.Builder(this);
247 | builder.setMessage(frnd_location);
248 | builder.setTitle("About "+frnd_name);
249 | //button
250 | builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
251 |
252 | @Override
253 | public void onClick(DialogInterface dialog, int which)
254 | {
255 |
256 |
257 | }
258 | });
259 |
260 | builder.setNegativeButton("Map Direction", new DialogInterface.OnClickListener() {
261 |
262 | @Override
263 | public void onClick(DialogInterface dialog, int which) {
264 | // TODO Auto-generated method stub
265 | if(frnd_lat!=null && frnd_lat.length()>4)
266 | {
267 | Intent i=new Intent(Search_Frnd.this, MoveToFriend.class);
268 |
269 | i.putExtra("frnd_lat", frnd_lat);
270 | i.putExtra("frnd_longi",frnd_longi);
271 | i.putExtra("frnd_name", frnd_name);
272 | i.putExtra("frnd_id", frnd_id);
273 | i.putExtra("name", my_name);
274 | i.putExtra("id", my_id);
275 | startActivity(i);
276 | }
277 | else
278 | Toast.makeText(getApplicationContext(), "Location is not available to show on Map!", Toast.LENGTH_SHORT).show();
279 |
280 | }
281 | });
282 |
283 | AlertDialog dialog=builder.create();
284 | dialog.show();
285 |
286 | }
287 | return super.onCreateDialog(id);
288 | }
289 |
290 |
291 | }
292 |
--------------------------------------------------------------------------------
/src/com/example/friendlocation/Signin.java:
--------------------------------------------------------------------------------
1 | package com.example.friendlocation;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | import org.apache.http.NameValuePair;
7 | import org.apache.http.message.BasicNameValuePair;
8 | import org.json.JSONException;
9 | import org.json.JSONObject;
10 |
11 | import android.net.ConnectivityManager;
12 | import android.net.NetworkInfo;
13 | import android.os.AsyncTask;
14 | import android.os.Bundle;
15 | import android.os.StrictMode;
16 | import android.app.Activity;
17 | import android.app.ProgressDialog;
18 | import android.content.Context;
19 | import android.content.Intent;
20 | import android.content.SharedPreferences;
21 | import android.support.v7.app.ActionBarActivity;
22 | import android.util.Log;
23 | import android.view.View;
24 | import android.widget.Button;
25 | import android.widget.EditText;
26 | import android.widget.Toast;
27 |
28 | public class Signin extends ActionBarActivity {
29 | Button login,signin;
30 | private EditText mobile_number,password,email_id,name;
31 | private ProgressDialog pDialog;
32 | int flag=0;
33 | SharedPreferences sp;
34 | JSONParser jsonParser = new JSONParser();
35 | private static String url = "http://znsoftech.com/googlemap/new/register.php";
36 | private static final String TAG_SUCCESS = "success";
37 | @Override
38 | protected void onCreate(Bundle savedInstanceState) {
39 | StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
40 | .detectDiskReads().detectDiskWrites().detectNetwork()
41 | .penaltyLog().build());
42 | super.onCreate(savedInstanceState);
43 | setContentView(R.layout.signin);
44 | sp=getSharedPreferences("location", Activity.MODE_PRIVATE);
45 | //Go To Login.java
46 | login=(Button)findViewById(R.id.login);
47 | login.setOnClickListener(new View.OnClickListener()
48 | {
49 | @Override
50 | public void onClick(View view) {
51 | Intent i = new Intent(getApplicationContext(), MainActivity.class);
52 | startActivity(i);
53 |
54 | }
55 | });
56 | // Close Login.java
57 |
58 | //Get all data and log in
59 | signin=(Button)findViewById(R.id.signin);
60 | mobile_number=(EditText)findViewById(R.id.mobile_number);
61 | password=(EditText)findViewById(R.id.password);
62 | email_id=(EditText)findViewById(R.id.email_id);
63 | name=(EditText)findViewById(R.id.name);
64 |
65 | signin.setOnClickListener(new View.OnClickListener()
66 | {
67 | @Override
68 | public void onClick(View view) {
69 |
70 | //Check all fields
71 | if(name.length()<3)
72 | {
73 | Toast.makeText(Signin.this,"Enter correct name!", Toast.LENGTH_LONG).show();
74 | return;
75 | }
76 | if(mobile_number.length()<10)
77 | {
78 | Toast.makeText(Signin.this,"Enter correct mobile number!", Toast.LENGTH_LONG).show();
79 | return;
80 | }
81 | if(password.length()<4)
82 | {
83 | Toast.makeText(Signin.this,"Enter minimum 4 letters in password!", Toast.LENGTH_LONG).show();
84 | return;
85 | }
86 | if(email_id.length()<11)
87 | {
88 | Toast.makeText(Signin.this,"Enter correct email id!", Toast.LENGTH_LONG).show();
89 | return;
90 | }
91 | //check connectivity
92 | if(!isOnline(Signin.this))
93 | {
94 | Toast.makeText(Signin.this,"No network connection!", Toast.LENGTH_LONG).show();
95 | return;
96 | }
97 |
98 | //from login.java
99 | new loginAccess().execute();
100 | }
101 |
102 | //code to check online details
103 | private boolean isOnline(Context mContext) {
104 | ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
105 | NetworkInfo netInfo = cm.getActiveNetworkInfo();
106 | if (netInfo != null && netInfo.isConnectedOrConnecting())
107 | {
108 | return true;
109 | }
110 | return false;
111 | }
112 | //Close code that check online details
113 | });
114 | //Close log in
115 | }
116 |
117 |
118 | class loginAccess extends AsyncTask {
119 |
120 | protected void onPreExecute() {
121 | super.onPreExecute();
122 | pDialog = new ProgressDialog(Signin.this);
123 | pDialog.setMessage("Sign in...");
124 | pDialog.setIndeterminate(false);
125 | //pDialog.setCancelable(true);
126 | pDialog.show();
127 | }
128 | @Override
129 | protected String doInBackground(String... arg0) {
130 | List params = new ArrayList();
131 | String number=mobile_number.getText().toString();
132 | String pwd=password.getText().toString();
133 | String id=email_id.getText().toString();
134 | String nam=name.getText().toString();
135 |
136 | params.add(new BasicNameValuePair("name", nam));
137 | params.add(new BasicNameValuePair("mobile_number", number));
138 | params.add(new BasicNameValuePair("password", pwd));
139 | params.add(new BasicNameValuePair("email_id", id));
140 |
141 | JSONObject json = jsonParser.makeHttpRequest(url,"POST", params);
142 |
143 | Log.d("Create Response", json.toString());
144 |
145 | try {
146 | int success = json.getInt(TAG_SUCCESS);
147 | if (success == 1)
148 | {
149 | flag=0;
150 | int id2=json.getInt("id");
151 | Intent i = new Intent(getApplicationContext(),Main_Welcome.class);
152 | i.putExtra("name",nam);
153 | i.putExtra("mobile_number",number);
154 | i.putExtra("id", id2+"");
155 | sp.edit().putString("id", id+"").commit();
156 | startActivity(i);
157 | finish();
158 | }
159 | else
160 | {
161 | // failed to Sign in
162 | flag=1;
163 | }
164 | } catch (JSONException e) {
165 | e.printStackTrace();
166 | }
167 | return null;
168 | }
169 | protected void onPostExecute(String file_url) {
170 | pDialog.dismiss();
171 | if(flag==1)
172 | Toast.makeText(Signin.this,"Please Enter Correct informations!", Toast.LENGTH_LONG).show();
173 |
174 | }
175 |
176 | }
177 |
178 | }
179 |
--------------------------------------------------------------------------------
/src/com/example/friendlocation/UpdateLocationService.java:
--------------------------------------------------------------------------------
1 | package com.example.friendlocation;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 | import org.apache.http.NameValuePair;
6 | import org.apache.http.message.BasicNameValuePair;
7 | import org.json.JSONException;
8 | import org.json.JSONObject;
9 | import android.app.Activity;
10 | import android.app.Service;
11 | import android.content.Context;
12 | import android.content.Intent;
13 | import android.content.SharedPreferences;
14 | import android.location.Criteria;
15 | import android.location.Location;
16 | import android.location.LocationListener;
17 | import android.location.LocationManager;
18 | import android.os.Bundle;
19 | import android.os.IBinder;
20 | import android.widget.Toast;
21 |
22 | public class UpdateLocationService extends Service {
23 |
24 | String url = "http://znsoftech.com/googlemap/new/location.php";
25 | JSONParser jsonParser = new JSONParser();
26 | String id;
27 |
28 | @Override
29 | public void onCreate() {
30 | super.onCreate();
31 | Toast.makeText(getApplicationContext(), "In onCreate()!", Toast.LENGTH_SHORT).show();
32 | }
33 |
34 | @SuppressWarnings("deprecation")
35 | @Override
36 | public void onStart(Intent intent, int startId){
37 | super.onStart(intent, startId);
38 | Toast.makeText(getApplicationContext(), "In onStart()!", Toast.LENGTH_SHORT).show();
39 | getresult();
40 | }
41 |
42 | private void getresult() {
43 | // TODO Auto-generated method stub
44 | SharedPreferences sp=getSharedPreferences("location", Activity.MODE_PRIVATE);
45 | id=sp.getString("id", null);
46 | if(id!=null)
47 | {
48 | Toast.makeText(getApplicationContext(), "In id!"+id, Toast.LENGTH_SHORT).show();
49 | LocationManager lm=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
50 | Criteria c=new Criteria();
51 | String provider=lm.getBestProvider(c, false);
52 | lm.requestSingleUpdate(provider, new LocationListener() {
53 |
54 | @Override
55 | public void onStatusChanged(String provider, int status, Bundle extras) {
56 | // TODO Auto-generated method stub
57 |
58 | }
59 |
60 | @Override
61 | public void onProviderEnabled(String provider) {
62 | // TODO Auto-generated method stub
63 |
64 | }
65 |
66 | @Override
67 | public void onProviderDisabled(String provider) {
68 | // TODO Auto-generated method stub
69 |
70 | }
71 |
72 | @Override
73 | public void onLocationChanged(Location location) {
74 | // TODO Auto-generated method stub
75 | List params = new ArrayList();
76 | params.add(new BasicNameValuePair("id", id));
77 | params.add(new BasicNameValuePair("lat", location.getLatitude()+""));
78 | params.add(new BasicNameValuePair("longi", location.getLongitude()+""));
79 | JSONObject json=jsonParser.makeHttpRequest(url,"POST", params);
80 | Toast.makeText(getApplicationContext(), "In onLocationChanged()!", Toast.LENGTH_SHORT).show();
81 | try {
82 | int success = json.getInt("success");
83 | if (success == 1)
84 | {
85 | Toast.makeText(getApplicationContext(), "Updated in Service!", Toast.LENGTH_SHORT).show();
86 | stopSelf();
87 | }
88 | else
89 | Toast.makeText(getApplicationContext(), "Not updated in Service!", Toast.LENGTH_SHORT).show();
90 | } catch (JSONException e) {
91 | // TODO Auto-generated catch block
92 | //e.printStackTrace();
93 | Toast.makeText(getApplicationContext(), "Error in JsonException Service!", Toast.LENGTH_SHORT).show();
94 | stopSelf();
95 | }
96 | }
97 | },null);
98 | }
99 | }
100 |
101 | @Override
102 | public int onStartCommand(Intent intent, int flags, int startId) {
103 | super.onStartCommand(intent, flags, startId);
104 | if(intent==null || intent.getAction()==null)
105 | return Service.START_NOT_STICKY;
106 | Toast.makeText(getApplicationContext(), "In onStartCommand()!", Toast.LENGTH_SHORT).show();
107 | getresult();
108 | return Service.START_NOT_STICKY;
109 | }
110 |
111 | @Override
112 | public IBinder onBind(Intent intent) {
113 | // TODO Auto-generated method stub
114 | return null;
115 | }
116 |
117 | @Override
118 | public void onDestroy() {
119 | super.onDestroy();
120 | Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
121 | }
122 |
123 | }
124 |
--------------------------------------------------------------------------------
/src/com/example/friendlocation/Welcome.java:
--------------------------------------------------------------------------------
1 | package com.example.friendlocation;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | import org.apache.http.NameValuePair;
7 | import org.apache.http.message.BasicNameValuePair;
8 | import org.json.JSONException;
9 | import org.json.JSONObject;
10 |
11 | import com.google.android.gms.maps.CameraUpdateFactory;
12 | import com.google.android.gms.maps.GoogleMap;
13 | import com.google.android.gms.maps.GoogleMap.OnMyLocationChangeListener;
14 | import com.google.android.gms.maps.model.LatLng;
15 | import com.google.android.gms.maps.model.MarkerOptions;
16 | import com.google.android.gms.maps.MapFragment;
17 |
18 | import android.annotation.TargetApi;
19 | import android.content.Intent;
20 | import android.location.Location;
21 | import android.os.Build;
22 | import android.os.Bundle;
23 | import android.support.v7.app.ActionBarActivity;
24 | import android.widget.Toast;
25 |
26 | @TargetApi(Build.VERSION_CODES.HONEYCOMB)
27 | public class Welcome extends ActionBarActivity {
28 |
29 | GoogleMap map;
30 | boolean flag=false;
31 | String name,mobile_number,id;
32 | JSONParser jsonParser = new JSONParser();
33 | private static String url = "http://znsoftech.com/googlemap/new/location.php";
34 |
35 | @TargetApi(Build.VERSION_CODES.HONEYCOMB)
36 | @Override
37 | protected void onCreate(Bundle savedInstanceState) {
38 | super.onCreate(savedInstanceState);
39 | setContentView(R.layout.welcome);
40 |
41 | Intent i=getIntent();
42 | name=i.getStringExtra("name");
43 | id=i.getStringExtra("id");
44 | mobile_number=i.getStringExtra("mobile_number");
45 |
46 | map=((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
47 | if(map==null)
48 | Toast.makeText(getApplicationContext(),"Sorry! unable to create Map.", Toast.LENGTH_SHORT).show();
49 | else
50 | {
51 | map.setMyLocationEnabled(true);
52 | map.getUiSettings().setZoomControlsEnabled(true);
53 | map.getUiSettings().setMyLocationButtonEnabled(true);
54 | //map.getUiSettings().setCompassEnabled(true);
55 |
56 | map.setOnMyLocationChangeListener(new OnMyLocationChangeListener() {
57 |
58 | @Override
59 | public void onMyLocationChange(Location location) {
60 | // TODO Auto-generated method stub
61 | LatLng loc = new LatLng(location.getLatitude(), location.getLongitude());
62 | MarkerOptions mo=new MarkerOptions().position(loc).title(name+", you are here!").snippet(""+loc);
63 | map.clear();
64 | map.addMarker(mo);
65 |
66 | if(flag==false)
67 | {
68 | map.moveCamera(CameraUpdateFactory.newLatLng(loc));
69 | map.animateCamera(CameraUpdateFactory.zoomTo(15));
70 | flag=true;
71 | }
72 |
73 | List params = new ArrayList();
74 | params.add(new BasicNameValuePair("id", id));
75 | params.add(new BasicNameValuePair("lat", location.getLatitude()+""));
76 | params.add(new BasicNameValuePair("longi", location.getLongitude()+""));
77 | JSONObject json=jsonParser.makeHttpRequest(url,"POST", params);
78 |
79 | try {
80 | int success = json.getInt("success");
81 | if (success == 1)
82 | Toast.makeText(getApplicationContext(), "Updated!!!", Toast.LENGTH_SHORT).show();
83 | else
84 | Toast.makeText(getApplicationContext(), "Error!!!", Toast.LENGTH_SHORT).show();
85 |
86 | } catch (JSONException e) {
87 | // TODO Auto-generated catch block
88 | //e.printStackTrace();
89 | }
90 | }
91 | });
92 | }
93 | }
94 |
95 | }
96 |
--------------------------------------------------------------------------------