maps google

print
<uses-permission android:name="ANDROID.PERMISSION.INTERNET" />
<uses-permission android:name="ANDROID.PERMISSION.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>

//access location of the phone

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

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >

<meta-data android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyAqhlJdujwA4_XYCVoDk0t3zG9H_TM3jZ4"/>

<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>

</application>

layout activity_map.xml

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:cameraTargetLat:"34"
map:cameraTargetLng:"-73.97"
map"CameraZoom="15"
map:uiRotateGestures:"false"
map:uiTiltGestures:"false"
map:uiScrollGestures:"false"
map:uiZoomGestures:"false"
map:mapType="hybrid"
/>

menu_main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/mapTypeNormal"
android:title="@string/normal" />
<item
android:id="@+id/mapTypeSatellite"
android:title="@string/satellite" />
<item
android:id="@+id/mapTypeTerrain"
android:title="@string/terrain" />
<item
android:id="@+id/mapTypeHybrid"
android:title="@string/hybrid" />
<item
android:id="@+id/mapTypeNone"
android:title="@string/none" />

</menu>

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();

//Add menu handling code
switch (id){
case R.id.mapTypeNone:
mMap.setMapType(GoogleMap.MAP_TYPE_NONE);
break;
case R.id.mapTypeNone:
mMap.setMapType(GoogleMap.MAP_TYPE_NOrmal);
break;
case R.id.mapTypeNone:
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
break;
case R.id.mapTypeNone:
mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
break;

}

return super.onOptionsItemSelected(item);
}

implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener;

private Marker marker;
private LocationListener mListener;// (com.google.android.gms.location)
private GoogleApiClient mLocationClient;
GoogleMap mMap;
private static final int ERROR_DIALOG_REQUEST = 9001;
private static final double
SEATTLE_LAT = 47.60621,
SEATTLE_LNG =-122.33207,
SYDNEY_LAT = -33.867487,
SYDNEY_LNG = 151.20699,
NEWYORK_LAT = 40.714353,
NEWYORK_LNG = -74.005973;

if (servicesOK()) {
setContentView(R.layout.activity_map);

if (initMap()) {
Toast.makeText(this, "Ready to map!", Toast.LENGTH_SHORT).show();
gotoLocation(SEATTLE_LAT, SEATTLE_LNG, 15);

// mMap.setMyLocationEnabled(true);

mLocationClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();

mLocationClient.connect();
} else {
Toast.makeText(this, "Map not connected!", Toast.LENGTH_SHORT).show();
}

} else {
setContentView(R.layout.activity_main);
}

public boolean servicesOK() {

int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

if (isAvailable == ConnectionResult.SUCCESS) {
return true;
} else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) {
Dialog dialog =
GooglePlayServicesUtil.getErrorDialog(isAvailable, this, ERROR_DIALOG_REQUEST);
dialog.show();
} else {
Toast.makeText(this, "Can't connect to mapping service", Toast.LENGTH_SHORT).show();
}

return false;
}
private boolean initMap() {
if (mMap == null) {
SupportMapFragment mapFragment =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mMap = mapFragment.getMap();
}
return (mMap != null);

if (mMap != null) {
mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker marker) {
return null;
}

@Override
public View getInfoContents(Marker marker) {
View v = getLayoutInflater().inflate(R.layout.info_window, null);
TextView tvLocality = (TextView) v.findViewById(R.id.tvLocality);
TextView tvLat = (TextView) v.findViewById(R.id.tvLat);
TextView tvLng = (TextView) v.findViewById(R.id.tvLng);
TextView tvSnippet = (TextView) v.findViewById(R.id.tvSnippet);

LatLng latLng = marker.getPosition();
tvLocality.setText(marker.getTitle());
tvLat.setText("Latitude: " + latLng.latitude);
tvLng.setText("Longitude: " + latLng.longitude);
tvSnippet.setText(marker.getSnippet());

return v;
}
});

mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
@Override
public void onMapLongClick(LatLng latLng) {
Geocoder gc = new Geocoder(MainActivity.this);
List<Address> list = null;

try {
list = gc.getFromLocation(latLng.latitude, latLng.longitude, 1);
} catch (IOException e) {
e.printStackTrace();
return;
}

Address add = list.get(0);
MainActivity.this.addMarker(add, latLng.latitude, latLng.longitude);
}
});

mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
String msg = marker.getTitle() + " (" +
marker.getPosition().latitude + ", " +
marker.getPosition().longitude + ")";
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
return false;
}
});
//dragging
nMap.serOnMarkerDragListener(new GoogleMap.OnMarkerDragListener(){
@override
public void onMarkerDragStart(marker marker)

@override
public void onMarkerDrag(marker marker)
@override
public void onMarkerDragDrag(marker marker){
eocoder gc = new Geocoder(MainActivity.this);
List<Address> list = null;

try {
list = gc.getFromLocation(latLng.latitude, latLng.longitude, 1);
} catch (IOException e) {
e.printStackTrace();
return;
}

Address add = list.get(0);
marker.setTitile(add.getLocality());
mark.setSnippet(add.getCountryName());
marker.showInfoWindow();
}

}
}
}

//zoom 1-15 floats
private void gotoLocation(double lat, double lng, float zoom) {
LatLng latLng = new LatLng(lat, lng);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(latLng, zoom);
mMap.moveCamera(update);
}

private void hideSoftKeyboard(View v) {
InputMethodManager imm =
(InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}

public void geoLocate(View v) throws IOException {

hideSoftKeyboard(v);

TextView tv = (TextView) findViewById(R.id.editText1);
String searchString = tv.getText().toString();

Geocoder gc = new Geocoder(this);
List<Address> list = gc.getFromLocationName(searchString, 1);

if (list.size() > 0) {
Address add = list.get(0);
String locality = add.getLocality();
Toast.makeText(this, "Found: " + locality, Toast.LENGTH_SHORT).show();

double lat = add.getLatitude();
double lng = add.getLongitude();
gotoLocation(lat, lng, 15);

//to delete previous markers
if(marker!=null){
makrer.remove();
}

MarkerOptions options = new MarkerOptions()
.title(locality)
.position(new LatLng(lat,lng))
//.icon(BitMadescriptorFactory.defaultMarket(BitMadescriptorFactory.HUE_BLUE));
.icon(BitMadescriptorFactory.fromResource());

nMap.addMarker(options);

// addMarker(add, lat, lng);
}

}

private void addMarker(Address add, double lat, double lng) {
MarkerOptions options = new MarkerOptions()
.title(add.getLocality())
.position(new LatLng(lat, lng))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_map_marker));

String country =add.getCountryName();
if(country.length()>0){
options.snippet(country);
}

marker = mMap.addMarker(options);
}

if (servicesOK() && initMap()) {

Geocoder gc = new Geocoder(this);
List<Address> list;
try {
list = gc.getFromLocationName(hotel.getAddress(), 1);
Address address = list.get(0);
double lat = address.getLatitude();
double lng = address.getLongitude();
LatLng latLong = new LatLng(lat, lng);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(latLong, 15);
mMap.moveCamera(update);
} catch (IOException e) {
Toast.makeText(this, getString(R.string.error_finding_hotel), Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
Log.d(this.getLocalClassName(), e.getMessage());
}

}

public void showCurrentLocation(MenuItem item) {
Location currentLocation = LocationServices.FusedLocationApi
.getLastLocation(mLocationClient);
if (currentLocation == null) {
Toast.makeText(this, "Couldn't connect!", Toast.LENGTH_SHORT).show();
} else {
LatLng latLng = new LatLng(
currentLocation.getLatitude(),
currentLocation.getLongitude()
);

CameraUpdate update = CameraUpdateFactory.newLatLngZoom(
latLng, 15
);
mMap.animateCamera(update);
}

}

@Override
public void onConnected(Bundle bundle) {
Toast.makeText(this, "Ready to map!", Toast.LENGTH_SHORT).show();
mListener=new Location listenet(){
@override
public void onLocationchanged(Location location){
location.getLatitude();
location.getLongitude();
gotoLocation(location.getLatitude(),location.getLongitude(),15);
LocationRequest.setPrioruty(LocationRequest.PRIORITY_HIGH_ACCURACY); //PRIORITY_HIGH_ACCURACY, BALANCED POWER, LOW POWER, NO POWER
request.setInterval(5000);
request.setFastestInterval(1000);
LocationServices.FocusLocationApi.requestLocationUpdates(
mLocationClient,request,mListener
);
}

}
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

}

@Override
public void onPause(){
super.onPause();
LocationServices.FusedLocationApi.removeLocationUpdates(
mLocationClient,mListener
);
}
Polyline line;
public void drawLine(){
PolylineOptions lineOtptions = new PolylineOptions()
.add(marker1.getPosition())
.add(marker2.getPosition())
line=mMap.addPolyline(lineOptions);
}
public void removeEverything(){

}

private void removeEverything() {
for (Marker marker : markers) {
marker.remove();
}
markers.clear();
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.