You are currently browsing the tag archive for the ‘Google Maps’ tag.

Preface:

One of readers asked me to write about getting the names of the location that I touch on the map if it is possible to get names from coordinates. That query triggered me to write this article. The answer is yes we can get location names if we have coordinates, the process is called Geocoding.

Geocoding is the process in which we translate latitude and longitude sets to text based address or string location names. Like if we have the coordinates 33695043,73050000 and we want to know which location is it. We will use geocoding and will find out that its Islamabad, the capital of Pakistan. Same is the case with Reverse Geocoding, if we have the name Islamabad, we can Reverse Geocode it to fine the coordinates. To continue with this article I have used the code from my last tutorial i.e, https://mirnauman.wordpress.com/2012/04/26/android-google-maps-tutorial-part-7-drawing-a-path-or-line-between-two-locations/ but u guys don’t need to use the code from that tutorial. You can use ur current google maps app and just insert the OnTouchEvent method the YourOverlay Class that extends Overlay Class

A lil boring theory:

We will use the Geocoder Class. This class is used for Geocoding and Reverse Geocoding. Now in Reverse Geocoding things can be a lil tricky coz the returned latitude, longitude values depend on the string address details that a user has provided. It may be just a city name, a city name with a street name or a city name with street name and name of a building or specific location. The Geocoder class requires a backend service that is not included in the core android framework. The Geocoder query methods will return an empty list if there no backend service in the platform. The major public methods of this class are getFromLocation() and getFromLocationName(). The earlier one will transform lat,long values to string location names and the later one will get the coordinates from string location names. both the methods will return an array of address i.e, List< Addresses >.

Enough theory, now some real stuff

Add the following code to the class that extends Overlay Class


@Override
 public boolean onTouchEvent(MotionEvent event, MapView mapView)
 {
 //---when user lifts his finger---
 if (event.getAction() == 1) {
 GeoPoint p = mapView.getProjection().fromPixels(
 (int) event.getX(),
 (int) event.getY());

 Toast.makeText(getBaseContext(),
 p.getLatitudeE6() / 1E6 + "," +
 p.getLongitudeE6() /1E6 ,
 Toast.LENGTH_SHORT).show();

 Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
 try {
 List<Address> addresses = geoCoder.getFromLocation(p.getLatitudeE6() / 1E6, p.getLongitudeE6() / 1E6, 1);

 String strCompleteAddress= "";
 if (addresses.size() > 0)
 {
 for (int i=0; i<addresses.get(0).getMaxAddressLineIndex();i++)
strCompleteAddress+= addresses.get(0).getAddressLine(i) + "\n";
 }
 Log.i("MyLocTAG => ", strCompleteAddress);
 Toast.makeText(getBaseContext(), strCompleteAddress, Toast.LENGTH_LONG).show();
 }
 catch (IOException e) {
 Log.i("MyLocTAG => ", "this is the exception part");
 e.printStackTrace();
 }
 return true;
 }
 else
 return false;
 }

The first Toast will show only the latitude and longitude in number for the location that is touched. Then we created an object of the Geocoder class.


Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());

Than we called the getFromLocation method, provided it with the latitude and longitude values and the number of search results that will be returned.


List<Address> addresses = geoCoder.getFromLocation(p.getLatitudeE6()  / 1E6,  p.getLongitudeE6() / 1E6, 1);

After this we concatenate each value in the Addresses array list to get a single string with the complete location address.


for (int i=0; i<addresses.get(0).getMaxAddressLineIndex();i++)
 strCompleteAddress += addresses.get(0).getAddressLine(i) + "\n";

The last part displays a Toast that will show the string location name.


Toast.makeText(getBaseContext(), strCompleteAddress, Toast.LENGTH_LONG).show();

Geocoding snapshot showing the complete string location name that is touched on the map

Same is the case with Reverse Geocoding, if we have the string location name we can get the coordinates to that location by using Reverse Geocoding. The sample code for Reverse Geocoding is attached below.


Geocoder geoCoder = new Geocoder(this, Locale.getDefault());
 try {
 List<Address> addresses = geoCoder.getFromLocationName("Bhattai Rd Islamabad", 5);
 String strCompleteAddress = "";
 if (addresses.size() > 0) {
 p = new GeoPoint(
 (int) (addresses.get(0).getLatitude() * 1E6),
 (int) (addresses.get(0).getLongitude() * 1E6));
 mc.animateTo(p);
 mapView.invalidate();
 }
 } catch (IOException e) {
 e.printStackTrace();
 }

