- Go to File>New>Project.
- Select Android>Android Application Project and click Next>.
- Enter applciation name.
- Enter package name. This should be the same package name you used when you created API key.
- Select API 8: Android 2.2 (Froyo) in Minimu Required SDK.
- Keep others as default and finish.
- select Project>properties and go to Android.
- In the Library box click 'Add...' button.
- select 'google-play-services_lib' on the popup and click 'ok' button.
3. Create New Activity and Layout.
- Go to File>New>Others.
- Select Android>Android Activity and click Next to the end.
- Add google map fragment to your layout xml file.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/gmap"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
</LinearLayout>
- You may find some useful codes to control gmap.
- You can extends GMapActivity from FragmentActivty rather than ActionBarActivity.
package com.example.googlemaptest;
import java.io.IOException;
import java.util.List;import java.util.Locale;
import android.location.Address;
import android.location.Geocoder;import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;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 GMapActivity extends ActionBarActivity {
private GoogleMap _gMap;private int _mapType = GoogleMap.MAP_TYPE_NORMAL;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);setContentView(R.layout.activity_gmap);
Bundle extras = this.getIntent().getExtras();
Location location = null;
if (extras != null && extras.containsKey("GPSINFO")) {
location = (Location)extras.get("GPSINFO");}
FragmentManager fragmentManager = getSupportFragmentManager();
SupportMapFragment mapFragment = (SupportMapFragment)
fragmentManager.findFragmentById(R.id.gmap);
_gMap = mapFragment.getMap();_gMap.getUiSettings().setCompassEnabled(true);
_gMap.getUiSettings().setZoomControlsEnabled(true);
_gMap.getUiSettings().setMyLocationButtonEnabled(true);
_gMap.getUiSettings().setAllGesturesEnabled(true);
_gMap.getUiSettings().setMyLocationButtonEnabled(true);
_gMap.getUiSettings().setRotateGesturesEnabled(true);
_gMap.getUiSettings().setTiltGesturesEnabled(true);
_gMap.getUiSettings().setZoomGesturesEnabled(true);
_gMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
LatLng sfLatLng = null;
if(location != null){double lat = location.getLatitude();
double lng = location.getLongitude();
sfLatLng = new LatLng(lat, lng);
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocation(lat, lng, 1);
} catch (IOException e) {
// TODO Auto-generated catch blocke.printStackTrace();
}
if(addresses != null){
_gMap.addMarker(new MarkerOptions().position(sfLatLng)
.title("Position in " + addresses.get(0).getAddressLine(1) + " " + addresses.get(0).getAddressLine(2))
.snippet(addresses.get(0).getAddressLine(0))
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
}else{_gMap.addMarker(new MarkerOptions()
.position(sfLatLng)
.title("Position")
.snippet("No address is available").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
}
_gMap.animateCamera(CameraUpdateFactory.newLatLngZoom(sfLatLng, 13));
}else{}
}
5. Change Manifast file.
- add uses-feature.
- add premission.
- add meta-data for API key.
- add meta-data for gms version.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.googlemaptest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<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" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
... >
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIASDFASlSG6qfsadfdfZtZVnuZTJyfsSDFSD2" />
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
6. Call GMapActivity
- You can add this code to call GMapActvity.
Location location = new Location("You are here");
location.setLatitude(latitude);
location.setLongitude(longitude);
Intent intent = new Intent(getBaseContext(), GMapActivity.class);
intent.putExtra("GPSINFO", location);
startActivity(intent);
- In some older verion android phone google play service should be updated.
- In the case of content filtering message, go to Menu>Settings in Google Play Store and unlock Content Filtering in User Controls.

No comments:
Post a Comment