In my activity, I've added a google map. I'm retrieving multiple Location Name, Latitude and Longitude from mySql database to activity. Now, I want to display all these locations in a single map. For this, I've wrote following code :
package com.googlemaps;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MainActivity extends Activity
{
private GoogleMap googleMap;
String Latitude,Longitude,Location;
MapView mapView;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
googleMap.setMyLocationEnabled(true);
RetriveLatLong();
MarkerOptions marker=new MarkerOptions().position(new LatLng(Double.parseDouble(Latitude),
Double.parseDouble(Longitude))).title(Location);
googleMap.addMarker(marker);
marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
googleMap.getUiSettings().setAllGesturesEnabled(true);
}
/**
* function to load map. If map is not created it will create it for you
* */
@SuppressLint("NewApi")
private void initilizeMap()
{
if (googleMap == null)
{
googleMap=((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT).show();
}
}
}
@Override
protected void onResume()
{
super.onResume();
initilizeMap();
}
private void RetriveLatLong()
{
// TODO Auto-generated method stub
String data;
List<String> r = new ArrayList<String>();
final List<String> r1 = new ArrayList<String>();
List<String> r2 = new ArrayList<String>();
List<Map<String, String>> data1 = new ArrayList<Map<String, String>>();
try
{
DefaultHttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http://demo.tugain.com/tapme/RetriveMap.php?q=cat");
HttpResponse response = client.execute(request);
HttpEntity entity=response.getEntity();
data=EntityUtils.toString(entity);
Log.e("STRING", data);
try
{
JSONArray json=new JSONArray(data);
for(int i=0;i<json.length(); i++)
{
JSONObject obj=json.getJSONObject(i);
Location=obj.getString("Location");
Latitude=obj.getString("Latitude");
Longitude=obj.getString("Longitude");
Log.e("STRING", Location);
Log.e("STRING",Latitude);
Log.e("STRING",Longitude);
r.add(Location);
r1.add(Latitude);
r2.add(Longitude);
Map<String, String> datum = new HashMap<String, String>();
datum.put("Loc",Location);
datum.put("Lat", Latitude);
datum.put("Long", Longitude);
data1.add(datum);
}
}
catch (JSONException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ClientProtocolException e) {
Log.d("HTTPCLIENT", e.getLocalizedMessage());
} catch (IOException e) {
Log.d("HTTPCLIENT", e.getLocalizedMessage());
}
}
}
But I am not getting even map. The app stopped with "Unfortunately Stopped".In Logcat, it displays error at googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); What is wrong with my code..?? And is this code will display all locations that will retrieved from mysql database..?? Please help me..!!!
0 comments:
Post a Comment