The reason behind writing this article is that I got a new machine and I was configuring it for Android development. Everything went well but when came to the step for generating the Google Maps API Key, I got stuck when couldn’t find “debug.keystore” file at the desired location inside “.android”  folder.

.android folder without debug.keystore file

If we don’t have debug.keystore file we can’t get Google Maps API Key. To solve this issue and to get the debug.keystore file in the desired location is

  1. Open Eclipse
  2. Start a new Android project
  3. Run the project in any AVD that’s already created.

When the project is successfully RUN on the Android Virtual Device. Now go to your “.android” folder and check. You will see that the “debug.keystore” is present there.

.android folder with debug.keystore

REASON

By default the “debug.keystore” file is not created in “.android” folder. When we successfully build our first project, the “debug.keystore” file is automatically created in “.android” folder.

Now we can use it to generate our Google Maps API Key.

Related Article

https://mirnauman.wordpress.com/2012/01/26/how-to-get-google-maps-api-key-for-android-issues-and-errors-solved/

 

I was robbed at gun point on 15th Mach 2012. Three masked men on a bike. They took my laptop, cell phones and all cash. Thanks to Allah that am still alive. The reason for putting it here is that currently i don’t have a laptop and coz of this i won’t be able to provide support about my articles till i get my hands on a new laptop. Secondly i have lost all my previous work. So to complete the mission that i have started  first i ll code again to reach the level where i was a few days earlier. Hope my frien

To get going with this tutorial one needs to go through all the previous 3 parts because we are using the same code from the previous tutorials and taking it a few steps further.

  1. https://mirnauman.wordpress.com/2012/01/30/using-google-maps-in-android-development-tutorial-part-1/
  2. https://mirnauman.wordpress.com/2012/02/07/using-gps-in-android-and-animating-google-maps-to-the-current-gps-location-android-tutorial-part-2/
  3. https://mirnauman.wordpress.com/2012/02/13/adding-image-to-googlemaps-using-map-overlays-android-tutorial-part-3/

Our main emphasis in this tutorial will be on adding a Menu to our Android GoogleMaps project and perform different actions using the buttons of that menu. So wat we will do is to create a menu with 3 buttons. Just like the one shown in the image below.

  1. My Current Location Button
  2. Map Satellite View Button
  3. Map Normal View Button

Menu with 3 buttons

Before we start we will copy 3 images(mylocation.png,satelliteview.png,normalview.png) in the drawables folder in the res folder. After this we will create a new xml file in the layout folder. We will name our file as “menu.xml”. The code for menu.xml is given below.

Menu XML file.

Save the menu.xml file and open GooglemapsActivity.java file. Now add the following code after the onCreate method in the GooglemapsActivity Class.

// Initiating Menu XML file (menu.xml)
 @Override
 public boolean onCreateOptionsMenu(Menu menu)
 {
 MenuInflater menuInflater = getMenuInflater();
 menuInflater.inflate(R.layout.menu, menu);
 return true;
 }

 /**
 * Event Handling for Individual menu item selected
 * Identify single menu item by it's id
 * */
 @SuppressWarnings("deprecation")
 @Override
 public boolean onOptionsItemSelected(MenuItem item)
 {

 switch (item.getItemId())
 {
 case R.id.my_location:
 // Single menu item is selected do something
 Toast.makeText(GooglemapsActivity.this, "Moving To Current location", Toast.LENGTH_SHORT).show();
 locLstnr.gpsCurrentLocation();

 return true;

 case R.id.mapview_normal:
 Toast.makeText(GooglemapsActivity.this, "Map Normal Street View", Toast.LENGTH_SHORT).show();
 if(mapView.isSatellite()==true){
 mapView.setSatellite(false);
 }
 return true;

 case R.id.mapview_satellite:
 Toast.makeText(GooglemapsActivity.this, "Map Satellite View", Toast.LENGTH_SHORT).show();
 if(mapView.isSatellite()==false){
 mapView.setSatellite(true);
 }
 return true;

 default:
 return super.onOptionsItemSelected(item);
 }
 }

In the above code, i have used locLstnr.gpsCurrentLocation(). To follow this completely one has to go through all the 3 previous parts of this tutorial. I have created a user defined function by the name of gpsCurrentLocation inside MyLocationListener Class. In this function I am getting the current coordinates and animating the map to that location. In the above code I have used the menu button to initiate this function. Now when we click the “My Location” button, the map will be animated to the current location, if we click the “Satellite View” button the map will be converted to satellite view and if we click the “Normal View” button than the map will be converted to Normal View.
Run and test the project and it will give us the following out put.

