googlemap学习

来源:互联网 发布:thinkpad 知乎 编辑:程序博客网 时间:2024/03/28 18:28
 

<!--  获取得到的地图密匙 -->    

  <com.google.android.maps.MapView

                 android:layout_width="fill_parent"

                 android:layout_height="fill_parent"

                 android:apiKey="0EGaQ0LojZADsSw-E2Nh0NIYkPuTgNKFSmvHWsA"

                 />

 

package my.yaner.googlemap;

 

import my.yaner.R;

import android.app.Activity;

import android.content.Context;

import android.location.Location;

import android.location.LocationListener;

import android.location.LocationManager;

import android.os.Bundle;

import android.widget.TextView;

/**

 * google地图搜索应用启动类

 * @author Administrator

 * 通过调用andorid自带的API实现地域的查询

 */

public class GoogleMapStartActivity extends Activity {

 

         protected void onCreate(Bundle savedInstanceState){

                   super.onCreate(savedInstanceState);

                   setContentView(R.layout.googlemap);

                  

                   init();

         }

 

         private void init() {

                   String serviceString = Context.LOCATION_SERVICE;

                   LocationManager locationManager = (LocationManager) getSystemService(serviceString);

                   String provider = LocationManager.GPS_PROVIDER;

                   Location location = locationManager.getLastKnownLocation(provider);                 //获取当前的位置信息

                   getLocationInfo(location);

                   locationManager.requestLocationUpdates(provider, 2000, 0, locationListener);

         }

        

         private void getLocationInfo(Location location) {

                   String latLongInfo;

                   TextView locationText = (TextView) findViewById(R.id.google_str_textview);

                   if(location != null){

                            double lat = location.getLatitude();

                            double lng = location.getLongitude();

                            latLongInfo = "Lat: " + lat + " ,nLong: " + lng;

                   }else{

                            latLongInfo = "no location found";

                   }

                   locationText.setText("您当前所在的位置是:" + latLongInfo);

         }

 

         private LocationListener locationListener = new LocationListener() {

                  

                   @Override

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

                                                       

                   }

                  

                   @Override

                   public void onProviderEnabled(String provider) {

                            getLocationInfo(null);

                           

                   }

                  

                   @Override

                   public void onProviderDisabled(String provider) {

                            getLocationInfo(null);                        

                   }

                  

                   @Override

                   public void onLocationChanged(Location location) {

                            getLocationInfo(location);

                   }

         };

}

原创粉丝点击