My Location Button Clicked and the map animated to current location

Satellite View Button Clicked and the map converted to Satellite View

Normal View Button Clicked and if the map is not is normal view it will be converted to normal view.

Now coming to the zooming functionality. All we need is just one line of code in the GooglemapsActivity Class. Add the following line after mapView = (MapView) findViewById(R.id.mapview1); in the onCreate method of our main GooglemapsActivity Class.


mapView.setBuiltInZoomControls(true);

Run the project and click on the map, click is must. As soon as the map is clicked the zooming controls will appear on the map.

Zoom Controls Enabled

Complete Source of MyLocationListener Class

 public class MyLocationListener implements LocationListener
 {

 @Override
 public void onLocationChanged(Location loc)
 {
 loc.getLatitude();
 loc.getLongitude();
 String Text = "My current location is: " +
 "Latitud = " + loc.getLatitude() +
 "Longitud = " + loc.getLongitude();
 Toast.makeText( getApplicationContext(),
 Text,
 Toast.LENGTH_SHORT).show();

 tvlat.setText(""+loc.getLatitude());
 tvlong.setText(""+loc.getLongitude());

 this.gpsCurrentLocation();
 }

 public void gpsCurrentLocation()
 {
 String coordinates[] = {""+tvlat.getText(), ""+tvlong.getText()};

 double lat = Double.parseDouble(coordinates[0]);
 double lng = Double.parseDouble(coordinates[1]);

 GeoPoint p = new GeoPoint(
 (int) (lat * 1E6),
 (int) (lng * 1E6));

mc.animateTo(p);
 mc.setZoom(7);

 MyMapOverlays marker = new MyMapOverlays(p) ;
 List listOfOverLays = mapView.getOverlays();
 listOfOverLays.clear();
 listOfOverLays.add(marker);

 mapView.invalidate();

 }

 @Override
 public void onProviderDisabled(String provider)
 {
 Toast.makeText( getApplicationContext(),
 "Gps Disabled",
 Toast.LENGTH_SHORT ).show();
 }

 @Override
 public void onProviderEnabled(String provider)
 {
 Toast.makeText( getApplicationContext(),
 "Gps Enabled",
 Toast.LENGTH_SHORT).show();
 }

 @Override
 public void onStatusChanged(String provider, int status, Bundle extras)
 {

 }

 }

Complete code of GooglemapsActivity Class

public class GooglemapsActivity extends MapActivity {

 private MapView mapView;
 private MapController mc;
 private TextView tvlat;
 private TextView tvlong;
 LocationManager locMgr;
 MyLocationListener locLstnr;
 Location mloc;

 /** Called when the activity is first created.
 * @return */

 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);

 mapView = (MapView) findViewById(R.id.mapview1);
 mc = mapView.getController();
 mapView.setBuiltInZoomControls(true);

 tvlat = (TextView)findViewById(R.id.tv_lat);
 tvlong = (TextView)findViewById(R.id.tv_long);

 tvlat.setText("0");
 tvlong.setText("0 ");

 locMgr = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
 locLstnr = new MyLocationListener();

 locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locLstnr);

 }

 // Initiating Menu XML file (menu.xml)
 @Override
 public boolean onCreateOptionsMenu(Menu menu)
 {
 MenuInflater menuInflater = getMenuInflater();
 menuInflater.inflate(R.layout.menu, menu);
 return true;
 }

 /**
 * Event Handling for Individual menu item selected
 * Identify single menu item by it's id
 * */
 @SuppressWarnings("deprecation")
 @Override
 public boolean onOptionsItemSelected(MenuItem item)
 {

 switch (item.getItemId())
 {
 case R.id.my_location:
 // Single menu item is selected do something
 Toast.makeText(GooglemapsActivity.this, "Moving To Current location", Toast.LENGTH_SHORT).show();
 locLstnr.gpsCurrentLocation();

 return true;

 case R.id.mapview_normal:
 Toast.makeText(GooglemapsActivity.this, "Map Normal Street View", Toast.LENGTH_SHORT).show();
 if(mapView.isSatellite()==true){
 mapView.setSatellite(false);
 }
 return true;

 case R.id.mapview_satallite:
 Toast.makeText(GooglemapsActivity.this, "Map Satallite View", Toast.LENGTH_SHORT).show();
 if(mapView.isSatellite()==false){
 mapView.setSatellite(true);
 }
 return true;

  default:
 return super.onOptionsItemSelected(item);
 }
 }

}

Note:- Please leave your comments if this article was helpful.

Update # 1:- In response to Max Comment # 1:-

Dear Max,
ur confusion is right coz i have slightly changed the logic in the MyLocationListener Class and have not posted the Updated code.. The target was to click a button and move to the current location of the GPS. But it was not that simple. So here is wat i did.
In the onLocationChanged() method in MyLocationListener Class i assigned the Latitude and Longitude to two hidden textviews.

public void onLocationChanged(Location loc)
{
loc.getLatitude();
loc.getLongitude();
String Text = “My current location is: ” +
“Latitud = ” + loc.getLatitude() +
“Longitud = ” + loc.getLongitude();
Toast.makeText( getApplicationContext(),
Text,
Toast.LENGTH_SHORT).show();

tvlat.setText(“”+loc.getLatitude());
tvlong.setText(“”+loc.getLongitude());
}

Than i created a new method inside MyLocationListener Class i.e, gpsCurrentLocation(). In gpsCurrentLocation() method i have used the code to get the coordinates from the textviews and animate the map to that location.

public void gpsCurrentLocation()
{
String coordinates[] = {“”+tvlat.getText(), “”+tvlong.getText()};

double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);

GeoPoint p = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));

mc.animateTo(p);
mc.setZoom(7);

MyMapOverlays marker = new MyMapOverlays(p) ;
List listOfOverLays = mapView.getOverlays();
listOfOverLays.clear();
listOfOverLays.add(marker);

mapView.invalidate();
}

After this call the gpsCurrentLocation() method in the OnLocationChanged() too . so that the method becomes like this


public class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location loc)
{
loc.getLatitude();
loc.getLongitude();
String Text = “My current location is: ” +
“Latitud = ” + loc.getLatitude() +
“Longitud = ” + loc.getLongitude();
Toast.makeText( getApplicationContext(),
Text,
Toast.LENGTH_SHORT).show();

tvlat.setText(“”+loc.getLatitude());
tvlong.setText(“”+loc.getLongitude());

this.gpsCurrentLocation();
}

Now come to the code of ur button click, or in my case menu click.
Create an object of MyLocationListener Class.
and simply call the the gpsCurrentLocation() method through that object. and u will be able to animate the map to the current GPS coordinates.
MyLocationListener locLstnr = new MyLocationListener();
locLstnr.gpsCurrentLocation();

Plz leave ur comments if this solves the issue of animating the map to the current GPS coordinates on button click or menu click.

 

Update # 2: In response to Sagar Zade issue.

LinearLayout Tag Code.

LinearLayout Tag Code.

Objective of this tutorial is to place an image to the current location of the device, means displaying an image at the current coordinates from the GPS of the device. We can have a better idea from the image displayed below of what we are trying to achieve.

A Cross image added to the current location from the GPS of the device

To start with this tutorial we will continue with the examples  from the previous tutorial and to have the exact output one must go through the previous tutorials in this series i.e,

First of all we will create a PNG image with transparent background or download it from Google or search it in the android samples folders. When we have our desired image just like the one shown below.

Our PNG

Copy this PNG and browse to your “res” folder of the GoogleMaps project. My path for the “res” folder is “C:\Users\Mir Nauman Tahir\workspace\googlemaps\res\” . In “res” folder we will have 3 more folders starting from drawable i.e

  1. drawable-hdpi
  2. drawable-ldpi
  3. drawable-mdpi

we have to copy our image in the the drawable folder but which one. If you have no idea copy the image to all the three folders. By default the image will be taken from the hdpi folder, if the image is not found by that name in the hdpi folder than the mdpi folder is searched. so we have to have our image in one of the two folders.  If the same image is present in both the folders than the hdpi image is taken.

The reason behind putting our image in the “res” folder is that we can easily access our image file by “R.id.our_image” code. extension should not be entered. Using this method saves us a lot of overhead by not getting involved in the path to the file and the addressing schemes whether relative or absolute.

Now coming to the code. First we will create our own Map Overlay Class that will extend com.google.android.maps.Overlay. For the time being the Class should be defined inside our main GooglemapsActivity Class. The code is given below.

/*My overlay Class starts*/
 class MyMapOverlays extends com.google.android.maps.Overlay
 {
 GeoPoint location = null;

public MyMapOverlays(GeoPoint location)
 {
 super();
 this.location = location;
 }

@Override
 public void draw(Canvas canvas, MapView mapView, boolean shadow)
 {

 super.draw(canvas, mapView, shadow);
 //translate the screen pixels
 Point screenPoint = new Point();
 mapView.getProjection().toPixels(this.location, screenPoint);

 //add the image
 canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.our_cross_image),
 screenPoint.x, screenPoint.y , null); //Setting the image  location on the screen (x,y).
 }
 }
 /*My overlay Class ends*/

We have our main map Overlay class in place, now we will come to our MyLocationListener Class. This is not necessary here. The following code can also be defined in onCreate method of our main GooglemapsActivity Class but as we want to draw the image on the current location of the GPS and that if the GPS coordinates changes the image should reapear in the newly updated location we will add this code to MyLocationListener Class.

Add the following code in the “onLocationChanged(Location loc)”  between “mc.setZoom(7)” and mapView.invalidate(). From our previous tutorial code https://mirnauman.wordpress.com/2012/02/07/using-gps-in-android-and-animating-google-maps-to-the-current-gps-location-android-tutorial-part-2/

//add a location marker.

 MyMapOverlays marker = new MyMapOverlays(p) ;
 List listOfOverLays = mapView.getOverlays();
 listOfOverLays.clear();
 listOfOverLays.add(marker);

 mapView.invalidate();

mapView.invalidate() should be the last statement coz it will redraw the map. Now Run the project. When the map loads for the first time it will show no image. At this stage send dummy coordinates from the DDMS and the map will animate to the current coordinates receieved from the DDMS and will show our “Cross” image on the current location.

Sending dummy coordinates from DDMS, Map animated to the current location and displaying our "Cross" image at the current location

This was all for adding images using map overlays. We will extend the same tutorial series for exploring further functionalities of GoogleMaps.

Note:- Please leave your comments if this article was of any help. Thanks.

This tutorial is first of the detailed tutorial that am planning to write. Some short details about my development environment.

  • Windows 7 Professional
  • Android 2.3.3(API10)
  • Google APIs (Google Inc)-API Level 10. (Create AVD of this API)
  • Eclipse Classic version 3.7.0

Before we start with the development of our first Google Maps Application for Android. We need to get Google Maps Api Key. I have made it very simple to get the key. Follow the link.

https://mirnauman.wordpress.com/2012/01/26/how-to-get-google-maps-api-key-for-android-issues-and-errors-solved/

Perform all the steps in the above link and get the key. When we have the key than we will proceed will our development of Google Maps Application for Android.

Start a new Android Project, when prompted for SDK selection

SDK selection

Select Google APIs Platform 2.3.3 API level 10, and move next. When the project has been created. Open your MainActivity.java file. In my case I have named it GooglemapsActivity.java. Import the following.


import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;

Now change the line

public class GooglemapsActivity extends Activity {

to

public class GooglemapsActivity extends MapActivity {

Now open the main.xml file and add a MapView

<com.google.android.maps.<span class="hiddenSpellError">MapView
 android:id="@+id/mapview1"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:enabled="true"
 android:clickable="true"
 android:apiKey=""
 />


Save the changes. Now open the AndroidManifest.xml file and Add the following permissions above the application tag.

<uses-permission android:name="android.permission.INTERNET"/>
 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

Now add the google maps library in the manifest file, inside the application tag above the activity tag.

<uses-library android:name="com.google.android.maps"/>

Save the changes.  Now Open the Android Virtual Device Manager and create a new AVD with the following settings.

Creating & Configuring New AVD

We must provide GPS and SD Card support in the AVD because we will need these two functionalities for our future tutorials. Now Right click on our googlemaps application from the PackageExplorer and click Run Configuration from Run As menu. Now Create new configuration from the selecting our googlemaps application on the Android Tab.

Run Configuration For Our Android Project

Selected Target AVD

And Select our recently created AVD from the target Tab. Click Apply and Run. Everything is fine still the application will not load any map. Reason is that, we have not created an instance of our MapView in our main GooglemapsActivity Class. To do that. Add the following lines of code below the setContentView() line.

mapView = (MapView) findViewById(R.id.mapview1);
mc = mapView.getController();

Save the changes and run the project again. Now everything is double fine but still the emulator will show empty grid like the one shown in the picture.

Google Maps Empty Grid

Reasons for this blank grid is that we have not supplied our application with the google maps api key. For this Open the main.xml file and replace the following code.

<com.google.android.maps.MapView
 android:id="@+id/mapview1"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:enabled="true"
 android:clickable="true"
 android:apiKey=""
 />

with

<com.google.android.maps.MapView
 android:id="@+id/mapview1"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:enabled="true"
 android:clickable="true"
 android:apiKey="xxxxxxxxxxxxxxxxxxxxxxxxxxx"
 />

The “xxxxxxxxxxxxxxxxxxxxxxx” show your Google Maps API Key that we have generated at the start of this tutorial. If the Google Maps API Key is correct than we will get a screen like this.

Default Map Location Loaded in the MapView Control

We can animate the map to a default location or to our desired coordinates. Add the following lines of code and the map will load at our desired coordinates and our desired zoom level.

String coordinates[] = {"30", "71"};
 double lat = Double.parseDouble(coordinates[0]);
 double lng = Double.parseDouble(coordinates[1]);

 GeoPoint p = new GeoPoint(
 (int) (lat * 1E6),
 (int) (lng * 1E6));

mc.animateTo(p);
 mc.setZoom(7);
 mapView.invalidate();

Map animated to our desired location

We have provided latitude = 30 and longitude = 71, zooming level =7. The map will center at the provided coordinates at the provided zooming level. I may have forgotten to explain some details so i am attaching the complete code the 3 main files for this project.

GooglemapsActivity.java Complete code

package com.maptest.googlemap;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import android.os.Bundle;

public class GooglemapsActivity extends MapActivity {

 private MapView mapView;
 private MapController mc;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);

 mapView = (MapView) findViewById(R.id.mapview1);
 mc = mapView.getController();

String coordinates[] = {"30", "71"};
 double lat = Double.parseDouble(coordinates[0]);
 double lng = Double.parseDouble(coordinates[1]);

 GeoPoint p = new GeoPoint(
 (int) (lat * 1E6),
 (int) (lng * 1E6));

mc.animateTo(p);
 mc.setZoom(7);
 mapView.invalidate();
 }

@Override
 protected boolean isRouteDisplayed() {
 // TODO Auto-generated method stub
 return false;
 }
}

AndroidManifest.xml Complete Code

<?xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.maptest.googlemap"
 android:versionCode="1"
 android:versionName="1.0" >

<uses-sdk android:minSdkVersion="10" />
 <uses-permission android:name="android.permission.INTERNET"/>
 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<application
 android:icon="@drawable/ic_launcher"
 android:label="@string/app_name" >

 <uses-library android:name="com.google.android.maps"/>

 <activity
 android:name=".GooglemapsActivity"
 android:label="@string/app_name" >
 <intent-filter>
 <action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>
 </activity>
 </application>

</manifest>

main.xml Complete Code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical" >

<com.google.android.maps.MapView
 android:id="@+id/mapview1"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:enabled="true"
 android:clickable="true"
 android:apiKey="xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
 />

</LinearLayout>

Note:- Please leave your comments if this article was helpful.

Update # 1.

Addressing the issues in comments from harrison , hennie and topan. The problem with force cash is solved by the following :

replace ur mapview control code in main.xml with this

<com.google.android.maps.MapView
        android:id=”@+id/mapview1″
        android:layout_width=”fill_parent”
        android:layout_height=”322dp”
        android:layout_weight=”0.64″
        android:apiKey=”0VUxp2vFvlmp4w3BsQu-xxxxxxxxxxxxxxxxxxxxxxg”
        android:clickable=”true” >
    </com.google.android.maps.MapView>
if this doesnt work than check ur google maps api key.
The above solution worked for Harrison and his reply to me was
Mir!Thank you very much, you have gotten this to work for me! Thank you very much!The solution was:
<com.google.android.maps.MapView

>

    </com.google.android.maps.MapView>For some reason it is really strict about the format of com.google.android.maps.MapViewThank you very much! Your help is GREATLY appreciated and now I can resume working on my design project.Thanks again,

Harrison

Update # 2.
Issue in which maps is not shown on the phone, if only dark screen is shown than this can be solved by zooming out a few times. Zooming out will solve the blackout screen problem. If map grid shows but no map appears than check the google maps api key.
Update # 3
For Android Google Maps API Ver 2.0 developers follow the following link.

 

Blog Stats

  • 327,480 hits

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 233 other followers

%d bloggers like this: